Image Color Palette Extractor

Upload a photo and this tool extracts its dominant colors automatically, using histogram-based color quantization — a ready swatch palette with hex codes, computed entirely in your browser, with nothing ever uploaded to a server.

246 views

Upload a photo to extract its dominant colors — everything runs in your browser.

Why You Can't Just Count Pixels

The obvious way to find a photo's dominant colors sounds simple: count how many pixels are each exact color, and keep whichever colors appear most often. In practice this fails almost immediately. A digital photo of a blue sky, for instance, is not one flat blue — it's thousands of subtly different blues, shifting pixel by pixel because of sensor noise, natural light gradients, and JPEG compression artifacts. Two adjacent sky pixels might be rgb(91, 148, 214) and rgb(92, 149, 213): visually identical, numerically distinct. Counted as exact values, neither is "the most common color" — each might appear only once or twice, buried among thousands of near-identical-but-technically-different blues. The naive approach returns noise, not a palette.

The fix is to stop counting exact pixel values and start counting buckets of similar colors. This tool divides each of the red, green and blue channels — normally ranging from 0 to 255 — into 8 equal bands of 32 values, using Math.floor(value / 32) * 32 on every channel. That turns 16.7 million possible RGB combinations into a manageable grid of 8 × 8 × 8 = 512 buckets. Every pixel in the resized image lands in exactly one bucket based on which band its red, green and blue values fall into, and a running tally is kept — using a plain object as a hash map — of how many pixels land in each one, plus a running sum of their actual red, green and blue values.

From Buckets to a Palette

Once every pixel has been counted, the buckets are sorted by population and the 6 to 8 most populous ones are kept. But the color shown for each bucket is not simply the bucket's numeric boundary (say, "224-255 red, 128-159 green, 0-31 blue") — that would just be a somewhat arbitrary corner of a 32×32×32 cube. Instead, this tool computes the true average red, green and blue value of every pixel that actually landed inside that bucket, which is much closer to what the eye perceives as the representative color of that region of the image.

This histogram-binning approach is a deliberately simple form of color quantization — reducing a large set of colors to a small representative set. A more sophisticated classic alternative is median-cut, defined by Paul Heckbert in his 1980 master's thesis at MIT, which recursively splits the color space along its widest dimension rather than using fixed-size boxes; it's still the technique behind how GIF images are reduced to a 256-color palette. Median-cut generally produces more perceptually accurate palettes for complex images, at the cost of more implementation complexity. Fixed-grid histogram binning, used here, is simpler to reason about and fast enough to run instantly in the browser, which is the trade-off this tool makes.

Before any counting happens, the uploaded image is first scaled down so its width doesn't exceed 200 pixels, using the HTML5 canvas API. This cuts the pixels that need to be read and bucketed by a large factor, and barely affects the resulting palette, since color buckets track broad regions of hue, not fine detail. The entire process — drawing the image, resizing it, reading pixel data with getImageData(), and computing bucket averages — happens locally in the browser's memory. The photo is never uploaded to any server.

Frequently Asked Questions

Why doesn't this tool just find the single most common color?

Because in real photographs almost no two pixels share the exact same RGB value — natural light, sensor noise and compression mean colors that look identical to the eye are numerically slightly different, so no exact value repeats often enough to be meaningful. Grouping nearby colors into buckets first is what makes the counting work.

How many colors does the palette show?

Up to 8 — the tool groups all pixels into 512 possible color buckets (8 levels each for red, green and blue), then shows the 6 to 8 buckets containing the most pixels. A very simple, mostly-flat image may return fewer if only a handful of buckets are populated at all.

Is this the same technique used to make GIF images?

Not exactly, though it's a relative. GIFs are usually reduced to 256 colors using median-cut, a more advanced quantization algorithm from 1980 that recursively splits the color space rather than using a fixed grid. This tool uses simpler fixed-size histogram binning, which is faster to compute and easier to reason about, at some cost to perceptual accuracy on complex images.

Does uploading a photo here send it to a server?

No — the image is decoded and drawn to a canvas element entirely inside your browser, and every pixel is read and analyzed locally with the canvas getImageData() API. Nothing about the photo is ever transmitted anywhere.

Why is the image resized before analyzing it?

Purely for speed. Reading and bucketing every pixel of a full-resolution photo could mean processing tens of millions of values; scaling the image down to a maximum width of 200px first cuts that dramatically while barely changing the outcome, since dominant colors are large regions, not fine detail.

Comments

No comments yet — be the first to write one!

Similar Tools