Skip to content

fix(native): register color spaces for color-mix and clip out-of-sRGB colors - #457

Merged
danstepanov merged 3 commits into
nativewind:mainfrom
Wadiou:fix/color-mix-oklch
Nov 23, 2026
Merged

danstepanov merged 3 commits into
nativewind:mainfrom
Wadiou:fix/color-mix-oklch

Conversation

@Wadiou

@Wadiou Wadiou commented Sep 19, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Register OKLCH, HSL, Lab, and LCH color spaces with colorjs.io so color-mix() evaluates them at runtime, and clip out-of-sRGB colors at compile time so they match the runtime result. Omitted percentages are handled by #579.

Key Changes

Runtime

  • Register OKLCH, HSL, Lab, and LCH color spaces with colorjs.io.

Compiler

  • src/compiler/declarations.ts: Clip out-of-sRGB colors instead of gamut mapping them, so a color folded at compile time matches the same color resolved at runtime.

Tests

  • src/__tests__/native/color-mix.test.tsx: Add test coverage for hsl and oklch color spaces.
  • src/__tests__/compiler/compiler.test.tsx: Cover clipping of an out-of-sRGB color.

@sammoore sammoore left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this! A couple of changes to make before merging:

  • Switching from args.shift to exact argument positioning breaks this PR (and tests) after rebasing with main.
  • Compile time evaluation of color outside the sRGB range results in gamut mapping per the CSS spec (outside of this PR, based on Color.toString's defaults from colorjs.io), while the runtime implementation here is clipping anything out of sRGB range (by way of passing the color, not gamut-mapped, to React Native).
    • Clipping is how browsers today behave on monitors that don't natively support the colors, but the CSS spec in most cases is going to give a color closer to the desired one.
    • However, React Native itself already clips, and it's the behavior users are accustomed to on the web (all major browsers fail these web-platform-tests). So, I'm leaning towards clipping.
    • So, let's update the compiler such that it and runtime produce the same color. Otherwise, the same color evaluated at compile time and runtime could be visibly different within the same application on the same device.

I already have fixes for these issues that we'll push before merging, as I'd like to squeeze this in to today's release.

@sammoore

Copy link
Copy Markdown
Contributor

To be clear, going back to args.shift and not handling the null is fine now: #579 has since been merged (after this PR was authored).

Wadiou and others added 3 commits November 23, 2026 12:22
…or 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.
nativewind#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.
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.
@danstepanov danstepanov changed the title fix(native): handle omitted percentages in color-mix and register color spaces fix(native): register color spaces for color-mix and clip out-of-sRGB colors Nov 23, 2026
@danstepanov
danstepanov merged commit 799a7b7 into nativewind:main Nov 23, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants