Skip to content

Commit

Permalink
Pull request comments actioned
Browse files Browse the repository at this point in the history
simplify code
  • Loading branch information
williamforster committed Jun 10, 2024
1 parent acf192f commit 612e9e1
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 18 deletions.
22 changes: 9 additions & 13 deletions src/color/Color.ts
Original file line number Diff line number Diff line change
Expand Up @@ -255,14 +255,13 @@ export class Color {
* @see http://http://www.w3.org/TR/css3-color/#hsl-color
*/
static sourceFromHsl(color: string): TRGBAColorSource | undefined {
const noNone = color.replaceAll('none', '0');
const match = noNone.match(reHSLa());
const match = color.match(reHSLa());
if (!match) {
return;
}
const match1degrees = Color.parseAngletoDegrees(match[1]);

const h = (((parseFloat(match1degrees) % 360) + 360) % 360) / 360,
const h = (((match1degrees % 360) + 360) % 360) / 360,
s = parseFloat(match[2]) / 100,
l = parseFloat(match[3]) / 100;
let r: number, g: number, b: number;
Expand Down Expand Up @@ -329,23 +328,20 @@ export class Color {
* @param {String} value ex: 0deg, 0.5turn, 2rad
* @return {String} numeric string in degrees, or '0' for improper input
*/
static parseAngletoDegrees(value: string): string {
static parseAngletoDegrees(value: string): number {
const lowercase = value.toLowerCase();
if (lowercase.indexOf('deg') > -1) {
return lowercase.replace('deg', '');
}
const numeric = parseFloat(lowercase);

if (lowercase.indexOf('rad') > -1) {
if (lowercase.includes('rad')) {
const numeric = parseFloat(lowercase.replace('rad', ''));
return numeric === undefined ? '0' : radiansToDegrees(numeric).toString();
return radiansToDegrees(numeric);
}

if (lowercase.indexOf('turn') > -1) {
const numeric = parseFloat(lowercase.replace('turn', ''));
return numeric === undefined ? '0' : (numeric * 360).toString();
if (lowercase.includes('turn')) {
return numeric * 360;
}

// Value is probably just a number already in degrees eg '50'
return lowercase;
return numeric;
}
}
5 changes: 0 additions & 5 deletions src/color/color.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,6 @@ describe('Color regex and conversion tests', () => {
expect(color1.getSource().toString()).toBe([192, 63, 106, 0.25].toString());
});

it('Converts a hsla color with empty entries to rgba', () => {
const color1 = new Color('hsla(none,none,50%,0.5)');
expect(color1.getSource().toString()).toBe([128, 128, 128, 0.5].toString());
});

it('Converts a hsla color with angle deg to rgba', () => {
const color1 = new Color('hsl(120deg,100%,50%)');
expect(color1.getSource().toString()).toBe([0, 255, 0, 1].toString());
Expand Down

0 comments on commit 612e9e1

Please sign in to comment.