Skip to main content
BVDART Logo
← All guidesDithering & palettes

Palette extraction

Palette extraction summarizes an image with a small collection of colors. Different selection methods preserve different aspects of the source.

Decide which colors deserve a palette slot

Botanical label · a small warm accent40 × 30 px
Botanical label with an adjustable orange flower
Count the source colors
27.2%
26.9%
26.8%
13.5%
3.6%
2.0%

The flower is the focal point, yet occupies few pixels. Popularity can spend every palette slot on the three similar paper shades. Grow the flower to change that tradeoff.

Keep the most frequent
Split and average color groups
What happens to the orange accent?
Image recolored with the chosen extraction

#e5632e → #b8b59b

Original accent → nearest available representative. Median cut can produce a new average color; it need not preserve the accent exactly.

Grow the orange flower at a fixed color budget until popularity gives the accent a palette slot. Use any control to take over.

What to try
  1. Compare the popularity and median-cut palettes with the same color budget.
  2. Grow the orange flower and watch its pixel population and chosen representative.
Open in the Lab ↗

What to look for: Frequency and representation are different goals. Similar background colors can crowd out a small accent, while group averages may create a new color.

How it works

A frequent color is not necessarily the most useful representative. A large neutral background can dominate a popularity palette and crowd out a small vivid accent. Methods that partition color space can allocate representatives to less common regions instead.

Palette size controls a tradeoff between fidelity and constraint. A larger palette can preserve more variation, but an intentional small palette can bring unity to otherwise unrelated source material. Extraction and assignment are separate: after choosing colors, the image still needs a rule for using them.

Use it in the Lab

Extract colors from a layer in the Lab’s drawing controls when choosing a painting palette. Use Indexed palette when the goal is to recolor the entire layer using a reduced palette.

Limits & distinctions

An extracted palette is a summary of the chosen image and sampling strategy. It is not automatically a harmonious palette, an accessible UI theme or a print color specification.

Further reading

Palette extraction is a fundamental technique in computer vision and image processing that reduces the complexity of an image's color space while preserving its visual essence. By analyzing thousands or millions of pixel colors and distilling them into a small set of representative hues, these algorithms enable applications ranging from image compression to artistic style transfer.

K-Means Clustering

K-means clustering is one of the most popular approaches to palette extraction. The algorithm treats each pixel as a point in 3D color space (typically RGB or LAB) and iteratively groups them into k clusters, where k is the desired number of colors in the final palette.

The process begins by randomly initializing k cluster centroids in color space. Each pixel is then assigned to its nearest centroid, and the centroids are recalculated as the mean of all assigned pixels. This assignment and update cycle repeats until convergence, typically when centroids move less than a threshold distance between iterations.

Initialization Matters

K-means is sensitive to initial centroid placement. Using k-means++ initialization, which spreads initial centroids apart, typically produces better results than random initialization and converges faster.

The primary advantage of k-means is its simplicity and speed, especially with optimizations like mini-batch k-means. However, it has notable limitations: it assumes spherical clusters of similar size, can get stuck in local minima, and weights colors purely by frequency rather than perceptual importance.

Median-Cut Algorithm

The median-cut algorithm takes a different approach by recursively subdividing the color space. It begins by placing all pixels into a single box in RGB space, then repeatedly splits boxes along their longest dimension at the median value until the desired number of colors is reached.

At each iteration, the algorithm identifies the box with the greatest range along any color channel, finds the median pixel value along that dimension, and splits the box into two at that point. This ensures that boxes with greater color variation are subdivided first, naturally allocating more palette entries to regions of the image with richer color diversity.

Median-cut is deterministic and fast, making it ideal for real-time applications. It tends to produce more evenly distributed palettes than k-means and handles images with distinct color regions well. However, it can oversample sparse regions of color space and may miss subtle color variations in densely populated areas.

Octree Quantization

Octree quantization builds a tree structure where each node represents a cubic region of RGB color space. The root represents the entire color cube, and each node can have up to eight children representing octants of its parent's space. Colors are inserted into the tree by traversing from root to leaf, creating nodes as needed.

To reduce the palette to k colors, the algorithm prunes the tree by merging leaf nodes with their siblings, starting from the deepest levels. Each node maintains a count of pixels it represents, allowing the algorithm to preserve frequently occurring colors while merging rare ones. The final palette consists of the colors represented by the remaining leaf nodes.

Octree quantization excels at incremental processing—colors can be added one at a time, making it ideal for streaming applications or when memory is constrained. The tree structure also enables efficient color lookup during image reconstruction.

The octree approach balances spatial distribution with frequency weighting more naturally than median-cut. However, it requires more memory than other methods and can be slower for very large images. The hierarchical structure also means that color boundaries align with octant divisions, which may not match perceptual color differences.

Perceptual Color Distance Metrics

A critical consideration in palette extraction is how to measure color similarity. Euclidean distance in RGB space is computationally efficient but doesn't align with human color perception—a small change in blue may be more noticeable than a larger change in green.

The CIELAB color space addresses this by encoding colors in a perceptually uniform manner. In LAB space, Euclidean distance approximates perceived color difference. The CIEDE2000 formula refines this further with corrections for lightness, chroma, and hue, providing the most accurate perceptual distance metric available.

Using perceptual metrics significantly improves palette quality. K-means clustering in LAB space produces palettes that better represent how humans perceive the image's color distribution. However, this comes at a computational cost—LAB conversions and CIEDE2000 calculations are more expensive than RGB operations.

Balancing Frequency and Perceptual Importance

Pure frequency-based extraction can produce unsatisfying results. A large blue sky might dominate the palette with multiple similar blues while missing a small but visually important red flower. Conversely, giving equal weight to all colors regardless of frequency can result in palettes that don't represent the image's overall character.

Several strategies address this tension:

  • Saliency weighting uses computer vision techniques to identify visually important regions and weight their colors more heavily during extraction.
  • Saturation boosting increases the influence of highly saturated colors, which tend to be more visually striking even when rare.
  • Hybrid approaches combine multiple extraction methods, using frequency-based clustering for dominant colors and perceptual sampling for accent colors.
  • Minimum distance constraints ensure that palette colors are sufficiently distinct in perceptual space, preventing near-duplicate entries.

The optimal balance depends on the application. Image compression prioritizes frequency to minimize reconstruction error, while design tools may emphasize perceptual diversity to provide useful creative options.

Applications in Color Harmony Analysis

Extracted palettes serve as the foundation for analyzing color harmony in images. By mapping palette colors onto a color wheel, algorithms can identify harmonic relationships such as complementary, analogous, triadic, or split-complementary schemes.

This analysis enables automated aesthetic evaluation, helping photographers and designers understand why certain images feel balanced or discordant. It also powers recommendation systems that suggest images with similar color moods or generate complementary color schemes for design projects.

Beyond Traditional Harmony

Modern approaches use machine learning to learn harmony rules from large datasets of aesthetically pleasing images, discovering patterns that go beyond classical color theory's rigid rules.

Style Transfer Applications

In neural style transfer and traditional color transfer algorithms, palette extraction plays a crucial role in separating color from content. By extracting palettes from both source and target images, algorithms can transfer the color mood of one image to another while preserving structural details.

The process typically involves matching colors from the source palette to the target palette, then remapping each pixel to its corresponding target color. More sophisticated approaches use histogram matching in LAB space or learn non-linear color transformations that preserve local contrast and texture.

Palette-based style transfer is computationally lighter than full neural style transfer and provides more direct control over the color transformation. This makes it valuable for real-time applications, mobile devices, and scenarios where artists need predictable, tweakable results.

Algorithm Trade-offs

Choosing the right palette extraction algorithm requires understanding the trade-offs between speed, quality, and determinism. K-means offers high-quality results but requires multiple iterations and can produce different outputs on repeated runs. Median-cut is fast and deterministic but may miss subtle color variations. Octree quantization balances these concerns while enabling incremental processing.

For production systems, hybrid approaches often work best: use median-cut or octree for initial extraction, then refine with a few iterations of k-means in LAB space. This combines the speed of spatial subdivision with the perceptual quality of clustering, producing palettes that are both representative and visually pleasing.

Perceptual Distance in LAB

ΔE = √((L₂-L₁)² + (a₂-a₁)² + (b₂-b₁)²) Where L represents lightness, a represents green-red axis, and b represents blue-yellow axis. A ΔE of 1.0 represents the smallest color difference the human eye can perceive.

Sources & references

Guide updated