Skip to content

Fix RGB channels drifting when only alpha is changed - #234

Merged
omgovich merged 2 commits into
masterfrom
fix/alpha-rgb-drift
Sep 1, 2026
Merged

omgovich merged 2 commits into
masterfrom
fix/alpha-rgb-drift

Conversation

@omgovich

@omgovich omgovich commented Sep 1, 2026

Copy link
Copy Markdown
Owner

Fixes #163. Alternative to #184.

Why

Moving only the alpha slider changed the RGB channels too: { r: 200, g: 120, b: 35, a: 0.5 } became { r: 199, g: 119, b: 34 } after an alpha-only interaction. The root cause is that rgbaToHsva rounds h/s/v to integers, so the internal HSVA representation loses precision and fromHsva(toHsva(color)) doesn't round-trip.

What changed

  • rgbaToHsva no longer rounds its result — the internal HSVA state keeps full precision. All output conversions (hsvaToRgba, hsvaToHex, etc.) still round, so the values emitted by onChange are unchanged in format.
  • This fixes the drift for RgbaColorPicker, RgbaStringColorPicker and HexAlphaColorPicker in one place, with no new API and no size cost (unlike Fix RGBA calc error when change alpha #184, which added an updateAlpha method to every color model and went over the size limit).
  • Added regression tests: an alpha-only interaction must keep the RGB channels (RgbaColorPicker) and the hex color (HexAlphaColorPicker) intact.
  • Updated four existing tests in tests/utils.test.js that asserted rounded values from input converters — they now compare via roundHsva, same semantics.

Verified exhaustively outside the test suite: with rounding removed, hsvaToRgba(rgbaToHsva(c)) round-trips exactly for all 16.7M RGB values.

How to review

The production change is only rgbaToHsva in src/utils/convert.ts — three round() calls removed. Worth double-checking that nothing user-visible relies on the internal HSVA being integers: the UI components already round for aria-* output, and hslaToHsva never rounded to begin with. Bundle size is neutral (±1 B gzip noise).

@omgovich omgovich self-assigned this Sep 1, 2026
@omgovich
omgovich force-pushed the fix/alpha-rgb-drift branch from 6f99919 to b0945d1 Compare September 1, 2026 15:49
@github-actions

github-actions Bot commented Sep 1, 2026

Copy link
Copy Markdown

Size Change: -2 B (-0.04%)

Total Size: 4.91 kB

📦 View Changed
Filename Size Change
dist/index.module.js 4.91 kB -2 B (-0.04%)

compressed-size-action

@omgovich
omgovich merged commit 6d6e0ce into master Sep 1, 2026
7 checks passed
@omgovich
omgovich deleted the fix/alpha-rgb-drift branch September 1, 2026 16:16
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.

RGBA calc error when change alpha

1 participant