fix(web): bound OKLCH gamut mapping - #6485
Conversation
|
Important Review skippedAuto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the ⚙️ Run configurationConfiguration used: Repository UI Review profile: CHILL Plan: Pro Plus Run ID: You can disable this status message by setting the Use the checkbox below for a quick retry:
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
ApprovabilityVerdict: Approved 335b00b This is a straightforward numerical bug fix that prevents overflow when processing extreme OKLCH chroma values. The change uses an equivalent mathematical formulation (log(a/b) = log(a) - log(b)) that avoids division overflow, with a test confirming the fix. You can customize Macroscope's approvability policy. Learn more. |
What Changed
Theme color gamut mapping now calculates its binary-search iteration count by subtracting base-2 logarithms instead of dividing chroma by the target resolution. This keeps the count finite for every finite OKLCH chroma accepted by the theme parser.
A focused regression imports a theme containing
oklch(0.5 1e303 0), verifies that parsing preserves the magnitude, and checks the mapped sRGB result.Verified with the 31-test
themePalettesuite, the web package typecheck, and focused lint and formatting checks.Why
The previous
chroma / resolutioncalculation could overflow toInfinityeven when chroma itself was finite. That made the loop bound infinite and could hang the web or desktop renderer when an imported theme with extreme chroma was converted for the color editor.The logarithm subtraction is algebraically equivalent for ordinary colors, preserves the existing chroma resolution, and avoids the overflowing intermediate value.
Checklist
Made with GPT-5.6 using the Codex harness in T3 Code.
Note
Fix overflow in
mapThemeOklchToSrgbGamutfor extremely large OKLCH chroma valuesThe bisection step count in
themePalette.tspreviously computedlog2(C / chromaResolution), which overflows toInfinitywhenCis extremely large (e.g.1e303). This is fixed by computinglog2(C) - log2(chromaResolution)instead, which avoids intermediate overflow while preserving the same effective resolution.Macroscope summarized 335b00b.
Note
Low Risk
Localized numeric fix in theme color conversion with a targeted test; no auth, storage, or API surface changes.
Overview
Fixes a hang when imported themes contain OKLCH colors with astronomically large but finite chroma (e.g.
oklch(0.5 1e303 0)).mapThemeOklchToSrgbGamutnow computes the bisection iteration count aslog2(C) - log2(chromaResolution)instead oflog2(C / chromaResolution). That avoids intermediate overflow toInfinity(which made the loop unbounded) while keeping the same effective resolution for normal colors.A regression test parses extreme chroma through
parseThemeFile, asserts the canonical OKLCH string is preserved, and checksthemeColorToHexreturns#b5005e.Reviewed by Cursor Bugbot for commit 335b00b. Bugbot is set up for automated code reviews on this repo. Configure here.