8000 fix(plugin-color)!: round greyscale and use the same luminance in color() by hipstersmoothie · Pull Request #1507 · jimp-dev/jimp · GitHub
[go: up one dir, main page]

Skip to content

fix(plugin-color)!: round greyscale and use the same luminance in color() - #1507

Merged
hipstersmoothie merged 1 commit into
mainfrom
sesh/jimp-2-0-greyscale-consistency
Nov 23, 2026
Merged

hipstersmoothie merged 1 commit into
mainfrom
sesh/jimp-2-0-greyscale-consistency

Conversation

@hipstersmoothie
Copy link
Copy Markdown
Collaborator

Grey-value bugs in plugin-color found while writing the 2.0 docs, plus a check of every other place that computes grey.

What was wrong

// 1. greyscale() truncates, so it isn't idempotent
new Jimp({ width: 1, height: 1, color: 0x050505ff }).greyscale();
// → [4, 4, 4, 255]. 0.2126 + 0.7152 + 0.0722 sums to a little under 1 in
//   floating point, so 62 of the 256 greys (5, 7, 10, 13, 14, 20, …) lose a
//   level every time. Colours were truncated too: yellow 236.589 → 236.

// 2. color()'s greyscale is a different formula
const red = () => new Jimp({ width: 1, height: 1, color: 0xff0000ff });
red().greyscale();                          // [54, 54, 54, 255]   Rec. 709 luminance
red().color([{ apply: "greyscale" }]);      // [128, 128, 128, 255] HSL lightness (tinycolor)

What changed

  • greyscale() sets r, g and b to Math.round(0.2126 * r + 0.7152 * g + 0.0722 * b), rounded to the nearest integer. Alpha is unchanged. Grey pixels keep their value, so greyscale() is idempotent.
  • color([{ apply: "greyscale" }]) now uses the same rounded Rec. 709 luminance, so it gives exactly the same bytes as greyscale(). In a chain it uses the current (possibly fractional) values and clamps to 0–255, like the other steps.
  • desaturate is unchanged, on purpose. It scales HSL saturation, so desaturate 100 gives the HSL lightness (max + min) / 2 (red → 128). If 100 switched to luminance, there would be a jump between 99.9 and 100. It also wouldn't match GIMP or CSS saturate(), where full desaturation isn't Rec. 709 lumin 8000 ance either. The color() JSDoc now spells this out.
  • otsuThreshold (with autoGreyscale, the default) rounds the luminance the same way, so it still equals greyscale() followed by otsuThreshold(img, { autoGreyscale: false }). threshold calls greyscale(), so it picks up the rounding automatically.
  • pHash / hash() output is unchanged. pHash used to call greyscale(), so it now does its own grey conversion with the old truncation (legacyGrey), because a different hash would break hashes users have stored. I checked this by hashing all 16 test images (pHash(), hash(), hash(2), hash(16)) and 50 random-noise images before and after the change; the results are byte-identical. plugin-hash no longer depends on plugin-color.

Left in place on purpose

  • plugin-mask uses GIMP's grey: linear-light luminance with babl's D50-adapted weights (0.2224879 / 0.7169038 / 0.0606083), encoded back to sRGB and rounded (fix(plugin-mask): use GIMP's luminance for colour masks, honour mask alpha, round results #1491). The goal there is to match GIMP's mask conversion, not greyscale(). Grey mask pixels are used as-is, so it's already idempotent and doesn't truncate.
  • plugin-dither (palette: "bw") uses the same Rec. 709 weights but keeps the luminance unrounded. It's the target value for error diffusion and Bayer offsets, so rounding would only throw away precision. It never truncates. For grey input the value is v to within float precision, and no Bayer or Floyd–Steinberg threshold lands on an integer, so the result is the same as greyscale().dither() for grey input.

Snapshots changed

  • packages/jimp/src/__snapshots__/callbacks.test.ts.snap (with greyscale): the middle row of the 3×3 test image is yellow, cyan and magenta. Their luminances (236.589, 200.787, 72.624) now round up: EC/C8/48 → ED/C9/49. The red, green and blue rows (54.213, 182.376, 18.411) are unchanged.
  • plugins/plugin-threshold/.../otsu-thresholds-a-photo-at-the-level-otsu-picks-1-snap.png: otsuThreshold(cops.jpg) goes from 93 to 94. scikit-image's threshold_otsu gives 93 on the new rounded grey values (it gave 92 on the old truncated ones), and jimp's documented level is one more than that. Pixel diff against the old snapshot: 72,465 kept pixels are exactly +1 (rounding instead of truncating), and 762 pixels with grey 93 are now kept instead of replaced with 255, because the level moved to 94. Nothing else changed.

The color-equivalence test's tinycolor reference now uses the rounded luminance for greyscale, and every other action is still checked against tinycolor. Added regression tests for greyscale() (exact values, alpha kept, every grey 0–255 unchanged), color() greyscale vs greyscale() on 4096 colours, and Otsu rounding ([5, 200] → 6 with and without autoGreyscale). I also renamed the desaturate 100 test to say it gives the HSL lightness.

BREAKING CHANGE: greyscale() rounds instead of truncating, so many pixels come out 1 level brighter. color([{ apply: "greyscale" }]) now matches greyscale() (Rec. 709 luminance) instead of HSL lightness. threshold (with the default autoGreyscale) and otsuThreshold change to match. pHash()/hash() are unchanged.

🤖 Generated with Claude Code

…or()

- greyscale() rounds the Rec. 709 luminance to the nearest integer instead
  of truncating it when writing into the buffer. The weights' floating
  point sum is a little under 1, so 62 of the 256 greys (5, 7, 10, ...)
  lost a level every time; greyscale() is now idempotent.
- color([{ apply: "greyscale" }]) gives exactly the same result as
  greyscale() instead of tinycolor's greyscale (HSL lightness). desaturate
  is unchanged: it scales HSL saturation, so desaturate 100 still gives the
  HSL lightness; the JSDoc now says so.
- otsuThreshold rounds the luminance like greyscale(), so it still matches
  greyscale() followed by otsuThreshold({ autoGreyscale: false }).
- pHash keeps the truncated luminance so stored hashes don't change, and no
  longer depends on plugin-color.

BREAKING CHANGE: greyscale(), color() with "greyscale", threshold (which
greyscales by default) and otsuThreshold give different output. pHash and
hash() output is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hipstersmoothie hipstersmoothie added bug there is a bug in the way jimp behaves major Increment the major version when merged labels Nov 23, 2026
@hipstersmoothie
hipstersmoothie merged commit 4f6db79 into main Nov 23, 2026
6 checks passed
@hipstersmoothie
hipstersmoothie deleted the sesh/jimp-2-0-greyscale-consistency branch November 23, 2026 18:44
@hipstersmoothie
Copy link
Copy Markdown
Collaborator Author

🚀 PR was released in v2.0.0 🚀

@hipstersmoothie hipstersmoothie added the released This issue/pull request has been released. label Nov 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug there is a bug in the way jimp behaves major Increment the major version when merged released This issue/pull request has been released.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant

0