@@ -49,6 +49,17 @@ export function getColorChannels(colorSpace: ColorSpace) {
49
49
}
50
50
}
51
51
52
+ /**
53
+ * Returns the hue value normalized to the range of 0 to 360.
54
+ */
55
+ export function normalizeHue ( hue : number ) {
56
+ if ( hue === 360 ) {
57
+ return hue ;
58
+ }
59
+
60
+ return ( ( hue % 360 ) + 360 ) % 360 ;
61
+ }
62
+
52
63
// Lightness threshold between orange and brown.
53
64
const ORANGE_LIGHTNESS_THRESHOLD = 0.68 ;
54
65
// Lightness threshold between pure yellow and "yellow green".
@@ -145,7 +156,7 @@ abstract class Color implements IColor {
145
156
} else if ( c >= 0.15 ) {
146
157
chroma = 'vibrant' ;
147
158
}
148
-
159
+
149
160
if ( l < 0.3 ) {
150
161
lightness = 'very dark' ;
151
162
} else if ( l < MAX_DARK_LIGHTNESS ) {
@@ -435,7 +446,7 @@ class HSBColor extends Color {
435
446
let m : RegExpMatchArray | void ;
436
447
if ( ( m = value . match ( HSB_REGEX ) ) ) {
437
448
const [ h , s , b , a ] = ( m [ 1 ] ?? m [ 2 ] ) . split ( ',' ) . map ( n => Number ( n . trim ( ) . replace ( '%' , '' ) ) ) ;
438
- return new HSBColor ( mod ( h , 360 ) , clamp ( s , 0 , 100 ) , clamp ( b , 0 , 100 ) , clamp ( a ?? 1 , 0 , 1 ) ) ;
449
+ return new HSBColor ( normalizeHue ( h ) , clamp ( s , 0 , 100 ) , clamp ( b , 0 , 100 ) , clamp ( a ?? 1 , 0 , 1 ) ) ;
439
450
}
440
451
}
441
452
@@ -565,10 +576,6 @@ class HSBColor extends Color {
565
576
// - hsla(X, X%, X%, X)
566
577
const HSL_REGEX = / h s l \( ( [ - + ] ? \d + (?: .\d + ) ? \s * , \s * [ - + ] ? \d + (?: .\d + ) ? % \s * , \s * [ - + ] ? \d + (?: .\d + ) ? % ) \) | h s l a \( ( [ - + ] ? \d + (?: .\d + ) ? \s * , \s * [ - + ] ? \d + (?: .\d + ) ? % \s * , \s * [ - + ] ? \d + (?: .\d + ) ? % \s * , \s * [ - + ] ? \d ( .\d + ) ? ) \) / ;
567
578
568
- function mod ( n , m ) {
569
- return ( ( n % m ) + m ) % m ;
570
- }
571
-
572
579
class HSLColor extends Color {
573
580
constructor ( private hue : number , private saturation : number , private lightness : number , private alpha : number ) {
574
581
super ( ) ;
@@ -578,7 +585,7 @@ class HSLColor extends Color {
578
585
let m : RegExpMatchArray | void ;
579
586
if ( ( m = value . match ( HSL_REGEX ) ) ) {
580
587
const [ h , s , l , a ] = ( m [ 1 ] ?? m [ 2 ] ) . split ( ',' ) . map ( n => Number ( n . trim ( ) . replace ( '%' , '' ) ) ) ;
581
- return new HSLColor ( mod ( h , 360 ) , clamp ( s , 0 , 100 ) , clamp ( l , 0 , 100 ) , clamp ( a ?? 1 , 0 , 1 ) ) ;
588
+ return new HSLColor ( normalizeHue ( h ) , clamp ( s , 0 , 100 ) , clamp ( l , 0 , 100 ) , clamp ( a ?? 1 , 0 , 1 ) ) ;
582
589
}
583
590
}
584
591
0 commit comments