Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/css-color-parser/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changes to CSS Color Parser

### Unreleased (minor)

- Add support for analogous set in color interpolations

### 4.0.2

_February 21, 2026_
Expand Down
2 changes: 1 addition & 1 deletion packages/css-color-parser/dist/index.mjs

Large diffs are not rendered by default.

53 changes: 37 additions & 16 deletions packages/css-color-parser/src/color-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -281,79 +281,90 @@ export function colorDataTo(colorData: ColorData, toNotation: ColorNotation): Co

// 2. Carry forward missing components
if (toNotation === colorData.colorNotation) {
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [0, 1, 2], outputColorData.channels, [0, 1, 2]);
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [0, 1, 2], [], outputColorData.channels, [0, 1, 2], []);
} else if (
predefinedRGB_or_XYZ_Spaces.has(toNotation) &&
predefinedRGB_or_XYZ_Spaces.has(colorData.colorNotation)
) {
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [0, 1, 2], outputColorData.channels, [0, 1, 2]);
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [0, 1, 2], [], outputColorData.channels, [0, 1, 2], []);
} else {
switch (toNotation) {
case ColorNotation.HSL:
switch (colorData.colorNotation) {
case ColorNotation.HWB:
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [0], outputColorData.channels, [0]);
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [0], [1, 2], outputColorData.channels, [0], [1, 2]);
break;
case ColorNotation.Lab:
case ColorNotation.OKLab:
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [2], outputColorData.channels, [0]);
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [2], [0, 1], outputColorData.channels, [0], [1, 2]);
break;
case ColorNotation.LCH:
case ColorNotation.OKLCH:
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [0, 1, 2], outputColorData.channels, [2, 1, 0]);
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [0, 1, 2], [], outputColorData.channels, [2, 1, 0], []);
break;
default:
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [], [], outputColorData.channels, [], []);
}

break;
case ColorNotation.HWB:
switch (colorData.colorNotation) {
case ColorNotation.HSL:
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [0], outputColorData.channels, [0]);
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [0], [1, 2], outputColorData.channels, [0], [1, 2]);
break;
case ColorNotation.LCH:
case ColorNotation.OKLCH:
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [0], outputColorData.channels, [2]);
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [0], [1, 2], outputColorData.channels, [2], [0, 1]);
break;
default:
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [], [], outputColorData.channels, [], []);
}

break;
case ColorNotation.Lab:
case ColorNotation.OKLab:
switch (colorData.colorNotation) {
case ColorNotation.HSL:
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [0], outputColorData.channels, [2]);
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [0], [1, 2], outputColorData.channels, [2], [0, 1]);
break;
case ColorNotation.Lab:
case ColorNotation.OKLab:
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [0, 1, 2], outputColorData.channels, [0, 1, 2]);
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [0, 1, 2], [], outputColorData.channels, [0, 1, 2], []);
break;
case ColorNotation.LCH:
case ColorNotation.OKLCH:
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [0], outputColorData.channels, [0]);
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [0], [1, 2], outputColorData.channels, [0], [1, 2]);
break;
default:
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [], [], outputColorData.channels, [], []);
}

break;
case ColorNotation.LCH:
case ColorNotation.OKLCH:
switch (colorData.colorNotation) {
case ColorNotation.HSL:
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [0, 1, 2], outputColorData.channels, [2, 1, 0]);
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [0, 1, 2], [], outputColorData.channels, [2, 1, 0], []);
break;
case ColorNotation.HWB:
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [0], outputColorData.channels, [2]);
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [0], [1, 2], outputColorData.channels, [2], [0, 1]);
break;
case ColorNotation.Lab:
case ColorNotation.OKLab:
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [0], outputColorData.channels, [0]);
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [0], [1, 2], outputColorData.channels, [0], [1, 2]);
break;
case ColorNotation.LCH:
case ColorNotation.OKLCH:
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [0, 1, 2], outputColorData.channels, [0, 1, 2]);
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [0, 1, 2], [], outputColorData.channels, [0, 1, 2], []);
break;
default:
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [], [], outputColorData.channels, [], []);
}

break;

default:
outputColorData.channels = carryForwardMissingComponents(colorData.channels, [], [], outputColorData.channels, [], []);
}
}

Expand Down Expand Up @@ -452,15 +463,25 @@ export function convertPowerlessComponentsToZeroValuesForDisplay(a: Color, color
return out;
}

function carryForwardMissingComponents(a: Color, aIndices: Array<number>, b: Color, bIndices: Array<number>): Color {
function carryForwardMissingComponents(a: Color, aIndices: Array<number>, aSet: Array<number>, b: Color, bIndices: Array<number>, bSet: Array<number>): Color {
if (aIndices.length < 3 && a.every(Number.isNaN)) {
return [Number.NaN, Number.NaN, Number.NaN];
}

const output: Color = [...b];

for (const i of aIndices) {
for (let i = 0; i < aIndices.length; i++) {
if (Number.isNaN(a[aIndices[i]])) {
output[bIndices[i]] = Number.NaN;
}
}

if (aSet.length && aSet.every((i) => Number.isNaN(a[i]))) {
for (let i = 0; i < bSet.length; i++) {
output[bSet[i]] = Number.NaN;
}
}

return output;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@ const tests = [
['color-mix(in lch, oklch(75% 0% 60deg), oklch(75% 50% 0deg))', 'oklch(0.74979 0.09824 0.10588)'],
['color-mix(in oklch, oklch(100% 0% none), oklch(50% 50% 0deg))', 'oklch(0.75 0.1 0)'],
['color-mix(in oklch, oklch(100% none 60deg), oklch(50% 50% 0deg))', 'oklch(0.75 0.2 30)'],

// Analogous sets
['color-mix(in oklch, rgb(none none none), oklch(0.5 0.2 50))', 'oklch(0.5 0.2 50)'],
['color-mix(in oklch, hwb(100deg none none), oklch(0.5 0.2 50deg))', 'oklch(0.5 0.2 94.8876)'],
['color-mix(in oklch, lab(100 none none), oklch(0.5 0.2 50deg))', 'oklch(0.75 0.2 50)'],
];

for (const test of tests) {
Expand Down
5 changes: 5 additions & 0 deletions packages/css-color-parser/test/basic/color-mix-function.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ const tests = [
['color-mix(in oklab, red 5%,)', ''],
['color-mix(in oklab, red, blue, green,)', ''],
['color-mix(in oklab, red, , green)', ''],

// Analogous sets
['color-mix(in oklch, rgb(200, 100, 50), oklch(none none none))', 'rgb(200, 100, 50)'],
['color-mix(in oklch, rgb(200, 100, 50), lab(none none none))', 'rgb(200, 100, 50)'],
['color-mix(in oklch, lab(40% 20% 10%), lab(40% 20% 10%))', 'rgb(136, 77, 75)'],
];

for (const test of tests) {
Expand Down
Loading