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
17 changes: 17 additions & 0 deletions src/__tests__/compiler/compiler.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
22 changes: 22 additions & 0 deletions src/__tests__/native/color-mix.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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(<View testID={testID} className="test" />);
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
Expand Down
8 changes: 7 additions & 1 deletion src/compiler/declarations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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" });
}
}

Expand Down
8 changes: 8 additions & 0 deletions src/native/styles/functions/color-mix.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
import {
ColorSpace,
to as convert,
HSL,
Lab,
LCH,
mix,
OKLab,
OKLCH,
P3,
parse,
sRGB,
Expand All @@ -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]);
Expand Down
Loading