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/__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/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" }); } } diff --git a/src/native/styles/functions/color-mix.ts b/src/native/styles/functions/color-mix.ts index c134e497..0a322d41 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]);