Skip to content

fix: coerce NaN Lab/OKLab channels to 0 in parseColor#351

Open
sarathfrancis90 wants to merge 1 commit into
nativewind:mainfrom
sarathfrancis90:fix-color-mix-nan-channels
Open

fix: coerce NaN Lab/OKLab channels to 0 in parseColor#351
sarathfrancis90 wants to merge 1 commit into
nativewind:mainfrom
sarathfrancis90:fix-color-mix-nan-channels

Conversation

@sarathfrancis90

Copy link
Copy Markdown

Summary

Fixes #317.

color-mix() is resolved at compile time by lightningcss. For a degenerate
mix the result can have NaN chromaticity channels — mixing #000 with
transparent in oklab gives oklab(0 NaN NaN / 0.5), because black has no
chromaticity to interpolate. parseColor passed those NaN coords straight
to colorjs.io, which serialized them to "#NaNNaNNaN80". React Native
silently discards an invalid color string, so utilities like Tailwind's
bg-black/50 rendered with no background.

Fix

The lab/lch/oklab/oklch cases in parseColor (src/compiler/declarations.ts)
now coerce each channel through a small nanToZero helper. A missing color
component is 0 per CSS Color 4, so oklab(0 NaN NaN / 0.5) becomes
oklab(0 0 0 / 0.5) = black at 50% alpha, serializing to #00000080.

Testing

Added a regression test in src/__tests__/native/color-mix.test.tsx for
color-mix(in oklab, #000 50%, transparent). It fails on main
(Received "#NaNNaNNaN80") and passes with the fix ("#00000080").

yarn test, yarn typecheck and yarn lint all pass.

color-mix() resolved at compile time by lightningcss can yield NaN
chromaticity channels for degenerate mixes (e.g. mixing black with
transparent in oklab). Passing NaN to colorjs.io produced an invalid
color string like "#NaNNaNNaN80" that React Native silently discards,
so utilities such as Tailwind's bg-black/50 rendered with no background.

Treat NaN channels as 0 (a missing component per CSS Color 4) for the
lab/lch/oklab/oklch cases, so the example resolves to nativewind#80.

Fixes nativewind#317
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.

[Bug] bg-black/50 silently fails — NaN oklab channels from lightningcss color-mix resolution

1 participant