Skip to content

fix(hsl): keep alpha when constructing from an object#396

Open
xianjianlf2 wants to merge 1 commit into
gka:mainfrom
xianjianlf2:fix/hsl-object-alpha-219
Open

fix(hsl): keep alpha when constructing from an object#396
xianjianlf2 wants to merge 1 commit into
gka:mainfrom
xianjianlf2:fix/hsl-object-alpha-219

Conversation

@xianjianlf2

Copy link
Copy Markdown

Problem

chroma({ h, s, l, a }) dropped the alpha channel and returned an opaque color, while the equivalent rgb object form kept it. hsl2rgb already preserves alpha for array/positional input via its args.length > 3 branch, but it unpacked objects with the keyOrder 'hsl', which excludes the a key.

Fix

Unpack with 'hsla' (the same keyOrder css/hsl2css.js already uses) so object input reaches the existing alpha-preserving path. Array and positional callers are unaffected since unpack ignores keyOrder when given 3+ args or an array.

-    args = unpack(args, 'hsl');
+    args = unpack(args, 'hsla');

Testing

Added a regression test in test/alpha.test.js:

it('constructing hsla color from object keeps alpha (#219)', () => {
    const color = chroma({ h: 120, s: 0.8, l: 0.75, a: 0.5 });
    expect(color.alpha()).toBe(0.5);
    expect(color.rgba()).toEqual([140, 242, 140, 0.5]);
});

Closes #219

`chroma({ h, s, l, a })` dropped the alpha channel and returned an
opaque color, while the equivalent rgb object form kept it. hsl2rgb
already preserves alpha for array/positional input via its
`args.length > 3` branch, but unpacked objects with the keyOrder
`'hsl'`, which excludes the `a` key. Unpack with `'hsla'` (the same
keyOrder css/hsl2css.js already uses) so object input reaches the
existing alpha-preserving path. Array and positional callers are
unaffected since unpack ignores keyOrder when given 3+ args or an array.

Closes gka#219
@xianjianlf2
xianjianlf2 requested a review from gka as a code owner July 13, 2026 12:00
@changeset-bot

changeset-bot Bot commented Jul 13, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 3a59c41

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.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

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.

hsla plain JS object constructor does not take alpha into account

1 participant