From a9c2d44d0de06950d8ddbde3429c84dbed006c81 Mon Sep 17 00:00:00 2001 From: Wadoud Date: Sun, 20 Sep 2026 00:48:07 +0100 Subject: [PATCH 1/3] fix(native): handle omitted percentages in color-mix and register color spaces When percentages are omitted in color-mix(), the descriptor contains trailing null values that were not consumed, causing args.length validation to fail. Consume trailing null/undefined values and register OKLCH, HSL, Lab, and LCH color spaces. --- src/__tests__/native/color-mix.test.tsx | 22 +++++++++++++++ src/native/styles/functions/color-mix.ts | 35 +++++++++++++++--------- 2 files changed, 44 insertions(+), 13 deletions(-) diff --git a/src/__tests__/native/color-mix.test.tsx b/src/__tests__/native/color-mix.test.tsx index d600e8fe..e48016d6 100644 --- a/src/__tests__/native/color-mix.test.tsx +++ b/src/__tests__/native/color-mix.test.tsx @@ -48,6 +48,28 @@ test("color-mix() - oklch", () => { }); }); +test("color-mix() - hsl", () => { + registerCSS( + `.test { + --bg: hsl(0 100% 50%); + @supports (color: color-mix(in lab, red, red)) { + background-color: color-mix(in oklab, var(--bg) 50%, transparent); + } + } + `, + { + inlineVariables: false, + }, + ); + + render(); + const component = screen.getByTestId(testID); + + expect(component.props.style).toStrictEqual({ + backgroundColor: "rgba(255, 0, 0, 0.5)", + }); +}); + test("color-mix() - black with transparent (NaN oklab channels)", () => { // lightningcss resolves this at compile time to oklab(0 NaN NaN / 0.5): // black is oklab [l=0, a=0, b=0] and transparent has no chromaticity, so the diff --git a/src/native/styles/functions/color-mix.ts b/src/native/styles/functions/color-mix.ts index c134e497..d1cae812 100644 --- a/src/native/styles/functions/color-mix.ts +++ b/src/native/styles/functions/color-mix.ts @@ -1,8 +1,12 @@ import { ColorSpace, to as convert, + HSL, + Lab, + LCH, mix, OKLab, + OKLCH, P3, parse, sRGB, @@ -13,6 +17,10 @@ import type { StyleFunctionResolver } from "../resolve"; ColorSpace.register(sRGB); ColorSpace.register(P3); ColorSpace.register(OKLab); +ColorSpace.register(OKLCH); +ColorSpace.register(HSL); +ColorSpace.register(Lab); +ColorSpace.register(LCH); export const colorMix: StyleFunctionResolver = (resolveValue, value) => { const resolved = resolveValue(value[2]); @@ -21,23 +29,24 @@ export const colorMix: StyleFunctionResolver = (resolveValue, value) => { // Resolved descriptors may be cached and shared by another styled component. const args: unknown[] = [...(resolved as unknown[])]; try { - const space = args.shift(); - const leftValue = args.shift(); - if (typeof space !== "string" || typeof leftValue !== "string") return; + if (args.length > 5) return; + const [space, leftValue, leftPct, rightValue, rightPct] = args; + if ( + typeof space !== "string" || + typeof leftValue !== "string" || + typeof rightValue !== "string" + ) { + return; + } ColorSpace.get(space); const left = parse(leftValue); - const percentage = () => { - const next = args[0]; - if (typeof next !== "string" || !next.endsWith("%")) return undefined; - args.shift(); - return Number(next.slice(0, -1)) / 100; + const parseWeight = (val: unknown) => { + if (typeof val !== "string" || !val.endsWith("%")) return undefined; + return Number(val.slice(0, -1)) / 100; }; - let leftWeight = percentage(); - const rightValue = args.shift(); - if (typeof rightValue !== "string") return; + let leftWeight = parseWeight(leftPct); const right = parse(rightValue); - let rightWeight = percentage(); - if (args.length) return; + let rightWeight = parseWeight(rightPct); // CSS Color 5: omitted weights are complementary, then both are normalized. leftWeight ??= rightWeight === undefined ? 0.5 : 1 - rightWeight; From c035baa7ae62f0bb12bb1cc91ac8c02e2c146039 Mon Sep 17 00:00:00 2001 From: Sam Moore Date: Mon, 23 Nov 2026 11:03:58 -0500 Subject: [PATCH 2/3] fix(native): read color-mix arguments in order again #579 now drops omitted weights from the compiled descriptor instead of serializing them as null, so the descriptor is 3 to 5 entries long and a weight's index depends on whether the left weight is present. Reading it by fixed position broke every mix without a left weight: `var(--x), blue`, `var(--x), blue 25%` and `var(--x), transparent 75%` resolved to nothing, failing 5 of main's color-mix tests. Restore main's sequential reader, which also keeps rejecting a weight that resolves to a non-percentage (an unset variable) instead of treating it as omitted. The color space registrations stay. --- src/native/styles/functions/color-mix.ts | 27 ++++++++++++------------ 1 file changed, 13 insertions(+), 14 deletions(-) diff --git a/src/native/styles/functions/color-mix.ts b/src/native/styles/functions/color-mix.ts index d1cae812..0a322d41 100644 --- a/src/native/styles/functions/color-mix.ts +++ b/src/native/styles/functions/color-mix.ts @@ -29,24 +29,23 @@ export const colorMix: StyleFunctionResolver = (resolveValue, value) => { // Resolved descriptors may be cached and shared by another styled component. const args: unknown[] = [...(resolved as unknown[])]; try { - if (args.length > 5) return; - const [space, leftValue, leftPct, rightValue, rightPct] = args; - if ( - typeof space !== "string" || - typeof leftValue !== "string" || - typeof rightValue !== "string" - ) { - return; - } + const space = args.shift(); + const leftValue = args.shift(); + if (typeof space !== "string" || typeof leftValue !== "string") return; ColorSpace.get(space); const left = parse(leftValue); - const parseWeight = (val: unknown) => { - if (typeof val !== "string" || !val.endsWith("%")) return undefined; - return Number(val.slice(0, -1)) / 100; + const percentage = () => { + const next = args[0]; + if (typeof next !== "string" || !next.endsWith("%")) return undefined; + args.shift(); + return Number(next.slice(0, -1)) / 100; }; - let leftWeight = parseWeight(leftPct); + let leftWeight = percentage(); + const rightValue = args.shift(); + if (typeof rightValue !== "string") return; const right = parse(rightValue); - let rightWeight = parseWeight(rightPct); + let rightWeight = percentage(); + if (args.length) return; // CSS Color 5: omitted weights are complementary, then both are normalized. leftWeight ??= rightWeight === undefined ? 0.5 : 1 - rightWeight; From 9f560e67b5fcea37536ea2d7dc94fcf51a698132 Mon Sep 17 00:00:00 2001 From: Sam Moore Date: Mon, 23 Nov 2026 14:00:24 -0500 Subject: [PATCH 3/3] fix(compiler): clip out-of-sRGB colors instead of gamut mapping them MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The compiler prints colors with colorjs's Color#toString({ format: "hex" }), which gamut maps anything outside sRGB with the CSS Color 4 algorithm (colorjs's default "css" method). A color resolved at runtime is instead passed to React Native as rgba() with out-of-range channels, which React Native clamps per channel. The same color could therefore render visibly differently depending on whether a variable kept it at runtime: `color-mix(in lch, red, blue)` folds to #cd007e but renders as #f50086 through a variable. Clip at compile time too. That matches React Native and every current browser: Chrome 158/160, Edge 158, Firefox 160/163 and Safari 27.2/TP 257 all read back the clipped value in WPT's css/css-color/canvas-srgb-gamut-mapping.html (1/20, control only). toString()'s inGamut option is typed as a boolean, so convert to sRGB and clip with toGamut() first. Tailwind's default palette is barely affected. 94 of its 242 colors are outside sRGB, but the CSS algorithm already returns the clipped color when that is within a just-noticeable difference, so only yellow-400/500 and amber-400/500 change, each by one 8-bit step (ΔE2000 ≤ 0.5). Only the default hexColors output changes. With hexColors: false or colorPrecision, the compiler still prints colors in their own space. --- src/__tests__/compiler/compiler.test.tsx | 17 +++++++++++++++++ src/compiler/declarations.ts | 8 +++++++- 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/src/__tests__/compiler/compiler.test.tsx b/src/__tests__/compiler/compiler.test.tsx index a1a6906b..b81f196f 100644 --- a/src/__tests__/compiler/compiler.test.tsx +++ b/src/__tests__/compiler/compiler.test.tsx @@ -43,6 +43,23 @@ test("reads global CSS variables", () => { }); }); +test.each([ + // Browsers read these back clipped in WPT css/css-color/canvas-srgb-gamut-mapping.html + ["oklch(80% 0.35 30)", "#f00"], + ["oklch(90% 0.35 0)", "#ff32d5"], + // Folded by lightningcss to an oklch()/lch() color outside sRGB + ["color-mix(in oklch, red, blue)", "#ba00c2"], + ["color-mix(in lch, red, blue)", "#f50086"], +])("out-of-sRGB color %s is clipped, not gamut mapped", (value, expected) => { + const compiled = compile(`:root { --c: ${value}; }`, { + inlineVariables: false, + }); + + expect(compiled.stylesheet()).toStrictEqual({ + vr: [["c", [[expected]]]], + }); +}); + test(":root CSS variables with media queries", () => { const compiled = compile( `:root { diff --git a/src/compiler/declarations.ts b/src/compiler/declarations.ts index f38ab1c2..366b878c 100644 --- a/src/compiler/declarations.ts +++ b/src/compiler/declarations.ts @@ -1891,7 +1891,13 @@ export function parseColor(cssColor: CssColor, builder: StylesheetBuilder) { if (!hexColors || colorPrecision) { return color?.toString({ precision: colorPrecision ?? 3 }); } else { - return color?.toString({ format: "hex" }); + // Clip out-of-gamut channels, as React Native and every browser do, so a + // folded color matches the same color resolved at runtime. toString() + // alone would gamut map it with the CSS Color 4 algorithm instead. + return color + ?.to("srgb") + .toGamut({ method: "clip" }) + .toString({ format: "hex" }); } }