Skip to content

fix: preserve alpha when interpolating in oklch - #394

Open
mahirhir wants to merge 1 commit into
gka:mainfrom
mahirhir:fix-oklch-interpolate-alpha
Open

fix: preserve alpha when interpolating in oklch#394
mahirhir wants to merge 1 commit into
gka:mainfrom
mahirhir:fix-oklch-interpolate-alpha

Conversation

@mahirhir

@mahirhir mahirhir commented Jun 29, 2026

Copy link
Copy Markdown

The shared hue interpolator in src/interpolator/_hsx.js reverses the polar color so the hue ends up at index 0 for the hue-blending math. For oklch it does that with Array.prototype.reverse():

xyz0 = col1.oklch().reverse();
xyz1 = col2.oklch().reverse();

rgb2oklch appends the alpha channel when it is below 1, so oklch() returns [L, c, h, a] for a semi-transparent color. reverse() flips the whole array to [a, h, c, L], and the next line [hue0, sat0, lbv0] = xyz0 then reads alpha as the hue, hue as the chroma and chroma as the lightness. So any oklch interpolation/mix where an input has alpha < 1 comes out wrong.

The hcl branch right above it does not have this problem: Color.prototype.hcl was switched to reverse3 (in 27a2bb3, resolving #359), which only reverses the first three components and leaves alpha in place. The oklch branch was left on the old reverse(). This change uses the same reverse3 helper there.

Mixing a half-transparent red with blue, the rgb part should match the opaque mix (only the alpha channel should differ):

// before
chroma.mix('#ff000080', '#0000ff', 0.5, 'oklch').hex('rgb') // '#ff00ff'  (wrong)
chroma.mix('#ff0000',   '#0000ff', 0.5, 'oklch').hex('rgb') // '#ba00c2'  (opaque, correct)

// after
chroma.mix('#ff000080', '#0000ff', 0.5, 'oklch').hex('rgb') // '#ba00c2'
chroma.mix('#ff000080', '#0000ff', 0.5, 'oklch').hex()      // '#ba00c2bf'

Added a regression test in test/mix.test.js and a changeset. The full suite passes.

@mahirhir
mahirhir requested a review from gka as a code owner June 29, 2026 07:42
@changeset-bot

changeset-bot Bot commented Jun 29, 2026

Copy link
Copy Markdown

🦋 Changeset detected

Latest commit: 57b5ef5

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
chroma-js Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another 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