Skip to content

fix(plugin-color): compute sepia output channels from the original pixel#1412

Open
spokodev wants to merge 1 commit into
jimp-dev:mainfrom
spokodev:fix/sepia-original-channels
Open

fix(plugin-color): compute sepia output channels from the original pixel#1412
spokodev wants to merge 1 commit into
jimp-dev:mainfrom
spokodev:fix/sepia-original-channels

Conversation

@spokodev

@spokodev spokodev commented Jul 2, 2026

Copy link
Copy Markdown

sepia() returns wrong colors for any non-grayscale pixel. The sepia matrix needs each output channel to be a linear combination of the original r, g, b, but the code overwrites red first, then computes green from the new red, then blue from the new red and green:

red = red * 0.393 + green * 0.769 + blue * 0.189;
green = red * 0.349 + green * 0.686 + blue * 0.168;   // reads the new red
blue = red * 0.272 + green * 0.534 + blue * 0.131;    // reads the new red and green

For a pure red pixel (255, 0, 0) the correct sepia output is (100, 88, 69); the current code returns (100, 34, 45).

This is a regression: the same bug was reported (#346) and fixed (#483) in 2018, and the v1 TypeScript rewrite reintroduced it.

Fix

Hold the original channels and derive each output from them, matching the merged 2018 fix.

Testing

Added a test that drives sepia against a known red pixel and asserts (100, 88, 69). It fails before this change. The existing packages/jimp sepia snapshot had captured the buggy output (64-22-2D); it is rebuilt to the corrected values (64-58-45). Full plugin-color suite stays green.

sepia overwrote red, then read that new red when computing green, then
read the new red and green when computing blue, so the green and blue
outputs were wrong for any non-grayscale pixel. Hold the original r, g, b
and derive each output channel from them. Regenerate the sepia snapshot,
which had captured the incorrect output.
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.

1 participant