Skip to content

Commit

Permalink
cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
romainmenke committed Apr 25, 2024
1 parent 2766478 commit 38b7672
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 26 deletions.
26 changes: 26 additions & 0 deletions packages/css-calc/test/basic/test.mjs
Expand Up @@ -87,6 +87,32 @@ assert.strictEqual(
);
}

{
const globals = new Map([
['none', [TokenType.Number, '0', -1, -1, { value: 0, type: NumberType.Number }]],
]);

assert.strictEqual(
calc('calc(none + 0.2)', { globals: globals }),
'0.2',
);

assert.strictEqual(
calc('calc(none + 0.2)'),
'calc(none + 0.2)',
);

assert.strictEqual(
calc('clamp(1, 2, none)', { globals: globals }),
'1',
);

assert.strictEqual(
calc('clamp(1, 2, none)'),
'2',
);
}

assert.strictEqual(
calc('clamp(10, 20, 15)'),
(15).toString(),
Expand Down
2 changes: 1 addition & 1 deletion packages/css-color-parser/dist/index.cjs

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion packages/css-color-parser/dist/index.mjs

Large diffs are not rendered by default.

38 changes: 23 additions & 15 deletions packages/css-color-parser/src/color-data.ts
Expand Up @@ -36,6 +36,14 @@ export interface ColorData {
syntaxFlags: Set<SyntaxFlag>
}

export function convertNaNToZero(x: Color): Color {
return [
Number.isNaN(x[0]) ? 0 : x[0],
Number.isNaN(x[1]) ? 0 : x[1],
Number.isNaN(x[2]) ? 0 : x[2],
];
}

export enum SyntaxFlag {
/** Is a color keyword, e.g. `transparent`, `currentColor`, ... */
ColorKeyword = 'color-keyword',
Expand Down Expand Up @@ -79,85 +87,85 @@ export function colorData_to_XYZ_D50(colorData: ColorData): ColorData {
return {
...colorData,
colorNotation: ColorNotation.XYZ_D50,
channels: sRGB_to_XYZ_D50(colorData.channels.map((x) => Number.isNaN(x) ? 0 : x) as Color),
channels: sRGB_to_XYZ_D50(convertNaNToZero(colorData.channels)),
};
case ColorNotation.Linear_sRGB:
return {
...colorData,
colorNotation: ColorNotation.XYZ_D50,
channels: lin_sRGB_to_XYZ_D50(colorData.channels.map((x) => Number.isNaN(x) ? 0 : x) as Color),
channels: lin_sRGB_to_XYZ_D50(convertNaNToZero(colorData.channels)),
};
case ColorNotation.Display_P3:
return {
...colorData,
colorNotation: ColorNotation.XYZ_D50,
channels: P3_to_XYZ_D50(colorData.channels.map((x) => Number.isNaN(x) ? 0 : x) as Color),
channels: P3_to_XYZ_D50(convertNaNToZero(colorData.channels)),
};
case ColorNotation.Rec2020:
return {
...colorData,
colorNotation: ColorNotation.XYZ_D50,
channels: rec_2020_to_XYZ_D50(colorData.channels.map((x) => Number.isNaN(x) ? 0 : x) as Color),
channels: rec_2020_to_XYZ_D50(convertNaNToZero(colorData.channels)),
};
case ColorNotation.A98_RGB:
return {
...colorData,
colorNotation: ColorNotation.XYZ_D50,
channels: a98_RGB_to_XYZ_D50(colorData.channels.map((x) => Number.isNaN(x) ? 0 : x) as Color),
channels: a98_RGB_to_XYZ_D50(convertNaNToZero(colorData.channels)),
};
case ColorNotation.ProPhoto_RGB:
return {
...colorData,
colorNotation: ColorNotation.XYZ_D50,
channels: ProPhoto_RGB_to_XYZ_D50(colorData.channels.map((x) => Number.isNaN(x) ? 0 : x) as Color),
channels: ProPhoto_RGB_to_XYZ_D50(convertNaNToZero(colorData.channels)),
};
case ColorNotation.HSL:
return {
...colorData,
colorNotation: ColorNotation.XYZ_D50,
channels: HSL_to_XYZ_D50(colorData.channels.map((x) => Number.isNaN(x) ? 0 : x) as Color),
channels: HSL_to_XYZ_D50(convertNaNToZero(colorData.channels)),
};
case ColorNotation.HWB:
return {
...colorData,
colorNotation: ColorNotation.XYZ_D50,
channels: HWB_to_XYZ_D50(colorData.channels.map((x) => Number.isNaN(x) ? 0 : x) as Color),
channels: HWB_to_XYZ_D50(convertNaNToZero(colorData.channels)),
};
case ColorNotation.Lab:
return {
...colorData,
colorNotation: ColorNotation.XYZ_D50,
channels: Lab_to_XYZ_D50(colorData.channels.map((x) => Number.isNaN(x) ? 0 : x) as Color),
channels: Lab_to_XYZ_D50(convertNaNToZero(colorData.channels)),
};
case ColorNotation.OKLab:
return {
...colorData,
colorNotation: ColorNotation.XYZ_D50,
channels: OKLab_to_XYZ_D50(colorData.channels.map((x) => Number.isNaN(x) ? 0 : x) as Color),
channels: OKLab_to_XYZ_D50(convertNaNToZero(colorData.channels)),
};
case ColorNotation.LCH:
return {
...colorData,
colorNotation: ColorNotation.XYZ_D50,
channels: LCH_to_XYZ_D50(colorData.channels.map((x) => Number.isNaN(x) ? 0 : x) as Color),
channels: LCH_to_XYZ_D50(convertNaNToZero(colorData.channels)),
};
case ColorNotation.OKLCH:
return {
...colorData,
colorNotation: ColorNotation.XYZ_D50,
channels: OKLCH_to_XYZ_D50(colorData.channels.map((x) => Number.isNaN(x) ? 0 : x) as Color),
channels: OKLCH_to_XYZ_D50(convertNaNToZero(colorData.channels)),
};
case ColorNotation.XYZ_D50:
return {
...colorData,
colorNotation: ColorNotation.XYZ_D50,
channels: XYZ_D50_to_XYZ_D50(colorData.channels.map((x) => Number.isNaN(x) ? 0 : x) as Color),
channels: XYZ_D50_to_XYZ_D50(convertNaNToZero(colorData.channels)),
};
case ColorNotation.XYZ_D65:
return {
...colorData,
colorNotation: ColorNotation.XYZ_D50,
channels: XYZ_D65_to_XYZ_D50(colorData.channels.map((x) => Number.isNaN(x) ? 0 : x) as Color),
channels: XYZ_D65_to_XYZ_D50(convertNaNToZero(colorData.channels)),
};
default:
throw new Error('Unsupported color notation');
Expand Down Expand Up @@ -252,7 +260,7 @@ export function colorDataTo(colorData: ColorData, toNotation: ColorNotation): Co
throw new Error('Unsupported color notation');
}
} else {
outputColorData.channels = colorData.channels.map((x) => Number.isNaN(x) ? 0 : x) as Color;
outputColorData.channels = convertNaNToZero(colorData.channels);
}

// 2. Carry forward missing components
Expand Down
5 changes: 2 additions & 3 deletions packages/css-color-parser/src/functions/contrast-color.ts
Expand Up @@ -2,9 +2,8 @@ import type { ColorData } from '../color-data';
import type { ColorParser } from '../color-parser';
import type { FunctionNode } from '@csstools/css-parser-algorithms';
import { ColorNotation } from '../color-notation';
import { SyntaxFlag, colorData_to_XYZ_D50 } from '../color-data';
import { SyntaxFlag, colorData_to_XYZ_D50, convertNaNToZero } from '../color-data';
import { isCommentNode, isTokenNode, isWhitespaceNode } from '@csstools/css-parser-algorithms';
import { Color } from '@csstools/color-helpers';
import { XYZ_D50_to_sRGB_Gamut } from '../gamut-mapping/srgb';
import { TokenType } from '@csstools/css-tokenizer';
import { toLowerCaseAZ } from '../util/to-lower-case-a-z';
Expand Down Expand Up @@ -46,7 +45,7 @@ export function contrastColor(colorMixNode: FunctionNode, colorParser: ColorPars
}

// Treat missing as zero.
backgroundColorData.channels = backgroundColorData.channels.map((x) => Number.isNaN(x) ? 0 : x) as Color;
backgroundColorData.channels = convertNaNToZero(backgroundColorData.channels);
backgroundColorData.channels = XYZ_D50_to_sRGB_Gamut(colorData_to_XYZ_D50(backgroundColorData).channels);
backgroundColorData.colorNotation = ColorNotation.sRGB;

Expand Down
6 changes: 3 additions & 3 deletions packages/css-color-parser/src/gamut-mapping/p3.ts
Expand Up @@ -7,14 +7,14 @@ export function XYZ_D50_to_P3_Gamut(color: Color): Color {
return clip(p3);
}

let oklch = color.slice() as Color;
let oklch: Color = color;
oklch = XYZ_D50_to_OKLCH(oklch);
if (oklch[0] < 0.000001) {
oklch = [0, 0, 0] as Color;
oklch = [0, 0, 0];
}

if (oklch[0] > 0.999999) {
oklch = [1, 0, 0] as Color;
oklch = [1, 0, 0];
}

return gam_P3(mapGamutRayTrace(oklch, oklch_to_lin_p3, lin_p3_to_oklch));
Expand Down
6 changes: 3 additions & 3 deletions packages/css-color-parser/src/gamut-mapping/srgb.ts
Expand Up @@ -7,14 +7,14 @@ export function XYZ_D50_to_sRGB_Gamut(color: Color): Color {
return clip(srgb);
}

let oklch = color.slice() as Color;
let oklch: Color = color;
oklch = XYZ_D50_to_OKLCH(oklch);
if (oklch[0] < 0.000001) {
oklch = [0, 0, 0] as Color;
oklch = [0, 0, 0];
}

if (oklch[0] > 0.999999) {
oklch = [1, 0, 0] as Color;
oklch = [1, 0, 0];
}

return gam_sRGB(mapGamutRayTrace(oklch, oklch_to_lin_srgb, lin_srgb_to_oklch));
Expand Down

0 comments on commit 38b7672

Please sign in to comment.