Repository navigation
fix(plugin-color)!: round greyscale and use the same luminance in color() - #1507
Merged
Merged
Conversation
…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>
Closed
Collaborator
Author
|
🚀 PR was released in |
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.
Grey-value bugs in
plugin-colorfound while writing the 2.0 docs, plus a check of every other place that computes grey.What was wrong
What changed
greyscale()sets r, g and b toMath.round(0.2126 * r + 0.7152 * g + 0.0722 * b), rounded to the nearest integer. Alpha is unchanged. Grey pixels keep their value, sogreyscale()is idempotent.color([{ apply: "greyscale" }])now uses the same rounded Rec. 709 luminance, so it gives exactly the same bytes asgreyscale(). In a chain it uses the current (possibly fractional) values and clamps to 0–255, like the other steps.desaturateis unchanged, on purpose. It scales HSL saturation, sodesaturate 100gives 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 CSSsaturate(), where full desaturation isn't Rec. 709 lumin 8000 ance either. Thecolor()JSDoc now spells this out.otsuThreshold(withautoGreyscale, the default) rounds the luminance the same way, so it still equalsgreyscale()followed byotsuThreshold(img, { autoGreyscale: false }).thresholdcallsgreyscale(), so it picks up the rounding automatically.hash()output is unchanged. pHash used to callgreyscale(), 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
greyscale(). Grey mask pixels are used as-is, so it's already idempotent and doesn't truncate.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 asgreyscale().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'sthreshold_otsugives 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-equivalencetest's tinycolor reference now uses the rounded luminance forgreyscale, and every other action is still checked against tinycolor. Added regression tests forgreyscale()(exact values, alpha kept, every grey 0–255 unchanged),color()greyscale vsgreyscale()on 4096 colours, and Otsu rounding ([5, 200]→ 6 with and withoutautoGreyscale). I also renamed thedesaturate 100test 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 matchesgreyscale()(Rec. 709 luminance) instead of HSL lightness.threshold(with the defaultautoGreyscale) andotsuThresholdchange to match.pHash()/hash()are unchanged.🤖 Generated with Claude Code