8000 feat: gradientMap() for duotones and gradient maps by hipstersmoothie · Pull Request #1564 · jimp-dev/jimp · GitHub
[go: up one dir, main page]

Skip to content

feat: gradientMap() for duotones and gradient maps - #1564

Merged
hipstersmoothie merged 2 commits into
mainfrom
sesh/gradient-map-duotone
Nov 25, 2026
Merged

hipstersmoothie merged 2 commits into
mainfrom
sesh/gradient-map-duotone

Conversation

@hipstersmoothie
Copy link
Copy Markdown
Collaborator

Adds image.gradientMap(): each pixel's luminance is mapped through a list of colour stops. Two stops give a duotone, the Spotify-style look for OG images.

image.gradientMap(["#1e3264", "#f037a5"]); // duotone
image.gradientMap({
  stops: [
    { offset: 0.3, color: "#0b3d2e" },
    { offset: 0.7, color: "#c6f432" },
  ],
  amount: 0.5,
});

Changes

refactor: share gradient stops (first commit, so the grain/gradient-dithering branch can rebase onto it)

  • ColorSchema/Color, GradientStopSchema/GradientStop, GradientStopsSchema/GradientStops, normalizeStops, resolveColor and gradientTable move from plugin-draw/src/paint.ts to a new @jimp/utils file, gradient.ts. The code is the same; gradientTable now takes a ColorSpace and a size. plugin-draw re-exports the types, so jimp's exports don't change.
  • plugin-draw, @jimp/layout and gradientMap now all take the same stops: { offset, color }, or bare colours spread out evenly. Layout already allowed bare colours; draw gradients now accept them too. Layout's stop colours now also accept { r, g, b, a }, and its stop objects are no longer strict.
  • @jimp/utils gains a zod dependency (lockfile updated).

feat(plugin-color): add gradientMap

  • New method in plugin-color, next to sepia. Options are a zod schema (GradientMapOptions, exported from jimp). Invalid colours throw Invalid color "...".
  • apply() and @jimp/worker need no registration: they call any plugin method. @jimp/server has an allowlist, so gradientMap is added to defaultAllowedEdits.
  • Docs: a methods/gradient-map.mdx page with example images (made by scripts/gradient-map-images.mjs), a playground op (shadows / optional midtone / highlights / amount), a methods.ts entry, a note in the drawing concepts page that bare-colour stops are accepted, and the custom-jimp plugin table.

Design decisions

  • Luminance: the same Rec. 709 luma greyscale() uses (on the stored, gamma-encoded values, rounded). So gradientMap(img) equals gradientMap(img.greyscale()), and black→white on a grey ramp leaves it unchanged (both are tested).
  • Interpolation: in the image's stored colour space (gamma encoded) with premultiplied alpha, through the same gradientTable as plugin-draw. That matches CSS gradients' default and Jimp's drawn gradients. CSS colours are converted to the image's space (P3 is tested). Linear-light interpolation, as exposure uses, was the other option. I didn't take it, so that stops look like the same stops 8000 drawn as a gradient.
  • Alpha: each pixel keeps its alpha, multiplied by the gradient colour's alpha, so transparent stops make those tones transparent.
  • amount (0–1, default 1, clamped like sepia) linearly blends all four channels with the original.
  • No duotone method: the docs show duotone as a two-stop example.

Testing

  • pnpm exec vitest run in utils, plugin-draw, plugin-color, layout, server, worker and jimp (node + type tests) all pass. There are new pixel-exact tests and two image snapshots.
  • Root pnpm build passes, including the browser bundle check. pnpm lint passes. The docs astro check + build pass.

🤖 Generated with Claude Code

hipstersmoothie and others added 2 commits November 24, 2026 19:11
…utils

Move ColorSchema, GradientStop and gradient sampling from plugin-draw into
@jimp/utils so other plugins can describe and sample gradients the same
way. Stops can now be bare colors, spread out evenly, in plugin-draw
gradients as well as layout.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…maps

image.gradientMap(stops) replaces each pixel with the color at its
luminance along a gradient; two stops make a duotone. Stops are the shared
GradientStops that plugin-draw and layout gradients take. Luminance is the
Rec. 709 luma greyscale() uses, and colors are interpolated in the image's
stored (gamma encoded) space like CSS and plugin-draw gradients. Alpha is
kept, multiplied by the gradient's alpha, and amount blends with the
original.

It's in @jimp/server's default allowed edits, and has a docs page with a
playground op and example images.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@hipstersmoothie hipstersmoothie added the minor Increment the minor version when merged label Nov 25, 2026
@hipstersmoothie
hipstersmoothie merged commit acf4ada into main Nov 25, 2026
7 checks passed
@hipstersmoothie
hipstersmoothie deleted the sesh/gradient-map-duotone branch November 25, 2026 03:32
@hipstersmoothie
Copy link
Copy Markdown
Collaborator Author

🚀 PR was released in v2.2.0 🚀

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

Labels

minor Increment the minor 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