Repository navigation
colorToAlpha (#39) - #52
Merged
Merged
Conversation
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
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Ljnxm1Qjrocq1izGajLx8J
matmen
force-pushed
the
color-to-alpha
branch
from
October 15, 2026 08:32
ee0ccdf to
378b4a5
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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: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.jschecks a red antialiased circle on white:Closes #39