@@ -7,7 +7,9 @@ import {valueError} from '../utils';
7
7
import {
8
8
fuzzyAssertInRange ,
9
9
fuzzyEquals ,
10
+ fuzzyGreaterThanOrEquals ,
10
11
fuzzyHashCode ,
12
+ fuzzyLessThan ,
11
13
fuzzyRound ,
12
14
positiveMod ,
13
15
} from './utils' ;
@@ -123,19 +125,6 @@ function NaNtoZero(val: number): number {
123
125
return Number . isNaN ( val ) ? 0 : val ;
124
126
}
125
127
126
- /**
127
- * Assert that `val` is either `NaN` or within `min` and `max`. Otherwise,
128
- * throw an error.
129
- */
130
- function assertClamped (
131
- val : number ,
132
- min : number ,
133
- max : number ,
134
- name : string
135
- ) : number {
136
- return Number . isNaN ( val ) ? val : fuzzyAssertInRange ( val , min , max , name ) ;
137
- }
138
-
139
128
/** Convert from sRGB (0-1) to RGB (0-255) units. */
140
129
function coordToRgb ( val : number ) : number {
141
130
return val * 255 ;
@@ -493,10 +482,14 @@ export class SassColor extends Value {
493
482
break ;
494
483
495
484
case 'hsl' : {
496
- const hue = normalizeHue ( options . hue ?? NaN ) ;
497
- const saturation = options . saturation ?? NaN ;
498
- let lightness = options . lightness ?? NaN ;
499
- lightness = assertClamped ( lightness , 0 , 100 , 'lightness' ) ;
485
+ let hue = normalizeHue ( options . hue ?? NaN ) ;
486
+ let saturation = options . saturation ?? NaN ;
487
+ const lightness = options . lightness ?? NaN ;
488
+ if ( ! Number . isNaN ( saturation ) && fuzzyLessThan ( saturation , 0 ) ) {
489
+ saturation = Math . abs ( saturation ) ;
490
+ hue = ( hue + 180 ) % 360 ;
491
+ }
492
+
500
493
this . color = new Color ( {
501
494
spaceId : encodeSpaceForColorJs ( space ) ,
502
495
coords : [ hue , saturation , lightness ] ,
@@ -519,11 +512,9 @@ export class SassColor extends Value {
519
512
520
513
case 'lab' :
521
514
case 'oklab' : {
522
- let lightness = options . lightness ?? NaN ;
515
+ const lightness = options . lightness ?? NaN ;
523
516
const a = options . a ?? NaN ;
524
517
const b = options . b ?? NaN ;
525
- const maxLightness = space === 'lab' ? 100 : 1 ;
526
- lightness = assertClamped ( lightness , 0 , maxLightness , 'lightness' ) ;
527
518
this . color = new Color ( {
528
519
spaceId : encodeSpaceForColorJs ( space ) ,
529
520
coords : [ lightness , a , b ] ,
@@ -534,11 +525,14 @@ export class SassColor extends Value {
534
525
535
526
case 'lch' :
536
527
case 'oklch' : {
537
- let lightness = options . lightness ?? NaN ;
538
- const chroma = options . chroma ?? NaN ;
539
- const hue = normalizeHue ( options . hue ?? NaN ) ;
540
- const maxLightness = space === 'lch' ? 100 : 1 ;
541
- lightness = assertClamped ( lightness , 0 , maxLightness , 'lightness' ) ;
528
+ const lightness = options . lightness ?? NaN ;
529
+ let chroma = options . chroma ?? NaN ;
530
+ let hue = normalizeHue ( options . hue ?? NaN ) ;
531
+ if ( ! Number . isNaN ( chroma ) && fuzzyLessThan ( chroma , 0 ) ) {
532
+ chroma = Math . abs ( chroma ) ;
533
+ hue = ( hue + 180 ) % 360 ;
534
+ }
535
+
542
536
this . color = new Color ( {
543
537
spaceId : encodeSpaceForColorJs ( space ) ,
544
538
coords : [ lightness , chroma , hue ] ,
@@ -830,7 +824,7 @@ export class SassColor extends Value {
830
824
case color . channel0Id :
831
825
if ( color . space === 'hsl' ) return fuzzyEquals ( channels [ 1 ] , 0 ) ;
832
826
if ( color . space === 'hwb' ) {
833
- return fuzzyEquals ( channels [ 1 ] + channels [ 2 ] , 100 ) ;
827
+ return fuzzyGreaterThanOrEquals ( channels [ 1 ] + channels [ 2 ] , 100 ) ;
834
828
}
835
829
return false ;
836
830
case color . channel2Id :
@@ -1105,10 +1099,6 @@ export class SassColor extends Value {
1105
1099
if ( isNumberOrNull ( options . alpha ) && options . alpha !== null ) {
1106
1100
fuzzyAssertInRange ( options . alpha , 0 , 1 , 'alpha' ) ;
1107
1101
}
1108
- if ( isNumberOrNull ( options . lightness ) && options . lightness !== null ) {
1109
- const maxLightness = space === 'oklab' || space === 'oklch' ? 1 : 100 ;
1110
- assertClamped ( options . lightness , 0 , maxLightness , 'lightness' ) ;
1111
- }
1112
1102
1113
1103
return this . getChangedColor ( options , space , spaceSetExplicitly ) . toSpace (
1114
1104
this . space
0 commit comments