8000 feat: palette() and dominantColor() extract an image's main colors by hipstersmoothie · Pull Request #1569 · jimp-dev/jimp · GitHub
[go: up one dir, main page]

Skip to content

feat: palette() and dominantColor() extract an image's main colors - #1569

Merged
hipstersmoothie merged 1 commit into
mainfrom
sesh/dominant-color-palette
Nov 25, 2026
Merged

hipstersmoothie merged 1 commit into
mainfrom
sesh/dominant-color-palette

Conversation

@hipstersmoothie
Copy link
Copy Markdown
Collaborator

Adds image.palette(count, options?) and image.dominantColor(options?) to @jimp/plugin-quantize, for theming things after a picture, such as OG cards whose background or accent color comes from album art or an avatar.

API

image.palette(count: number, options?: { sampleSize?: number }): { color: number; weight: number }[]
image.dominantColor(options?: { sampleSize?: number }): number | undefined
  • palette returns up to count colors (1–256), sorted by weight, largest first. color is an opaque RGBA hex number, like getPixelColor returns. weight is the share of the image the color covers, and the weights add up to 1. It returns fewer colors if the image has fewer, and [] if it's fully transparent.
  • dominantColor is palette(5)[0].color, or undefined for a fully transparent image.
  • PaletteColor and PaletteOptions are exported from @jimp/plugin-quantize.
  • Invalid input throws a ZodError: count must be at least 1, count must be a whole number, count must be at most 256, sampleSize must be at least 1.

How it works

  • Reuses image-q's Wu quantizer, the same one quantize uses by default. It runs at 4 bits per channel instead of image-q's default of 5. Its 4D tables take about 80ms to set up at 5 bits whatever the image size, and a few ms at 4. The returned colors aren't rounded to 4 bits: each is the mean of the pixels in its cluster.
  • Sampling: at most sampleSize pixels (default 10,000), on an even grid. A warm call takes about 6–15ms in Node, whether the image is 128×128 or 10000×783.
  • Alpha: fully transparent pixels are skipped. Wu gets 1–4 copies of each pixel depending on its alpha, so the clusters are weighted by alpha, and every visible pixel still gets at least one copy so a faint color can't tint a nearby cluster. Then every pixel is matched to its nearest cluster, and each cluster's mean color and weight are computed using exact alpha.

apply / server / worker

Both methods return values, so they're handled like hash:

  • apply()'s JimpEdit type already excludes them, and at runtime it throws "palette" returns a value instead of changing the image. A test checks that the image is left unchanged.
  • @jimp/server: they aren't in defaultAllowedEdits, and allowing them explicitly still gives a 400 from apply. Tests cover both.
  • @jimp/worker: a type test checks that run({ edits: [["dominantColor"]] }) is an error.

Docs

  • New method pages: methods/palette.mdx and methods/dominant-color.mdx, registered in data/methods.ts. Each has an OG card example built with @jimp/layout and a rendered image. I ran the examples to make those images.
  • New dominantColor sticker icon. palette already had one.
  • Cross-links from quantize, apply ("which methods can be used") and the "Methods that return values" table in chaining-and-async.

Testing

  • plugins/plugin-quantize: pnp 8000 m exec vitest run, 28 tests.
  • packages/jimp: tests plus typecheck, 271 tests.
  • packages/server: 143 tests.
  • packages/worker: node tests plus the types.test-d.ts typecheck.
  • Root pnpm build (browser bundle check OK) and pnpm lint.
  • Docs pnpm build: astro check gives 0 errors, the links on the new pages resolve, and I looked at both OG cards.

🤖 Generated with Claude Code

…'s main colors

`image.palette(count, options?)` returns up to `count` colors, most common
first, as `{ color, weight }` with opaque RGBA hex colors (like
getPixelColor) and weights that add up to 1. `image.dominantColor(options?)`
returns the top color of a 5-cluster palette, or undefined for a fully
transparent image. Neither changes the image.

The colors are clustered with image-q's Wu quantizer, as `quantize` uses by
default, at 4 bits per channel so setup takes a few ms instead of ~80ms.
Pixels are sampled on an even grid (`sampleSize`, default 10000), so a call
takes about 10ms whatever the image size. Transparent pixels are skipped
and partially transparent ones are weighted by alpha, both in clustering
and in each cluster's mean color and weight.

They return values, so apply(), @jimp/server edits and @jimp/worker tasks
reject them, as for hash; tests cover all three. Docs pages with an OG card
example built on @jimp/layout.

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 e7381b7 into main Nov 25, 2026
7 checks passed
@hipstersmoothie
hipstersmoothie deleted the sesh/dominant-color-palette branch November 25, 2026 03:29
@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