Skip to content

colorToAlpha (#39) - #52

Merged
matmen merged 2 commits into
masterfrom
color-to-alpha
Oct 15, 2026
Merged

matmen merged 2 commits into
masterfrom
color-to-alpha

Conversation

@matmen

@matmen matmen commented Oct 15, 2026

Copy link
Copy Markdown
Owner

Adds Image.colorToAlpha(color?, options?), which makes a color transparent, typically to remove a solid background from a logo, emoji or sticker.

How it works

A simple approach ("pixels of the background color, ± a tolerance, get alpha 0") leaves a fringe of the old background around every antialiased edge, which shows as a halo on a different background. colorToAlpha() works like GIMP's "Color to Alpha" instead:

  • Every pixel is unmixed: for each pixel it computes the lowest opacity at which some color, drawn over the removed color, produces exactly that pixel. The pixel takes that opacity and that color.
  • Exact pixels of the color become fully transparent.
  • Edges that are partly the color become semi-transparent with the color unmixed, so they look clean over any background.
  • Lossless round trip: compositing the result over the removed color gives back the original image exactly.

API

  • color: the color to remove. Without it, the top left pixel's color is used (usually the background).
  • options.transparencyThreshold (0–1, default 0): pixels at most this far from the color become fully transparent. This helps with noisy or JPEG-compressed backgrounds.
  • options.opacityThreshold (0–1, default 1): pixels at least this far from the color stay fully opaque.

The color is removed everywhere in the image, not only from the connected background: removing a white background also makes white areas inside the subject transparent. Removing only the connected area around the subject (like a magic wand) would be a separate feature.

Tests

tests/regressions.js checks a red antialiased circle on white:

  • the background becomes transparent and the circle stays opaque
  • compositing the result over white gives the original back exactly
  • the edge becomes translucent red with no white in it
  • a transparency threshold makes more of the edge transparent

Closes #39

@matmen matmen added this to the v3.1.0 milestone Oct 15, 2026
@matmen matmen self-assigned this Oct 15, 2026
@matmen matmen added the enhancement New feature or request label Oct 15, 2026
matmen and others added 2 commits October 15, 2026 08:32
Works like GIMP's "Color to Alpha": the color is unmixed from every pixel,
so compositing the result over that color gives the original back and
antialiased edges keep no fringe of it. Thresholds make colors close to it
fully transparent or keep distant ones fully opaque; without a color, the
top left pixel's is used.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Ljnxm1Qjrocq1izGajLx8J
@matmen
matmen merged commit 8314d37 into master Oct 15, 2026
13 checks passed
@matmen
matmen deleted the color-to-alpha branch October 15, 2026 08:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[FR] Replace (background) color to transparency

1 participant