Repository navigation
feat: palette() and dominantColor() extract an image's main colors - #1569
Merged
Merged
Conversation
…'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>
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.
Adds
image.palette(count, options?)andimage.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
palettereturns up tocountcolors (1–256), sorted byweight, largest first.coloris an opaque RGBA hex number, likegetPixelColorreturns.weightis 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.dominantColorispalette(5)[0].color, orundefinedfor a fully transparent image.PaletteColorandPaletteOptionsare exported from@jimp/plugin-quantize.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
quantizeuses 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.sampleSizepixels (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.apply / server / worker
Both methods return values, so they're handled like
hash:apply()'sJimpEdittype 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 indefaultAllowedEdits, and allowing them explicitly still gives a 400 fromapply. Tests cover both.@jimp/worker: a type test checks thatrun({ edits: [["dominantColor"]] })is an error.Docs
methods/palette.mdxandmethods/dominant-color.mdx, registered indata/methods.ts. Each has an OG card example built with@jimp/layoutand a rendered image. I ran the examples to make those images.dominantColorsticker icon.palettealready had one.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 thetypes.test-d.tstypecheck.pnpm build(browser bundle check OK) andpnpm lint.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