Skip to content

Commit 0ea0fef

Browse files
committed
more tests
1 parent 3cdb3d5 commit 0ea0fef

15 files changed

Lines changed: 634 additions & 54 deletions

packages/css-color-parser/dist/index.mjs

Lines changed: 1 addition & 1 deletion
Large diffs are not rendered by default.

packages/css-color-parser/src/color-data.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { ColorNotation } from './color-notation';
44
import type { TokenNumber} from '@csstools/css-tokenizer';
55
import { NumberType, TokenType } from '@csstools/css-tokenizer';
66
import { HSL_to_XYZ_D50, HSL_to_XYZ_D65, HWB_to_XYZ_D50, HWB_to_XYZ_D65, LCH_to_XYZ_D50, LCH_to_XYZ_D65, Lab_to_XYZ_D50, Lab_to_XYZ_D65, OKLCH_to_XYZ_D50, OKLCH_to_XYZ_D65, OKLab_to_XYZ_D50, OKLab_to_XYZ_D65, P3_to_XYZ_D50, P3_to_XYZ_D65, ProPhoto_RGB_to_XYZ_D50, ProPhoto_RGB_to_XYZ_D65, XYZ_D50_to_LCH, XYZ_D50_to_Lab, XYZ_D50_to_ProPhoto, XYZ_D50_to_XYZ_D50, XYZ_D50_to_XYZ_D65, XYZ_D65_to_HSL, XYZ_D65_to_HWB, XYZ_D65_to_OKLCH, XYZ_D65_to_OKLab, XYZ_D65_to_P3, XYZ_D65_to_XYZ_D50, XYZ_D65_to_XYZ_D65, XYZ_D65_to_a98_RGB, XYZ_D65_to_lin_P3, XYZ_D65_to_lin_sRGB, XYZ_D65_to_rec_2020, XYZ_D65_to_sRGB, a98_RGB_to_XYZ_D50, a98_RGB_to_XYZ_D65, lin_P3_to_XYZ_D50, lin_P3_to_XYZ_D65, lin_sRGB_to_XYZ_D50, lin_sRGB_to_XYZ_D65, rec_2020_to_XYZ_D50, rec_2020_to_XYZ_D65, sRGB_to_XYZ_D50, sRGB_to_XYZ_D65 } from '@csstools/color-helpers';
7+
import { normalize } from './functions/normalize';
78

89
/**
910
* A color data object.
@@ -413,55 +414,64 @@ function convertToDestination(colorData: ColorData, toNotation: ColorNotation):
413414
// 9. If dest-rect is not the same as dest, in other words dest is a cylindrical polar color representation, convert from dest-rect to dest, and let this be col2.
414415
colorData.channels = XYZ_D65_to_sRGB(xyzColorData.channels);
415416
colorData.channels = colorData.channels.map((x) => reducePrecisionOrNaN(x, 8)) as Color;
417+
colorData.channels = normalizeAfterColorConversion(colorData.channels);
416418
break;
417419
}
418420
case ColorNotation.sRGB: {
419421
const xyzColorData = colorData_to_XYZ_D65(colorData);
420422
colorData.colorNotation = ColorNotation.sRGB;
421423
colorData.channels = XYZ_D65_to_sRGB(xyzColorData.channels);
424+
colorData.channels = normalizeAfterColorConversion(colorData.channels);
422425
break;
423426
}
424427
case ColorNotation.Linear_sRGB: {
425428
const xyzColorData = colorData_to_XYZ_D65(colorData);
426429
colorData.colorNotation = ColorNotation.Linear_sRGB;
427430
colorData.channels = XYZ_D65_to_lin_sRGB(xyzColorData.channels);
431+
colorData.channels = normalizeAfterColorConversion(colorData.channels);
428432
break;
429433
}
430434
case ColorNotation.Display_P3: {
431435
const xyzColorData = colorData_to_XYZ_D65(colorData);
432436
colorData.colorNotation = ColorNotation.Display_P3;
433437
colorData.channels = XYZ_D65_to_P3(xyzColorData.channels);
438+
colorData.channels = normalizeAfterColorConversion(colorData.channels);
434439
break;
435440
}
436441
case ColorNotation.Linear_Display_P3: {
437442
const xyzColorData = colorData_to_XYZ_D65(colorData);
438443
colorData.colorNotation = ColorNotation.Linear_Display_P3;
439444
colorData.channels = XYZ_D65_to_lin_P3(xyzColorData.channels);
445+
colorData.channels = normalizeAfterColorConversion(colorData.channels);
440446
break;
441447
}
442448
case ColorNotation.Rec2020: {
443449
const xyzColorData = colorData_to_XYZ_D65(colorData);
444450
colorData.colorNotation = ColorNotation.Rec2020;
445451
colorData.channels = XYZ_D65_to_rec_2020(xyzColorData.channels);
452+
colorData.channels = normalizeAfterColorConversion(colorData.channels);
446453
break;
447454
}
448455
case ColorNotation.ProPhoto_RGB: {
449456
const xyzColorData = colorData_to_XYZ_D50(colorData);
450457
colorData.colorNotation = ColorNotation.ProPhoto_RGB;
451458
colorData.channels = XYZ_D50_to_ProPhoto(xyzColorData.channels);
459+
colorData.channels = normalizeAfterColorConversion(colorData.channels);
452460
break;
453461
}
454462
case ColorNotation.A98_RGB: {
455463
const xyzColorData = colorData_to_XYZ_D65(colorData);
456464
colorData.colorNotation = ColorNotation.A98_RGB;
457465
colorData.channels = XYZ_D65_to_a98_RGB(xyzColorData.channels);
466+
colorData.channels = normalizeAfterColorConversion(colorData.channels);
458467
break;
459468
}
460469
case ColorNotation.HSL: {
461470
const xyzColorData = colorData_to_XYZ_D65(colorData);
462471
colorData.colorNotation = ColorNotation.HSL;
463472
colorData.channels = XYZ_D65_to_HSL(xyzColorData.channels);
464473
colorData.channels = colorData.channels.map((x) => reducePrecisionOrNaN(x, 8)) as Color;
474+
colorData.channels = normalizeAfterColorConversion(colorData.channels, 0);
465475

466476
// https://drafts.csswg.org/css-color-4/#color-conversion
467477
// 9. This may produce missing components.
@@ -473,6 +483,7 @@ function convertToDestination(colorData: ColorData, toNotation: ColorNotation):
473483
colorData.colorNotation = ColorNotation.HWB;
474484
colorData.channels = XYZ_D65_to_HWB(xyzColorData.channels);
475485
colorData.channels = colorData.channels.map((x) => reducePrecisionOrNaN(x, 8)) as Color;
486+
colorData.channels = normalizeAfterColorConversion(colorData.channels, 0);
476487

477488
// https://drafts.csswg.org/css-color-4/#color-conversion
478489
// 9. This may produce missing components.
@@ -483,12 +494,14 @@ function convertToDestination(colorData: ColorData, toNotation: ColorNotation):
483494
const xyzColorData = colorData_to_XYZ_D50(colorData);
484495
colorData.colorNotation = ColorNotation.Lab;
485496
colorData.channels = XYZ_D50_to_Lab(xyzColorData.channels);
497+
colorData.channels = normalizeAfterColorConversion(colorData.channels);
486498
break;
487499
}
488500
case ColorNotation.LCH: {
489501
const xyzColorData = colorData_to_XYZ_D50(colorData);
490502
colorData.colorNotation = ColorNotation.LCH;
491503
colorData.channels = XYZ_D50_to_LCH(xyzColorData.channels);
504+
colorData.channels = normalizeAfterColorConversion(colorData.channels, 2);
492505

493506
// https://drafts.csswg.org/css-color-4/#color-conversion
494507
// 9. This may produce missing components.
@@ -499,6 +512,7 @@ function convertToDestination(colorData: ColorData, toNotation: ColorNotation):
499512
const xyzColorData = colorData_to_XYZ_D65(colorData);
500513
colorData.colorNotation = ColorNotation.OKLCH;
501514
colorData.channels = XYZ_D65_to_OKLCH(xyzColorData.channels);
515+
colorData.channels = normalizeAfterColorConversion(colorData.channels, 2);
502516

503517
// https://drafts.csswg.org/css-color-4/#color-conversion
504518
// 9. This may produce missing components.
@@ -509,18 +523,21 @@ function convertToDestination(colorData: ColorData, toNotation: ColorNotation):
509523
const xyzColorData = colorData_to_XYZ_D65(colorData);
510524
colorData.colorNotation = ColorNotation.OKLab;
511525
colorData.channels = XYZ_D65_to_OKLab(xyzColorData.channels);
526+
colorData.channels = normalizeAfterColorConversion(colorData.channels);
512527
break;
513528
}
514529
case ColorNotation.XYZ_D50: {
515530
const xyzColorData = colorData_to_XYZ_D50(colorData);
516531
colorData.colorNotation = ColorNotation.XYZ_D50;
517532
colorData.channels = XYZ_D50_to_XYZ_D50(xyzColorData.channels);
533+
colorData.channels = normalizeAfterColorConversion(colorData.channels);
518534
break;
519535
}
520536
case ColorNotation.XYZ_D65: {
521537
const xyzColorData = colorData_to_XYZ_D65(colorData);
522538
colorData.colorNotation = ColorNotation.XYZ_D65;
523539
colorData.channels = XYZ_D65_to_XYZ_D65(xyzColorData.channels);
540+
colorData.channels = normalizeAfterColorConversion(colorData.channels);
524541
break;
525542
}
526543
default:
@@ -915,6 +932,21 @@ export function noneToZeroInRelativeColorDataChannels(x: Map<string, TokenNumber
915932
return globals;
916933
}
917934

935+
function normalizeAfterColorConversion(a: Color, hueIndex: number = -1): Color {
936+
a = convertNaNToZero(a);
937+
a = [
938+
hueIndex === 0 ? a[0] : normalize(a[0], 1, -2_147_483_647, 2_147_483_647),
939+
hueIndex === 1 ? a[1] : normalize(a[1], 1, -2_147_483_647, 2_147_483_647),
940+
hueIndex === 2 ? a[2] : normalize(a[2], 1, -2_147_483_647, 2_147_483_647),
941+
];
942+
943+
if (!Number.isNaN(a[hueIndex]) && !Number.isFinite(a[hueIndex])) {
944+
a[hueIndex] = 0;
945+
}
946+
947+
return a;
948+
}
949+
918950
function dummyNumberToken(x: number): TokenNumber {
919951
if (Number.isNaN(x)) {
920952
return [TokenType.Number, 'none', -1, -1, { value: Number.NaN, type: NumberType.Number }];

packages/css-color-parser/src/functions/color-normalize-channel-values.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,11 @@ export function normalize_Color_ChannelValues(token: CSSToken, index: number, co
2626
colorData.syntaxFlags.add(SyntaxFlag.HasPercentageValues);
2727
}
2828

29-
let value = normalize(token[4].value, 100, -2_147_483_647, 2_147_483_647);
29+
let value;
3030
if (index === 3) {
3131
value = normalize(token[4].value, 100, 0, 1);
32+
} else {
33+
value = normalize(token[4].value, 100, -2_147_483_647, 2_147_483_647);
3234
}
3335

3436
return [
@@ -48,9 +50,11 @@ export function normalize_Color_ChannelValues(token: CSSToken, index: number, co
4850
colorData.syntaxFlags.add(SyntaxFlag.HasNumberValues);
4951
}
5052

51-
let value = normalize(token[4].value, 1, -2_147_483_647, 2_147_483_647);
53+
let value;
5254
if (index === 3) {
5355
value = normalize(token[4].value, 1, 0, 1);
56+
} else {
57+
value = normalize(token[4].value, 1, -2_147_483_647, 2_147_483_647);
5458
}
5559

5660
return [

packages/css-color-parser/src/functions/hsl-normalize-channel-values.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,11 @@ export function normalize_legacy_HSL_ChannelValues(token: CSSToken, index: numbe
2727
colorData.syntaxFlags.add(SyntaxFlag.HasPercentageValues);
2828
}
2929

30-
let value = normalize(token[4].value, 1, 0, 100);
30+
let value;
3131
if (index === 3) {
3232
value = normalize(token[4].value, 100, 0, 1);
33+
} else {
34+
value = normalize(token[4].value, 1, 0, 100);
3335
}
3436

3537
return [
@@ -49,9 +51,11 @@ export function normalize_legacy_HSL_ChannelValues(token: CSSToken, index: numbe
4951
return false;
5052
}
5153

52-
let value = normalize(token[4].value, 1, 0, 100);
54+
let value;
5355
if (index === 3) {
5456
value = normalize(token[4].value, 1, 0, 1);
57+
} else {
58+
value = normalize(token[4].value, 1, 0, 100);
5559
}
5660

5761
return [
@@ -105,11 +109,13 @@ export function normalize_modern_HSL_ChannelValues(token: CSSToken, index: numbe
105109
colorData.syntaxFlags.add(SyntaxFlag.HasPercentageValues);
106110
}
107111

108-
let value = token[4].value;
112+
let value;
109113
if (index === 3) {
110114
value = normalize(token[4].value, 100, 0, 1);
111115
} else if (index === 1) {
112116
value = normalize(token[4].value, 1, 0, 2_147_483_647);
117+
} else {
118+
value = normalize(token[4].value, 1, -2_147_483_647, 2_147_483_647);
113119
}
114120

115121
return [
@@ -129,11 +135,13 @@ export function normalize_modern_HSL_ChannelValues(token: CSSToken, index: numbe
129135
colorData.syntaxFlags.add(SyntaxFlag.HasNumberValues);
130136
}
131137

132-
let value = token[4].value;
138+
let value;
133139
if (index === 3) {
134140
value = normalize(token[4].value, 1, 0, 1);
135141
} else if (index === 1) {
136142
value = normalize(token[4].value, 1, 0, 2_147_483_647);
143+
} else {
144+
value = normalize(token[4].value, 1, -2_147_483_647, 2_147_483_647);
137145
}
138146

139147
return [

packages/css-color-parser/src/functions/hwb-normalize-channel-values.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,11 @@ export function normalize_HWB_ChannelValues(token: CSSToken, index: number, colo
4242
colorData.syntaxFlags.add(SyntaxFlag.HasPercentageValues);
4343
}
4444

45-
let value = token[4].value;
45+
let value;
4646
if (index === 3) {
4747
value = normalize(token[4].value, 100, 0, 1);
48+
} else {
49+
value = normalize(token[4].value, 1, -2_147_483_647, 2_147_483_647);
4850
}
4951

5052
return [
@@ -64,9 +66,11 @@ export function normalize_HWB_ChannelValues(token: CSSToken, index: number, colo
6466
colorData.syntaxFlags.add(SyntaxFlag.HasNumberValues);
6567
}
6668

67-
let value = token[4].value;
69+
let value;
6870
if (index === 3) {
6971
value = normalize(token[4].value, 1, 0, 1);
72+
} else {
73+
value = normalize(token[4].value, 1, -2_147_483_647, 2_147_483_647);
7074
}
7175

7276
return [

packages/css-color-parser/src/functions/lab-normalize-channel-values.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ export function normalize_Lab_ChannelValues(token: CSSToken, index: number, colo
2626
colorData.syntaxFlags.add(SyntaxFlag.HasPercentageValues);
2727
}
2828

29-
let value = normalize(token[4].value, 1, 0, 100);
29+
let value;
3030
if (index === 1 || index === 2) {
3131
value = normalize(token[4].value, 0.8, -2_147_483_647, 2_147_483_647);
3232
} else if (index === 3) {
3333
value = normalize(token[4].value, 100, 0, 1);
34+
} else {
35+
value = normalize(token[4].value, 1, 0, 100);
3436
}
3537

3638
return [
@@ -50,11 +52,13 @@ export function normalize_Lab_ChannelValues(token: CSSToken, index: number, colo
5052
colorData.syntaxFlags.add(SyntaxFlag.HasNumberValues);
5153
}
5254

53-
let value = normalize(token[4].value, 1, 0, 100);
55+
let value;
5456
if (index === 1 || index === 2) {
5557
value = normalize(token[4].value, 1, -2_147_483_647, 2_147_483_647);
5658
} else if (index === 3) {
5759
value = normalize(token[4].value, 1, 0, 1);
60+
} else {
61+
value = normalize(token[4].value, 1, 0, 100);
5862
}
5963

6064
return [

packages/css-color-parser/src/functions/lch-normalize-channel-values.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@ export function normalize_LCH_ChannelValues(token: CSSToken, index: number, colo
4040
colorData.syntaxFlags.add(SyntaxFlag.HasPercentageValues);
4141
}
4242

43-
let value = normalize(token[4].value, 1, 0, 100);
43+
let value;
4444
if (index === 1) {
4545
value = normalize(token[4].value, (100 / 150), 0, 2_147_483_647);
4646
} else if (index === 3) {
4747
value = normalize(token[4].value, 100, 0, 1);
48+
} else {
49+
value = normalize(token[4].value, 1, 0, 100);
4850
}
4951

5052
return [
@@ -64,11 +66,13 @@ export function normalize_LCH_ChannelValues(token: CSSToken, index: number, colo
6466
colorData.syntaxFlags.add(SyntaxFlag.HasNumberValues);
6567
}
6668

67-
let value = normalize(token[4].value, 1, 0, 100);
69+
let value;
6870
if (index === 1) {
6971
value = normalize(token[4].value, 1, 0, 2_147_483_647);
7072
} else if (index === 3) {
7173
value = normalize(token[4].value, 1, 0, 1);
74+
} else {
75+
value = normalize(token[4].value, 1, 0, 100);
7276
}
7377

7478
return [

packages/css-color-parser/src/functions/oklab-normalize-channel-values.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ export function normalize_OKLab_ChannelValues(token: CSSToken, index: number, co
2626
colorData.syntaxFlags.add(SyntaxFlag.HasPercentageValues);
2727
}
2828

29-
let value = normalize(token[4].value, 100, 0, 1);
29+
let value;
3030
if (index === 1 || index === 2) {
3131
value = normalize(token[4].value, 250, -2_147_483_647, 2_147_483_647);
3232
} else if (index === 3) {
3333
value = normalize(token[4].value, 100, 0, 1);
34+
} else {
35+
value = normalize(token[4].value, 100, 0, 1);
3436
}
3537

3638
return [
@@ -50,11 +52,13 @@ export function normalize_OKLab_ChannelValues(token: CSSToken, index: number, co
5052
colorData.syntaxFlags.add(SyntaxFlag.HasNumberValues);
5153
}
5254

53-
let value = normalize(token[4].value, 1, 0, 1);
55+
let value;
5456
if (index === 1 || index === 2) {
5557
value = normalize(token[4].value, 1, -2_147_483_647, 2_147_483_647);
5658
} else if (index === 3) {
5759
value = normalize(token[4].value, 1, 0, 1);
60+
} else {
61+
value = normalize(token[4].value, 1, 0, 1);
5862
}
5963

6064
return [

packages/css-color-parser/src/functions/oklch-normalize-channel-values.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,13 @@ export function normalize_OKLCH_ChannelValues(token: CSSToken, index: number, co
4040
colorData.syntaxFlags.add(SyntaxFlag.HasPercentageValues);
4141
}
4242

43-
let value = normalize(token[4].value, 100, 0, 1);
43+
let value;
4444
if (index === 1) {
4545
value = normalize(token[4].value, 250, 0, 2_147_483_647);
4646
} else if (index === 3) {
4747
value = normalize(token[4].value, 100, 0, 1);
48+
} else {
49+
value = normalize(token[4].value, 100, 0, 1);
4850
}
4951

5052
return [
@@ -64,11 +66,13 @@ export function normalize_OKLCH_ChannelValues(token: CSSToken, index: number, co
6466
colorData.syntaxFlags.add(SyntaxFlag.HasNumberValues);
6567
}
6668

67-
let value = normalize(token[4].value, 1, 0, 1);
69+
let value;
6870
if (index === 1) {
6971
value = normalize(token[4].value, 1, 0, 2_147_483_647);
7072
} else if (index === 3) {
7173
value = normalize(token[4].value, 1, 0, 1);
74+
} else {
75+
value = normalize(token[4].value, 1, 0, 1);
7276
}
7377

7478
return [

0 commit comments

Comments
 (0)