Repository navigation
fix(native): register color spaces for color-mix and clip out-of-sRGB colors - #457
Merged
Merged
Conversation
sammoore
suggested changes
Nov 23, 2026
sammoore
left a comment
Contributor
There was a problem hiding this comment.
Thanks for this! A couple of changes to make before merging:
- Switching from
args.shiftto exact argument positioning breaks this PR (and tests) after rebasing withmain. - Compile time evaluation of color outside the
sRGBrange results in gamut mapping per the CSS spec (outside of this PR, based onColor.toString's defaults fromcolorjs.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.
Contributor
|
To be clear, going back to |
…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
force-pushed
the
fix/color-mix-oklch
branch
from
November 23, 2026 20:22
51cfb55 to
9f560e6
Compare
danstepanov
approved these changes
Nov 23, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Register OKLCH, HSL, Lab, and LCH color spaces with
colorjs.iosocolor-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
OKLCH,HSL,Lab, andLCHcolor spaces withcolorjs.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 forhslandoklchcolor spaces.src/__tests__/compiler/compiler.test.tsx: Cover clipping of an out-of-sRGB color.