8000 feat(plugin-resize): add separateAlpha to keep the colour of transparent pixels by hskl18 · Pull Request #1496 · jimp-dev/jimp · GitHub
[go: up one dir, main page]

Skip to content

feat(plugin-resize): add separateAlpha to keep the colour of transparent pixels - #1496

Closed
hskl18 wants to merge 1 commit into
jimp-dev:mainfrom
hskl18:feat/819-resize-keep-transparent-rgb
Closed

hskl18 wants to merge 1 commit into
jimp-dev:mainfrom
hskl18:feat/819-resize-keep-transparent-rgb

Conversation

@hskl18
@hskl18 hskl18 commented Nov 23, 2026
Copy link
Copy Markdown
Contributor

Fixes #819

What's Changing and Why

resize() treats alpha as opacity, so the colour under alpha 0 is lost: the default mode ignores it, and the other blending modes premultiply by alpha (#1459, #1481). That is right for pictures, and wrong when alpha holds something else, like the self-illumination textures in #819.

resize now takes separateAlpha: true. The colour is resized as if the image were opaque, and the alpha as it is without the option:

image.resize({ w: 300, separateAlpha: true });

It works with every ResizeStrategy. I did it by resizing an opaque copy for the colour and the image itself for the alpha, so neither resizer changes. A flag that skips the premultiply steps would have touched seven code paths across the two resizers. The cost is that it runs the resize twice.

The default doesn't change.

What else might be affected

Nothing without the option. scale, scaleToFit, contain, cover and rotate don't take it yet; I can add it to them here or in a follow-up if you want it there.

Testing

  • New tests in plugin-resize cover every mode at four sizes: the colour equals the resize of an opaque copy, and the alpha equals the resize without the option. 31 of them fail without the change.
  • Locally on Node 22: pnpm build, pnpm lint, pnpm test and pnpm test:browser (Chromium) pass.

Tasks

  • Add tests
  • Update Documentation
  • Update jimp.d.ts (the file no longer exists; the option's type comes from the zod schema)
  • Add SemVer Label

@hipstersmoothie
Copy link
Copy Markdown
Collaborator

merged via #1502

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.

Make resize() not remove rgb data when alpha = 0

2 participants

0