Skip to content

fix(blend): composite colors using their alpha values - #407

Merged
gka merged 1 commit into
gka:mainfrom
snowyukitty:fix/blend-alpha-compositing
Oct 24, 2026
Merged

gka merged 1 commit into
gka:mainfrom
snowyukitty:fix/blend-alpha-compositing

Conversation

@snowyukitty

Copy link
Copy Markdown
Contributor

Fixes #281.

blend() reads both inputs with rgb(), discarding their alpha values. A transparent top color can therefore change the bottom color, and two transparent inputs can produce an opaque result.

Composite the existing RGB blend functions using source-over alpha, with the second color over the first. Handle transparent operands before evaluating the blend, and clamp the overlap color before weighting it. Also handle the white-backdrop/black-source burn endpoint, which currently throws on 0/0.

The existing opaque RGB results and clipping behavior are retained, except that the undefined burn endpoint now returns white. Translucent results can now include alpha in the default hex() output; the documentation includes examples. A NaN alpha set through the mutating setter is now rejected.

Validation:

  • Node 20.20.2, pnpm 10.24.0: pnpm lint, pnpm test -- --run (2,582 tests), and pnpm build pass.
  • The final blend tests produce 39 intended failures and 14 passing controls with the original production source; all 53 pass with the fix.
  • pnpm run docs passes on Node 24.18.1/Linux.

Regression coverage includes all eight modes, transparent and partially transparent operands, direct mode methods, RGB rounding, input Color immutability, burn clipping, and alpha modified through the mutating setter. No dependencies or generated files change.

@snowyukitty
snowyukitty requested a review from gka as a code owner October 24, 2026 01:45
@changeset-bot

changeset-bot Bot commented Oct 24, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: ad604f3

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

@gka gka left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

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

looks good, thanks!

@gka
gka merged commit a411cbc into gka:main Oct 24, 2026
1 check passed
gka added a commit that referenced this pull request Oct 24, 2026
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.

Blending of rgb and rgba don't blend properly

2 participants