You are in my Digital Garden.
Colour Quantization
Last modified: Dec 10th, 2025
See also image dithering.
“Median cut” seems like the obvious and simple go-to algorithm for this.
There also seem to be options available to how I implement this. Some (most?) implementations split every bucket every time, resulting in power-of-two palettes. Some implementations choose a single bucket to split at each iteration (the one with the largest range in any channel), resulting in arbitrary palette sizes. The latter sounds like it would give better results but I should test both if possible.
- https://github.com/joshdk/quantize/blob/65999d3a4c76/mmcq.go
- https://en.wikipedia.org/wiki/Median_cut
- http://leptonica.org/papers/mediancut.pdf
- https://rosettacode.org/wiki/Color_quantization#Go
- https://muthu.co/reducing-the-number-of-colors-of-an-image-using-median-cut-algorithm/
- https://joelcarlson.github.io/2016/01/15/median-cut/
Note using linear colour information performs worse than other methods like sRGB because this is a perceptual task. Using Lab or Oklab might perform even better, but I haven’t tried that.