Skip to content

Fix division by zero in polar color space unpremultiply when alpha is 0#1195

Closed
queelius wants to merge 1 commit intoparcel-bundler:masterfrom
queelius:fix/polar-unpremultiply-div-by-zero
Closed

Fix division by zero in polar color space unpremultiply when alpha is 0#1195
queelius wants to merge 1 commit intoparcel-bundler:masterfrom
queelius:fix/polar-unpremultiply-div-by-zero

Conversation

@queelius
Copy link
Copy Markdown

Summary

  • Fix division by zero in polar_premultiply macro's unpremultiply method when alpha == 0.0
  • Add && self.alpha != 0.0 guard to match the existing rectangular_premultiply behavior
  • Add tests for color-mix() with zero-alpha colors in all four polar color spaces (HSL, HWB, LCH, OKLCH)

Problem

When using color-mix() to interpolate colors with zero alpha in polar color spaces (HSL, HWB, LCH, OKLCH), the unpremultiply step divides by alpha (which is 0), producing NaN values. These NaN values then serialize as none in the CSS output.

For example:

/* Input */
.foo { color: color-mix(in oklch, oklch(0.5 0.2 120 / 0), oklch(0.7 0.1 240 / 0)); }
/* Before fix: */ .foo{color:oklch(none none 180/0)}
/* After fix:  */ .foo{color:oklch(0% 0 180/0)}

The rectangular_premultiply macro already guards against this with self.alpha != 0.0, but polar_premultiply was missing the check.

Test plan

  • Added test cases for color-mix() with zero-alpha in HSL, HWB, LCH, and OKLCH
  • All existing tests pass (cargo test - 117 tests + doc tests)

Fixes #1194

Generated with Claude Code

The polar_premultiply macro's unpremultiply method was missing a check
for alpha != 0.0 before dividing by alpha, causing NaN values to leak
into the output when interpolating colors with zero alpha in polar color
spaces (HSL, HWB, LCH, OKLCH) via color-mix().

The rectangular_premultiply macro already had this guard. This commit
adds the same check to polar_premultiply to match.

Fixes parcel-bundler#1194

Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
Copilot AI review requested due to automatic review settings March 25, 2026 08:44
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

Fixes a division-by-zero bug in polar color-space interpolation when alpha == 0.0, preventing NaN components from leaking into serialized CSS (e.g. none in oklch() / lch() output) during color-mix().

Changes:

  • Add an alpha != 0.0 guard in the polar_premultiply macro’s unpremultiply method (matching rectangular_premultiply behavior).
  • Add regression tests covering color-mix() with zero-alpha inputs in HSL, HWB, LCH, and OKLCH.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/values/color.rs Prevents division by zero during unpremultiply for polar color spaces when alpha is zero.
src/lib.rs Adds regression tests to ensure zero-alpha color-mix() in polar spaces doesn’t serialize NaN/none.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@queelius
Copy link
Copy Markdown
Author

Closing this to keep your queue clean. Happy to reopen if useful, or feel free to pick up any of it. Thanks!

@queelius queelius closed this Apr 25, 2026
@queelius queelius deleted the fix/polar-unpremultiply-div-by-zero branch April 25, 2026 00:42
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.

color-mix: division by zero in polar color space unpremultiply when alpha is 0

2 participants