ReCaseText
11 min read

Color Palette Extraction: How Algorithms Find the Dominant Colors in Any Image

Extracting a color palette from a photograph is a clustering problem in three-dimensional color space. Here's how the three main algorithms — histogram binning, median cut, and k-means — work, why they produce different results from the same image, and how to use extracted palettes in design.

Every photograph contains thousands or millions of distinct pixel colors, but the human eye perceives only a handful of dominant hues. A sunset photograph reads as orange, pink, purple, and deep blue — not as 2.4 million individual RGB values. Extracting a compact color palette from an image means answering the question: which small set of colors best represents this image? The answer depends on what "best represents" means, and different algorithms give different answers.

Color palette extraction is used across design, branding, data visualization, fashion, interior design, and web development. Designers extract palettes from photographs, artworks, and nature to inform color choices. Brand teams analyze competitor imagery to understand market color trends. Web developers pull palettes from hero images to generate coordinated UI themes. The common thread is reducing the overwhelming complexity of photographic color into an actionable set of swatches.

This article explains how the three most common extraction algorithms work — histogram binning, median cut, and k-means clustering — and what makes each one better or worse for different images and use cases.

The Color Space Problem

Every pixel in a standard 24-bit image has three color channels — red, green, and blue — each ranging from 0 to 255. This means there are 16,777,216 possible colors. A typical photograph uses hundreds of thousands of these. The goal of palette extraction is to reduce those hundreds of thousands of colors to a small set — usually 5 to 10 — that captures the visual character of the image.

This is fundamentally a clustering problem in three-dimensional space. Each pixel can be plotted as a point in an RGB cube where the three axes are red, green, and blue. The distribution of these points reveals the image's color structure — a landscape might have a dense cluster of greens (foliage), a cluster of blues (sky), and scattered browns and grays. The palette extraction algorithm's job is to identify these clusters and select a representative color for each one.

The choice of color space affects the results. RGB is the most common because it is the native representation of digital images, but it has a well-known problem: equal distances in RGB do not correspond to equal perceptual differences. A shift of 10 units in the green channel is more visually noticeable than the same shift in the blue channel. Alternative color spaces like CIE LAB are perceptually uniform — equal distances correspond to equal perceived color differences — which can produce more perceptually balanced palettes. However, the added complexity of color space conversion is often not justified for practical palette extraction, and RGB-based algorithms produce results that are good enough for most design applications.

Algorithm 1: Histogram Binning

The simplest approach divides the RGB color cube into a uniform grid of bins and counts how many pixels fall into each bin. If you divide each axis into 4 equal segments, you get a 4 by 4 by 4 grid with 64 bins. Each bin spans a range of 64 color values per channel. After counting, you sort the bins by population and select the most populated ones. The representative color for each bin is the average of all pixels that fell into it.

The advantage of histogram binning is simplicity and speed. The algorithm makes a single pass through the pixel data, incrementing a counter for each pixel's bin. It runs in linear time and requires minimal memory.

The disadvantages are significant. The grid is uniform and does not adapt to the image's color distribution. A bin that spans a heavily populated region of color space gets the same treatment as a bin that spans an empty region. If two visually distinct color clusters happen to fall in the same bin (because they are close in RGB space), they get merged into a single average that may not match either cluster. Conversely, a single visual cluster that spans a bin boundary gets split into two separate entries.

Increasing the grid resolution helps with the merging problem but creates the opposite issue — too many bins, most of them sparsely populated, making it harder to identify meaningful clusters. Decreasing the resolution causes more merging. There is no grid resolution that works well for all images, which is why more adaptive algorithms are preferred for quality palette extraction.

Algorithm 2: Median Cut

Median cut, originally developed by Paul Heckbert in 1982, improves on histogram binning by adapting the partitioning to the image's actual color distribution. Instead of a fixed grid, median cut recursively subdivides the color space along the axis with the greatest range of values.

The algorithm works as follows. Start with all pixels in a single box that spans the full RGB range. Find which color channel (red, green, or blue) has the greatest range of values across all pixels in the box. Sort the pixels by that channel's value and find the median. Split the box at the median, creating two boxes — one containing the darker half and one containing the lighter half. Repeat the process on the box with the greatest range, splitting it again at its median. Continue until you have the desired number of boxes (equal to the desired palette size).

The representative color for each final box is the average of all pixels it contains. Because the splitting is guided by the data — always dividing the most spread-out box along its most spread-out dimension — median cut naturally concentrates resolution where the color variation is greatest and avoids wasting resolution on empty or homogeneous regions.

Median cut typically produces more visually coherent palettes than histogram binning because the boxes adapt to the image's actual color clusters. A sunset with a tight cluster of oranges and a spread of blues will have more boxes allocated to the blue range (where the variation is greater) and fewer to the orange range (where the colors are similar), producing a palette that reflects the image's true color distribution rather than an arbitrary grid.

The main limitation of median cut is that it always splits at the median, which can bisect a natural color cluster if the cluster happens to be the most spread-out box at some iteration. This can produce two palette entries that are very similar and together represent what should have been a single color. In practice, this is rare enough that median cut remains one of the most widely recommended algorithms for palette extraction.

Algorithm 3: K-Means Clustering

K-means is the most mathematically rigorous of the three approaches. It directly solves the clustering problem by iteratively refining a set of K cluster centers (centroids) to minimize the total distance between each pixel and its nearest centroid.

The algorithm starts by placing K centroids at random positions in the RGB color space (or at positions chosen by a smarter initialization strategy like k-means++). Then it alternates between two steps. In the assignment step, every pixel is assigned to the nearest centroid based on Euclidean distance in RGB space. In the update step, each centroid is moved to the average position of all pixels assigned to it. These two steps repeat until the centroids stop moving (converge) or a maximum iteration count is reached.

K-means directly optimizes for the property we care about: each palette color is the best possible representative of all the pixels it claims, in the sense that it minimizes the sum of squared distances. This tends to produce the most visually accurate palettes — the extracted colors closely match the dominant hues as perceived by the viewer.

The disadvantages are computational cost and sensitivity to initialization. K-means requires multiple passes through the pixel data (typically 10 to 30 iterations), and each pass computes the distance from every pixel to every centroid. For a 12-megapixel image with K=10, that is 120 million distance calculations per iteration. On a sampled-down image (every tenth pixel, for example), this is fast enough for real-time use, but it is significantly slower than histogram binning or median cut.

The initialization sensitivity means that different random starting positions can produce different palettes from the same image. The standard mitigation is to run the algorithm multiple times with different random seeds and select the result with the lowest total error. K-means++ initialization (choosing initial centroids that are well-spread across the color space) also reduces this problem significantly.

Our color palette extractor uses the median cut algorithm, which provides the best balance of quality and performance for browser-based real-time extraction. It processes pixel data from the Canvas API's getImageData without any server upload.

Sampling for Performance

A 12-megapixel image contains 12 million pixels. Processing every pixel through a clustering algorithm is unnecessary because the color distribution of a 10 percent sample is statistically indistinguishable from the full image's distribution. All practical palette extraction tools sample the pixel data before analysis — typically using every Nth pixel, or resizing the image to a small working resolution (200 by 200 is common) before extracting pixel data.

This sampling has negligible impact on palette quality. The dominant colors in an image are dominant precisely because they appear in many pixels — any reasonable sample will capture them. Sampling does reduce the ability to detect rare but visually important accent colors (a small red flower in a field of green, for example), but for palettes of 5 to 10 colors, the dominant hues are almost always the ones you want.

In the browser, sampling is implemented by either reading every Nth entry from the getImageData array or by drawing the image onto a small canvas (which handles the downsampling through the browser's built-in image scaling) and reading all pixels from the small canvas. The latter approach is simpler to implement and lets the browser handle the resampling optimally.

Color Representation: HEX, RGB, and HSL

Extracted palette colors are typically displayed in multiple formats for different use cases. The three most common are HEX, RGB, and HSL.

HEX is the standard web color format — a six-character string like #E64A19 that encodes the red, green, and blue channels as two hexadecimal digits each. HEX is used in CSS, HTML, and most design tools. It is compact and unambiguous.

RGB represents the same information as three decimal numbers — rgb(230, 74, 25) in CSS notation. RGB is more readable than HEX when you need to understand the channel balance of a color (which channel dominates, how saturated the color is). It is also the native format for Canvas pixel data and most programming language color libraries.

HSL (Hue, Saturation, Lightness) represents color in a way that corresponds more closely to human perception. Hue is the color's position on the color wheel (0 to 360 degrees), saturation is how vivid the color is (0 to 100 percent), and lightness is how bright it is (0 to 100 percent). HSL is particularly useful for designers because it makes relationships between colors intuitive — analogous colors have similar hues, complementary colors have hues 180 degrees apart, and you can create lighter or darker variants by adjusting only the lightness value.

Our color palette extractor displays extracted colors in all three formats, and our image color picker provides detailed HEX, RGB, and HSL values for any pixel in an image. For a deeper understanding of these color models and how they relate to each other, see our article on understanding HEX, RGB, and HSL color models.

Using Extracted Palettes in Design

A raw extracted palette is a starting point, not a finished design system. The colors pulled from a photograph are descriptive — they tell you what colors exist in the image. Turning them into a functional design palette requires a few additional steps.

Identify roles. In any design system, colors serve specific purposes: a primary action color, a background color, a text color, accent colors for highlights and alerts, and neutral colors for borders and subtle UI elements. Map your extracted colors to these roles based on their saturation and lightness. Highly saturated colors work as accents. Desaturated, light colors work as backgrounds. Dark, low-saturation colors work as text.

Check contrast. Extracted colors may not meet accessibility contrast requirements when used together. The Web Content Accessibility Guidelines require a minimum contrast ratio of 4.5:1 for normal text and 3:1 for large text. Verify that your text and background color pairings meet these thresholds. The contrast ratio depends on relative luminance — the same luminance concept discussed in our article on grayscale conversion.

Expand the palette. A 5-color extracted palette often needs lighter and darker variants for practical use. Generate tints (lighter versions by mixing with white) and shades (darker versions by mixing with black) of each extracted color to create a full range that covers all UI needs.

Test in context. Colors look different when placed next to each other than they do in isolation. Simultaneous contrast — a perceptual phenomenon where a color appears to shift based on its surrounding colors — means that a palette that looks harmonious as swatches may need adjustment when applied to an actual layout.

Applications Beyond Design

Color palette extraction has applications well beyond visual design. In computer vision, color histograms and dominant color signatures are used for image retrieval — searching a database of images for ones with similar color profiles. In film and video production, palette analysis is used to ensure visual consistency across shots and scenes. In art history and conservation, palette extraction from digital reproductions helps identify pigments, date works, and detect forgeries based on anachronistic color chemistry.

In e-commerce, extracted palettes power "shop by color" features — automatically categorizing products by their dominant colors without manual tagging. In social media analytics, palette analysis across large image datasets reveals color trends, seasonal patterns, and brand visual identities.

In data visualization, extracted palettes provide naturally harmonious color schemes derived from real-world sources. A palette extracted from a coral reef photograph produces a vibrant, naturally balanced set of colors that works well for categorical data visualization — each color is distinct, and the overall impression is cohesive because the colors co-occur naturally.

The Bottom Line

Color palette extraction reduces the millions of colors in a photograph to a handful of representative swatches using clustering algorithms in three-dimensional color space. Histogram binning is fast but crude. Median cut adapts to the data and produces good results efficiently. K-means optimizes mathematically for the best possible representatives but costs more computation and depends on initialization. For practical browser-based extraction, median cut with pixel sampling provides the best balance of quality and speed. Our color palette extractor implements this approach entirely in your browser — upload an image, get a palette in HEX, RGB, and HSL, with no server processing and no data leaving your device.

References

Atomic Spin — A Tool for Extracting Color Palettes From Images — Excellent comparison of histogram, median cut, and k-means approaches with interactive visualizations in RGB color space.

scikit-learn — Color Quantization Using K-Means — Python implementation and explanation of k-means color quantization with before-and-after comparisons.

BVDART — Color Palette Extraction Algorithms — Overview of extraction algorithms with a focus on median cut and k-means, including discussion of perceptual color spaces.

Joel Carlson — Exploring the Median Cut Algorithm with R — Step-by-step walkthrough of the median cut algorithm with R code and visualizations of the recursive splitting process.

Figma — What Is Color Theory? — Comprehensive guide to color theory fundamentals relevant to applying extracted palettes in design practice.