Skip to content

Commit fef63f3

Browse files
authored
[Color 4] Update behavior to match latest specs (#278)
1 parent 1c136de commit fef63f3

File tree

1 file changed

+20
-30
lines changed

1 file changed

+20
-30
lines changed

lib/src/value/color.ts

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@ import {valueError} from '../utils';
77
import {
88
fuzzyAssertInRange,
99
fuzzyEquals,
10+
fuzzyGreaterThanOrEquals,
1011
fuzzyHashCode,
12+
fuzzyLessThan,
1113
fuzzyRound,
1214
positiveMod,
1315
} from './utils';
@@ -123,19 +125,6 @@ function NaNtoZero(val: number): number {
123125
return Number.isNaN(val) ? 0 : val;
124126
}
125127

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-
139128
/** Convert from sRGB (0-1) to RGB (0-255) units. */
140129
function coordToRgb(val: number): number {
141130
return val * 255;
@@ -493,10 +482,14 @@ export class SassColor extends Value {
493482
break;
494483

495484
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+
500493
this.color = new Color({
501494
spaceId: encodeSpaceForColorJs(space),
502495
coords: [hue, saturation, lightness],
@@ -519,11 +512,9 @@ export class SassColor extends Value {
519512

520513
case 'lab':
521514
case 'oklab': {
522-
let lightness = options.lightness ?? NaN;
515+
const lightness = options.lightness ?? NaN;
523516
const a = options.a ?? NaN;
524517
const b = options.b ?? NaN;
525-
const maxLightness = space === 'lab' ? 100 : 1;
526-
lightness = assertClamped(lightness, 0, maxLightness, 'lightness');
527518
this.color = new Color({
528519
spaceId: encodeSpaceForColorJs(space),
529520
coords: [lightness, a, b],
@@ -534,11 +525,14 @@ export class SassColor extends Value {
534525

535526
case 'lch':
536527
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+
542536
this.color = new Color({
543537
spaceId: encodeSpaceForColorJs(space),
544538
coords: [lightness, chroma, hue],
@@ -830,7 +824,7 @@ export class SassColor extends Value {
830824
case color.channel0Id:
831825
if (color.space === 'hsl') return fuzzyEquals(channels[1], 0);
832826
if (color.space === 'hwb') {
833-
return fuzzyEquals(channels[1] + channels[2], 100);
827+
return fuzzyGreaterThanOrEquals(channels[1] + channels[2], 100);
834828
}
835829
return false;
836830
case color.channel2Id:
@@ -1105,10 +1099,6 @@ export class SassColor extends Value {
11051099
if (isNumberOrNull(options.alpha) && options.alpha !== null) {
11061100
fuzzyAssertInRange(options.alpha, 0, 1, 'alpha');
11071101
}
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-
}
11121102

11131103
return this.getChangedColor(options, space, spaceSetExplicitly).toSpace(
11141104
this.space

0 commit comments

Comments
 (0)