@@ -123,7 +123,7 @@ class SassColor extends Value {
123
123
case ColorSpace .oklab:
124
124
case ColorSpace .lch:
125
125
case ColorSpace .oklch:
126
- return fuzzyEquals (channel0, 0 );
126
+ return fuzzyEquals (channel0, 0 ) || fuzzyEquals (channel0, 100 ) ;
127
127
128
128
default :
129
129
return false ;
@@ -156,11 +156,13 @@ class SassColor extends Value {
156
156
switch (space) {
157
157
case ColorSpace .lab:
158
158
case ColorSpace .oklab:
159
- return fuzzyEquals (channel0, 0 );
159
+ return fuzzyEquals (channel0, 0 ) || fuzzyEquals (channel0, 100 ) ;
160
160
161
161
case ColorSpace .lch:
162
162
case ColorSpace .oklch:
163
- return fuzzyEquals (channel0, 0 ) || fuzzyEquals (channel1, 0 );
163
+ return fuzzyEquals (channel0, 0 ) ||
164
+ fuzzyEquals (channel0, 100 ) ||
165
+ fuzzyEquals (channel1, 0 );
164
166
165
167
default :
166
168
return false ;
@@ -285,7 +287,7 @@ class SassColor extends Value {
285
287
[num ? alpha]) =>
286
288
SassColor .forSpaceInternal (
287
289
ColorSpace .hsl,
288
- hue? .toDouble (),
290
+ _normalizeHue ( hue? .toDouble () ),
289
291
saturation.andThen ((saturation) =>
290
292
fuzzyAssertRange (saturation.toDouble (), 0 , 100 , "saturation" )),
291
293
lightness.andThen ((lightness) =>
@@ -299,7 +301,7 @@ class SassColor extends Value {
299
301
[num ? alpha]) =>
300
302
SassColor .forSpaceInternal (
301
303
ColorSpace .hwb,
302
- hue? .toDouble (),
304
+ _normalizeHue ( hue? .toDouble () ),
303
305
whiteness.andThen ((whiteness) =>
304
306
fuzzyAssertRange (whiteness.toDouble (), 0 , 100 , "whiteness" )),
305
307
blackness.andThen ((blackness) =>
@@ -367,29 +369,52 @@ class SassColor extends Value {
367
369
/// Throws a [RangeError] if [alpha] isn't between `0` and `1` .
368
370
factory SassColor .lab (double ? lightness, double ? a, double ? b,
369
371
[double ? alpha]) =>
370
- SassColor .forSpaceInternal (ColorSpace .lab, lightness, a, b, alpha);
372
+ SassColor .forSpaceInternal (
373
+ ColorSpace .lab,
374
+ lightness.andThen (
375
+ (lightness) => fuzzyAssertRange (lightness, 0 , 100 , "lightness" )),
376
+ a,
377
+ b,
378
+ alpha);
371
379
372
380
/// Creates a color in [ColorSpace.lch] .
373
381
///
374
382
/// Throws a [RangeError] if [alpha] isn't between `0` and `1` .
375
383
factory SassColor .lch (double ? lightness, double ? chroma, double ? hue,
376
384
[double ? alpha]) =>
377
- SassColor .forSpaceInternal (ColorSpace .lch, lightness, chroma, hue, alpha);
385
+ SassColor .forSpaceInternal (
386
+ ColorSpace .lch,
387
+ lightness.andThen (
388
+ (lightness) => fuzzyAssertRange (lightness, 0 , 100 , "lightness" )),
389
+ chroma,
390
+ _normalizeHue (hue),
391
+ alpha);
378
392
379
393
/// Creates a color in [ColorSpace.oklab] .
380
394
///
381
395
/// Throws a [RangeError] if [alpha] isn't between `0` and `1` .
382
396
factory SassColor .oklab (double ? lightness, double ? a, double ? b,
383
397
[double ? alpha]) =>
384
- SassColor .forSpaceInternal (ColorSpace .oklab, lightness, a, b, alpha);
398
+ SassColor .forSpaceInternal (
399
+ ColorSpace .oklab,
400
+ lightness.andThen (
401
+ (lightness) => fuzzyAssertRange (lightness, 0 , 100 , "lightness" )),
402
+ a,
403
+ b,
404
+ alpha);
385
405
386
406
/// Creates a color in [ColorSpace.oklch] .
387
407
///
388
408
/// Throws a [RangeError] if [alpha] isn't between `0` and `1` .
389
409
factory SassColor .oklch (double ? lightness, double ? chroma, double ? hue,
390
410
[double ? alpha]) =>
391
411
SassColor .forSpaceInternal (
392
- ColorSpace .oklch, lightness, chroma, hue, alpha);
412
+ ColorSpace .oklch,
413
+ lightness.andThen (
414
+ (lightness) => fuzzyAssertRange (lightness, 0 , 100 , "lightness" )),
415
+ chroma,
416
+ _normalizeHue (hue),
417
+ alpha);
393
418
394
419
/// Creates a color in the color space named [space] .
395
420
///
@@ -401,14 +426,17 @@ class SassColor extends Value {
401
426
throw RangeError .value (channels.length, "channels.length" ,
402
427
'must be exactly ${space .channels .length } for color space "$space "' );
403
428
} else {
404
- var clampChannels = space == ColorSpace .hsl || space == ColorSpace .hwb;
429
+ var clampChannel0 = space.channels[0 ].name == "lightness" ;
430
+ var clampChannel12 = space == ColorSpace .hsl || space == ColorSpace .hwb;
405
431
return SassColor .forSpaceInternal (
406
432
space,
407
- channels[0 ],
408
- clampChannels
433
+ clampChannel0
434
+ ? channels[0 ].andThen ((value) => fuzzyClamp (value, 0 , 100 ))
435
+ : channels[0 ],
436
+ clampChannel12
409
437
? channels[1 ].andThen ((value) => fuzzyClamp (value, 0 , 100 ))
410
438
: channels[1 ],
411
- clampChannels
439
+ clampChannel12
412
440
? channels[2 ].andThen ((value) => fuzzyClamp (value, 0 , 100 ))
413
441
: channels[2 ],
414
442
alpha);
@@ -432,6 +460,12 @@ class SassColor extends Value {
432
460
"[BUG] Tried to create "
433
461
"$_space (${channel0OrNull ?? 'none' }, ${channel1OrNull ?? 'none' }, "
434
462
"${channel2OrNull ?? 'none' })" );
463
+ assert (
464
+ space.channels[0 ].name != "lightness" ||
465
+ fuzzyCheckRange (channel0, 0 , 100 ) != null ,
466
+ "[BUG] Tried to create "
467
+ "$_space (${channel0OrNull ?? 'none' }, ${channel1OrNull ?? 'none' }, "
468
+ "${channel2OrNull ?? 'none' })" );
435
469
assert (space != ColorSpace .lms);
436
470
437
471
_checkChannel (channel0OrNull, space.channels[0 ].name);
@@ -449,6 +483,12 @@ class SassColor extends Value {
449
483
}
450
484
}
451
485
486
+ /// If [hue] isn't null, normalizes it to the range `[0, 360)` .
487
+ static double ? _normalizeHue (double ? hue) {
488
+ if (hue == null ) return hue;
489
+ return (hue % 360 + 360 ) % 360 ;
490
+ }
491
+
452
492
/// @nodoc
453
493
@internal
454
494
T accept <T >(ValueVisitor <T > visitor) => visitor.visitColor (this );
@@ -894,11 +934,6 @@ class SassColor extends Value {
894
934
double _interpolateHues (
895
935
double hue1, double hue2, HueInterpolationMethod method, double weight) {
896
936
// Algorithms from https://www.w3.org/TR/css-color-4/#hue-interpolation
897
- if (method != HueInterpolationMethod .specified) {
898
- hue1 = (hue1 % 360 + 360 ) % 360 ;
899
- hue2 = (hue2 % 360 + 360 ) % 360 ;
900
- }
901
-
902
937
switch (method) {
903
938
case HueInterpolationMethod .shorter:
904
939
var difference = hue2 - hue1;
@@ -925,10 +960,6 @@ class SassColor extends Value {
925
960
case HueInterpolationMethod .decreasing:
926
961
if (hue1 < hue2) hue1 += 360 ;
927
962
break ;
928
-
929
- case HueInterpolationMethod .specified:
930
- // Use the hues as-is.
931
- break ;
932
963
}
933
964
934
965
return hue1 * weight + hue2 * (1 - weight);
0 commit comments