Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Preview: Picking Colors

stough, 202-

Trying out alternative color picking schemes for a 256-color palette... The way I chose in color_clustering is k-means, which works pretty well. Some other ideas:

  • Pick the 256 most frequent colors (this is the one I’m doing here)

  • replace very rare colors with nearby more common ones until you get to 256? Interesting, maybe hard to implement...?

  • compare every pixel with neighbor. take median (really just average), then pick the 256 most common medians...That seems overly complicated

<Figure size 800x300 with 4 Axes>

This “popularity” palette fails badly on this image. The most frequent exact colors are all clustered among the dark clouds and the dim sky, since those big regions contain many pixels with nearly the same color. The brilliant orange glow of the sunset uses many slightly different colors, each one rare, so not a single one of them makes the top 256. Those pixels get mapped to the nearest dull color in the palette, which you can see as the bright band in the error image.

Compare that to k-means, whose reconstruction is nearly indistinguishable from the original. K-means doesn’t care how often an exact color occurs, only how many pixels are near each center. So a region of many slightly different oranges still pulls a center its way. The classic median cut algorithm, used in many early GIF encoders, gets a similar benefit by recursively splitting the RGB cube into boxes holding equal numbers of pixels.