A 1,000 × 1,000 photo has a million pixels, and often tens of thousands of distinct colors. Pulling five or ten swatches out of it is a quantization problem: group similar pixels, choose one color to represent each group, and decide which groups matter most. Each step involves a choice, and those choices are why two tools give different palettes for the same image.

A palette is not the dominant color

There are two common ways to get one color from an image, and neither gives a palette.

  • The average of all pixels. Averaging a colorful photo produces a muted gray-brown or gray-blue that usually appears nowhere in the image. It can work as a placeholder background while the image loads, but it does not describe the image.
  • The most frequent color. This is usually the background: sky, a white studio wall, a dark stage. It describes the largest area, not the subject.

A palette keeps several separate groups, including small ones that stand out, such as a red umbrella on a beach. Ordering then becomes a separate decision. Sorting by area puts the sky first. Sorting by saturation puts the umbrella first. Neither order is wrong, but they suggest very different designs.

Common quantization approaches

Most tools use one of three families of algorithm, often with changes of their own.

Fixed grid (histogram buckets)

Divide the RGB cube into equal cells, for example 16 steps per channel, and count the pixels in each cell. It is fast and deterministic. The weakness is that the cell boundaries are arbitrary: two nearly identical colors on either side of a boundary land in different cells, while a cell can mix colors that look different. Grid methods usually add a merge step afterward for this reason.

Median cut

Put a box around all the colors in the image. Find the channel with the widest range, sort the colors along it, and split the box at the median so each half holds about the same number of pixels. Keep splitting boxes, one at a time, until you have as many as you want colors, then average each box. Implementations differ in which box they split next. Because splits follow the data, crowded regions of color space get more boxes than empty ones. A large sky can produce several near-identical blues, while a small vivid detail gets averaged into a neighboring box.

k-means clustering

Choose k starting colors, assign each pixel to the nearest one, move each center to the mean of its pixels, and repeat until the centers stop moving. The result depends on k and on the starting points, so two runs with random starts can give different palettes unless the starts are fixed. It is slower than the other two, so tools usually run it on a downscaled image or a random sample of pixels.

For all three, the color space matters as much as the algorithm. Distance in RGB does not match how different two colors look. A perceptual space such as CIELAB or Oklab groups colors closer to how people see them. Many fast tools still measure distance in RGB, and that has visible effects, as the next section shows.

What GradientDeck's picker runs

GradientDeck's color picker from image uses the open-source extract-colors library (version 4.1) with its default options, in your browser. Uploaded files are read from a local object URL and are not sent to a server. Reading the library's source, the process works like this:

  1. The image is drawn onto a canvas scaled down to about 64,000 pixels, keeping its aspect ratio. Smaller images are used at full size.
  2. Pixels with alpha of 250 or less are skipped, so the transparent background of a PNG logo does not become a swatch.
  3. Each pixel is put in a bucket keyed by the top 4 bits of each channel, which gives 16 levels per channel and up to 4,096 buckets. This is a fixed-grid step. Each bucket is represented by its most common exact color, weighted by the bucket's total pixel count.
  4. Starting from the largest bucket, any remaining bucket whose RGB distance is under 0.22 is merged into it. Distance is the sum of the absolute red, green, and blue differences divided by 765, so 1 is the distance from black to white.
  5. The survivors are grouped again when they are close in hue (within 1/12 of the color wheel, 30 degrees), saturation, and lightness (each within 0.2). Each group becomes the plain average of its members, so a swatch can be a color that does not appear exactly in the image.
  6. The library ranks the results with a score that favors vivid, mid-lightness colors. GradientDeck then re-sorts them by area, the share of pixels each one represents, and shows up to ten.

A worked example

To see the effect of these steps, we ran extract-colors 4.1 with default options on a synthetic 400 × 300 test image: a sky fading from #2b6cb0 to #a3cfee across the top two thirds, noisy sand (#e2c79a) below, a small red umbrella (#d62828, under 1% of the image), and a dark green palm tree (#2f5d3a, about 3%).

  • The result had four colors. Sorted by area, as GradientDeck shows them: #3171b3 (45.5%), #e5ce92 (35.4%), #92c1e5 (18.3%), and #d62828 (0.8%).
  • The sky gradient became two swatches, a deep blue and a pale blue. The sand's noise collapsed into one color.
  • The red umbrella survived as its own swatch despite its tiny area, because it is far from everything else. The library's own ranking put it first; sorting by area puts it last.
  • The palm tree disappeared. Its bucket was smaller than the largest sky bucket (#3171b3), and the RGB distance between them is (2 + 20 + 121) / 765 ≈ 0.19. That is under the 0.22 merge threshold, so the palm was absorbed into the blue group. To the eye, dark green and medium blue are clearly different colors. RGB distance does not see it that way.
  • For comparison, the plain average of every pixel was #8fa8b6, a gray-blue that matches no part of the image.

Extracted swatches, widths proportional to area (the red is widened to be visible).

So if a color you can clearly see is missing from the swatches, it was probably merged into a larger group nearby in RGB. Crop the image to the area you care about and extract again.

Why different tools disagree

  • Algorithm: grid, median cut, k-means, or a mix, each with its own merge rules.
  • Color space: RGB distances and Lab or Oklab distances group colors differently.
  • Sampling: how far the image is downscaled, or which random pixels are sampled.
  • Number of colors: some tools always return exactly five; GradientDeck shows however many groups remain, up to ten.
  • Ordering: by area, by saturation, or by a combined score.
  • Representative color: the mean of a group, its most common color, or the pixel nearest the mean. Means can produce colors that are not in the image.

From extracted colors to a UI palette

Extracted swatches describe a photo. An interface needs neutrals, an action color, and text colors that meet contrast requirements. Work through the swatches by job:

  1. Drop near-duplicates. Two blues from the same sky gradient are one hue at two lightness levels; keep one and plan to generate the steps yourself.
  2. Find the candidate primary: a saturated color of medium lightness, often a small-area swatch like the umbrella red. Large-area swatches are rarely good action colors; they are the backdrop.
  3. Derive neutrals rather than using the extracted ones directly. Take the hue of the dominant swatch and build a low-saturation gray scale from it, so backgrounds relate to the image without competing with it.
  4. Pick or derive a text color that is very dark (or very light for a dark theme). Extracted mid-tones are almost never dark enough for body text.
  5. Adjust lightness, not hue, when a pair fails contrast. Darkening the red a few steps keeps the connection to the photo; switching to a different red loses it.

Before committing, list the pairs that will actually meet on screen and measure each one. This snippet checks each extracted color against white and near-black text and reports which reaches 4.5:1:

js
const luminance = (hex) => {
  const [r, g, b] = [1, 3, 5].map((i) => {
    const c = parseInt(hex.slice(i, i + 2), 16) / 255;
    return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
  });
  return 0.2126 * r + 0.7152 * g + 0.0722 * b;
};
const ratio = (a, b) => {
  const [hi, lo] = [luminance(a), luminance(b)].sort((x, y) => y - x);
  return (hi + 0.05) / (lo + 0.05);
};

const extracted = ["#3171b3", "#e5ce92", "#92c1e5", "#d62828"];

for (const color of extracted) {
  const onWhite = ratio(color, "#ffffff");
  const onDark = ratio(color, "#111111");
  console.log(color,
    "white text:", onWhite.toFixed(2), onWhite >= 4.5 ? "ok" : "no",
    "dark text:", onDark.toFixed(2), onDark >= 4.5 ? "ok" : "no");
}

For the example, #3171b3 and #d62828 can carry white text (about 5.1:1 and 5.0:1). The sand and pale blue need dark text, which makes them candidates for tinted surfaces rather than buttons.

For mapping these colors onto surface, border, text, and state tokens, see Turning a color palette into UI color tokens. For contrast over images and gradients, see Contrast, in context.

Common mistakes

  • Using the largest swatch as the brand color. It is usually sky, wall, or shadow.
  • Assuming a missing color was not detected. It was likely merged into a larger, nearby group; crop and extract again.
  • Using extracted swatches as text colors without measuring them.
  • Extracting from a heavily compressed thumbnail. Compression artifacts and resizing blur edges and can add in-between colors.
  • Expecting identical results from a different tool, or from the same photo at a different crop.

Using the GradientDeck color picker from image

Upload an image (PNG, JPG, WebP, GIF, or another format your browser can decode, chosen with the file picker or dropped onto the upload area) or search Unsplash photos in the second tab. The extracted swatches appear in area order. A HEX, RGB, or HSL toggle sets the copy format, and selecting a swatch copies its value. Export palette provides CSS custom properties, CSS and Tailwind gradients, a mesh gradient, a PNG, and an SVG. The actions menu opens the colors in the palette visualizer or the palette generator, finds similar palettes, shows lighter and darker variations, or opens a full-screen view. Below the picker, similar palettes from GradientDeck's curated collection are listed when their colors are close to the extracted ones.

References

extract-colors (library used by GradientDeck)

MDN: CanvasRenderingContext2D.getImageData()

W3C: Understanding SC 1.4.3 Contrast (Minimum)