Resolving an image, one color at a time

Aporia Labs

This is the sample post that ships with the site. It explains the animation on the home page, and along the way it uses every building block a post can use: math, code, a figure, sidenotes, citations, callouts, an interactive widget, and a BibTeX block. Replace it with your own.

The animation shows one small image, about 160 by 90 pixels, redrawn with a palette of 2 colors, then 4, 8, 16, 64 and 256. With 2 colors you see blobs. By 256 you see the whole scene. The code that does this is about a hundred lines, in src/lib/quantize.ts.

Fewer colors

Quantizing an image means choosing a small palette PP of KK colors and replacing every pixel pp with its nearest palette entry:

Q(p)=arg min⁡c∈P∥p−c∥2Q(p) = \operatorname*{arg\,min}_{c \in P} \lVert p - c \rVert^2

A pixel of the original takes 24 bits. A pixel of the quantized image is an index into the palette, which takes log⁡2K\log_2 K bits.

Colors KK2481664256
Bits per pixel123468

The same operation, nearest entry in a codebook, is how VQ-VAE turns continuous latents into discrete tokens [1]. There the codebook is learned; here it is computed from the image.

Choosing the palette

The palette comes from median cut [2]. Put every pixel in one box in RGB space. Find the box whose pixels are most spread out along some channel, sort its pixels along that channel, and split it at the median. Repeat until there are KK boxes. Each box contributes one palette color: the mean of its pixels.The palette is recomputed from scratch for each level. The 4-color palette does not contain the 2-color one.

Diffusing the error

Nearest-color rounding alone gives flat bands. Floyd–Steinberg dithering [3] fixes that by keeping track of the rounding error e=p−Q(p)e = p - Q(p) at each pixel and adding it to the neighbors that have not been visited yet, in these proportions:

116[⋅∗7351]\frac{1}{16} \begin{bmatrix} \cdot & \ast & 7 \\ 3 & 5 & 1 \end{bmatrix}

The star is the current pixel. The weights sum to one, so away from the image edges no error is lost: a region’s average color survives even when no single pixel has that color.

donee7/163/165/161/16
Where one pixel's error goes. Pixels are visited left to right, top to bottom, so every target is still unvisited.

In code, the diffusion step is a few lines per color channel:

for (let c = 0; c < 3; c++) {
  const err = work[i + c] - nearest[c];
  if (x + 1 < w) work[i + 4 + c] += (err * 7) / 16;
  if (y + 1 < h) {
    const below = i + w * 4 + c;
    if (x > 0) work[below - 4] += (err * 3) / 16;
    work[below] += (err * 5) / 16;
    if (x + 1 < w) work[below + 4] += err / 16;
  }
}

Try it

A landscape of sky, hills and water, dithered down to 8 colors.
The same quantizer as the hero, on a daytime landscape. Turn dithering off to see the bands it hides.

What failed

A palette built from the extremes of the image instead of the means should dither over more of it. That is untested, and the palette is left as is, because the blobs resolving into detail are the point of the animation.

Cite this note

Cite this note
@misc{aporia2026resolving,
  author       = {Kavya B S},
  title        = {Resolving an image, one color at a time},
  year         = {2026},
  month        = nov,
  howpublished = {Aporia Labs},
  url          = {https://aporia-labs.vercel.app/posts/resolving-an-image/}
}

References

  1. van den Oord, A., Vinyals, O., & Kavukcuoglu, K. (2017). Neural discrete representation learning. Advances in Neural Information Processing Systems 30. Link
  2. Heckbert, P. (1982). Color image quantization for frame buffer display. ACM SIGGRAPH Computer Graphics, 16(3), 297–307. Link
  3. Floyd, R. W., & Steinberg, L. (1976). An adaptive algorithm for spatial greyscale. Proceedings of the Society for Information Display, 17(2), 75–77.