Color Quantization: From Millions to Dozens of Colors
The Core Challenge
A 24-bit RGB image can contain up to 16,777,216 unique colors. But many artistic and technical contexts demand far fewer: 256 for GIF, 16 for classic pixel art, sometimes as few as 2 for monochrome prints. Color quantization is the science — and art — of choosing which handful of colors best represents all 16.7 million possibilities.
The naive approach of evenly dividing the RGB cube into bins produces terrible results. Most images use only a small region of the full color space, so uniform division wastes palette slots on colors that don't appear in the image at all.
Median Cut: Divide and Conquer
Paul Heckbert's Median Cut algorithm (1982) takes a spatial approach. It treats every pixel as a point in 3D RGB space, then repeatedly splits the point cloud along its longest axis at the median. After n splits, you have 2ⁿ clusters. The average color of each cluster becomes a palette entry.
Median Cut is elegant because it naturally adapts to the image. A sunset photograph, dominated by reds and oranges, will allocate most palette entries to warm hues — exactly where the detail matters. The blue sky gets fewer entries because it's more uniform.
K-Means Clustering
K-Means approaches the problem iteratively. Start with k random "centroids" in RGB space. Assign each pixel to its nearest centroid (using Euclidean distance). Recalculate each centroid as the average of its assigned pixels. Repeat until convergence.
K-Means often finds better palettes than Median Cut because it optimizes globally, but it's slower and sensitive to initialization. In practice, combining Median Cut initialization with K-Means refinement gives the best results — the speed of divide-and-conquer with the polish of iterative optimization.
The Distance Problem
Every quantization algorithm relies on measuring distance between colors. The naive Euclidean distance in RGB space treats red, green, and blue as equally important, but human vision doesn't. We're far more sensitive to changes in green (luminance) than in blue.
Better quantization uses perceptual distance metrics — working in CIELAB space where Euclidean distance approximates perceptual difference, or applying luminance weighting to RGB distances. This single change can dramatically improve palette quality without changing the algorithm at all.
Octree Quantization
For real-time applications, the octree method builds a tree structure that recursively subdivides the RGB cube into eight octants. Leaf nodes accumulate pixel counts, and the tree is pruned from the bottom up — merging the least populated nodes first — until the desired palette size is reached.
Octrees are memory-efficient and can be built in a single pass over the image. They're the algorithm of choice for many practical implementations, including early web browsers' GIF rendering.
Quantization in Practice
In the Lab's Indexed Color Studio, you can compare these algorithms side by side. Generate a noisy gradient, then quantize it to 4, 8, 16, or 32 colors with each method. The differences are striking: Median Cut preserves structure, K-Means optimizes smoothness, and octrees balance speed with quality.
Combined with dithering, even a 16-color palette can reproduce photographic detail. The quantization algorithm chooses which 16 colors; the dithering algorithm decides how to arrange them. Together, they turn severe constraints into creative opportunities.
Gerelateerde concepten
Gerelateerde artikelen
Dithering: Creating Illusions with Mathematics
When you have only a handful of colors, clever pixel placement can trick the eye into seeing thousands. A deep dive into error-diffusion dithering and the algorithms behind the illusion.
10 feb 2026
The Beauty of Restricted Palettes
Why limiting colors to 8, 16, or 32 produces more compelling art than millions of colors ever could.
20 jan 2025
Probeer in het Lab
Verken Gerelateerde Secties
Gebruik deze secties om kunstwerken te ontdekken, technische context te lezen en het volledige ecosysteem van algoritmische kunst te verkennen.
