30

Color Quantization and Dithering

A photo holds millions of colors; a GIF holds at most 256, an e-ink badge maybe four, an old console two. Quantization is the art of throwing colors away on purpose, and dithering is the clever trick that makes the survivors look like more than they are - scattering a handful of palette colors so your eye blends them into shades that were never really there. This is the hands-on guide to turning many colors into few without it looking awful.

Digital · 67 4 Live Demos ~31 min read Indexed color
≤256
Colors in indexed mode
banding
Quantization's artifact
dither
Trades bands for texture
FS
Floyd–Steinberg diffusion
01

Why throw colors away?

A 24-bit image can express 16.7 million colors. Almost nothing that displays or stores that image can afford all of them. The GIF format tops out at 256 colors per frame. PNG-8, indexed BMPs, and many sprite formats do the same. E-paper price tags show a handful. Embedded screens, LED matrices, retro consoles, thermal printers, and a thousand constrained devices all live on tiny palettes. Even when the hardware can show everything, compression and file size reward using fewer colors.

Quantization answers the question "which colors do we keep, and where does each original pixel land?" Done naively, it produces ugly banding - smooth gradients shatter into visible steps. Dithering is the countermeasure: by mixing the available colors in a fine pattern, it fools the eye into seeing intermediate shades that the palette can't actually produce. Together they are one of the oldest and most useful tricks in computer graphics.

The core trade-off: quantization removes information; dithering hides the loss by spending spatial resolution. You can have a clean palette and visible bands, or a busy texture and smooth-looking tone - dithering lets you choose where the error goes.
02

Choosing a palette

Before you can map pixels, you need a palette - the small set of colors you are allowed to use. A bad palette dooms the result no matter how good the dithering; a palette tuned to the image can look almost lossless. There are two families: fixed palettes (chosen ahead of time) and adaptive ones (computed from the image's actual colors).

Uniform / fixed
Evenly spaced colors - e.g. the 6×6×6 "web-safe" cube, or N levels per channel. Simple and image-independent, but wastes slots on colors the image never uses.
Median cut
Recursively split the box of image colors along its longest axis, then average each region. The classic adaptive method behind most GIF encoders.
Octree
Insert colors into an 8-way tree and merge the least-used leaves until only N remain. Memory-efficient and fast for streaming quantization.
k-means clustering
Iteratively group pixels around N moving centroids. Higher quality, slower - and best done in a perceptual space, not raw RGB.
Perceptual weighting
Distances measured in OKLab/CIELAB match the eye far better than RGB Euclidean distance, so the kept colors are the ones that matter perceptually.
Mapping (the second half)
Once the palette exists, each pixel is mapped to its nearest palette entry - with or without dithering. Palette choice and mapping are separate decisions.

The demos below use a simple uniform palette (N levels per channel) so the quantization is easy to see and reason about - but everything about dithering applies equally to an adaptive median-cut or k-means palette. The palette decides which colors; dithering decides how you arrange them.

03

Posterization and banding

The simplest quantization is posterization: round every channel to the nearest of N evenly spaced levels. With 2 levels per channel you get 8 colors; with 4, you get 64. The problem shows up wherever the original is smooth - a sky, a soft shadow, a gradient. Continuous tone collapses into flat plateaus separated by hard edges: banding, also called contouring. Drop the levels and watch the bands appear.

Interactive 01 · Posterization

Rounding a smooth gradient to N levels

A smooth gradient, posterized to the number of levels you choose - no dithering yet. At high level counts it looks continuous; lower it and the gradient breaks into visible bands. This is quantization at its most naive, and the banding it produces is exactly what dithering exists to fix.

04

Ordered dithering

The first cure for banding is ordered dithering. Instead of rounding every pixel the same way, you nudge each one up or down by a small amount read from a fixed threshold matrix that tiles across the image. The classic is the Bayer matrix, a recursively-built grid that spreads its thresholds as evenly as possible. The result is a regular cross-hatch that, from a distance, reads as the in-between shades the palette can't store. Compare the plain and dithered halves.

Interactive 02 · Ordered (Bayer) dithering

The same gradient, plain on top, dithered below

Both halves use the same few levels. The top is plain posterization (hard bands); the bottom adds an ordered Bayer dither. Raise the matrix size for a finer pattern, lower the levels to make the effect dramatic. Notice the dithered half looks smoother despite using exactly the same palette.

Why ordered dithering endures: it is stateless and tileable - every pixel's output depends only on its own value and position, so it parallelizes perfectly and never streaks. That is why GPUs, print screening, and real-time graphics still reach for it.
05

Error-diffusion dithering

The other great family is error diffusion, and its most famous member is Floyd–Steinberg (1976). The idea is beautifully simple: quantize a pixel to the nearest palette color, measure the error you just introduced, and push that error onto the neighboring pixels that haven't been processed yet - 7/16 to the right, then 3/16, 5/16, 1/16 across the row below. Each pixel pays for the rounding of the ones before it, so the errors cancel out over an area instead of piling up into a band. Toggle it on the gradient below.

Interactive 03 · Floyd–Steinberg error diffusion

Spreading the rounding error across neighbors

A soft grayscale field quantized to just a few levels. With error diffusion off you see hard bands; switch it on and the rounding error scatters into an organic, newspaper-like texture that reads as smooth tone. Lower the levels all the way to 2 for the classic 1-bit look.

Ordered vs error diffusion: ordered is fast, regular, and tileable but can look "screened"; error diffusion usually looks smoother and more natural but is sequential (order-dependent) and can smear or "worm" on flat areas. Most GIF and PNG-8 encoders default to Floyd–Steinberg for exactly that smoother look.
06

Palette reduction in practice

Put it together on a full-color image. Reducing a rich, multi-hue picture to a small palette is where quantization and dithering really earn their keep: the right palette size plus dithering can make an 8- or 27-color version look startlingly close to the original. Switch the palette size and flip dithering on and off to feel the trade.

Interactive 04 · Color palette reduction

A colorful image squeezed onto a tiny palette

A synthetic color image quantized to a uniform palette of N levels per channel - so 2 levels is an 8-color palette, 3 is 27, 4 is 64. With dithering off, low palettes show blocky banding; with it on, the same palette renders smooth, photographic tone. This is exactly what happens when you export a GIF.

07

Best practices and pitfalls

Quantization is full of small decisions that quietly make or break the result. A few habits keep reduced-color output looking deliberate rather than damaged.

Quantize in linear light
Diffusing error in gamma-encoded sRGB biases the result. Convert to linear (or a perceptual space) for the math, then back.
Match palette to image
An adaptive palette (median cut, k-means) beats a fixed cube for most photos - dithering can't invent a color the palette lacks.
Don't over-dither flat art
Logos, icons, and flat illustration usually want clean quantization; dithering adds noise that ruins crisp edges and balloons file size.
Mind compression
Error-diffusion texture is high-frequency noise that defeats PNG/GIF run-length compression - dithered files are often larger.
Beware re-quantizing
Dithering an already-dithered image compounds the noise. Dither once, at the final palette, from the highest-quality source.
Add a little noise for banding
Even in 8-bit output, smooth gradients can band. A touch of dither or noise before display is the standard fix in film and games.
"Quantization decides what you lose; dithering decides whether anyone notices. One is arithmetic, the other is sleight of hand - and the eye falls for it every time." Editorial summary · spending resolution to buy tone
Where you still meet it daily: GIF and PNG-8 export, GPU dithering of 10-bit content down to 8-bit panels, e-ink displays, thermal and laser print screening, retro and pixel-art tooling, and the subtle dither games add to hide gradient banding on cheap monitors. Quantization never went away - it just got quieter.
08

Test your understanding

Six questions on quantization, banding, palettes, ordered dithering, and error diffusion. Instant feedback, no scores recorded - a wrong answer comes with a short explanation.

Quick check

Loading…
 
Question 1 of 6
09

Continue your journey

Quantization sits between how color is stored, how it's mapped into a gamut, and how print turns tone into dots - here's where to go next.