Skip to content

fix(mix): avoid NaN channels when alpha delta cancels the weight#689

Open
chatman-media wants to merge 1 commit into
styled-components:mainfrom
chatman-media:fix/mix-divide-by-zero-nan
Open

fix(mix): avoid NaN channels when alpha delta cancels the weight#689
chatman-media wants to merge 1 commit into
styled-components:mainfrom
chatman-media:fix/mix-divide-by-zero-nan

Conversation

@chatman-media

Copy link
Copy Markdown

When `x * alphaDelta === -1`, `z` (`1 + x * alphaDelta`) is `0`, but weight1 was still computed as `(x / z + 1) / 2`, dividing by zero. The result was rgba(NaN,NaN,NaN,0).

mix(1, 'rgba(255, 0, 0, 0)', 'rgba(0, 0, 255, 1)')
// before: "rgba(NaN,NaN,NaN,0)"
// after:  "rgba(255,0,0,0)"   (weight 1 => color1)

Fix mirrors the reference Sass implementation: compute z first and skip the division in that special case (combinedWeight1 = x). The normal path is algebraically identical, so existing outputs are unchanged.

Part of #654. Added a regression test (fails without the fix with the exact rgba(NaN,NaN,NaN,0)); all 252 color tests pass.

When `x * alphaDelta === -1`, `z` (`1 + x * alphaDelta`) is `0`, but the
result was still computed as `(x / z + 1) / 2`, dividing by zero and
producing `rgba(NaN,NaN,NaN,0)`. Restructure to match the reference Sass
implementation: compute `z` first and skip the division in that case
(`combinedWeight1 = x`). The normal path is algebraically unchanged.

e.g. `mix(1, 'rgba(255, 0, 0, 0)', 'rgba(0, 0, 255, 1)')` now returns
`rgba(255,0,0,0)` instead of `rgba(NaN,NaN,NaN,0)`.
@chatman-media chatman-media requested a review from bhough as a code owner June 26, 2026 20:43
@changeset-bot

changeset-bot Bot commented Jun 26, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 2efcedf

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

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.

1 participant