Lance Pollard
Lance Pollard

Reputation: 79480

Algorithm to sort colors like the rainbow

So I generated random colors and blended them together using chroma.js.

enter image description here

It looks decent because the blending in chroma.js is amazing. However, notice there are two or 3 green spots (or more) in the image, and a few purple spots, etc. To make this look nicer, I was thinking it would be good to sort the colors before blending them, so they are more like the rainbow. Wondering if there is any way to do this or if it's possible somehow, I can't really imagine how to do this.

As far as I thought is this. For each color, calculate it's distance to one of the 6 rainbow colors. The closest to red goes first, then the closest to orange, etc. But I don't really see how to do that, seems complicated.

Upvotes: 4

Views: 3012

Answers (1)

Yakov Galka
Yakov Galka

Reputation: 72549

Sorting by hue will give you the colors "like the rainbow". However, notice that while the rainbow colors are fully saturated ones, your set of colors vary in saturation and luminance too. Therefore ordering by hue may not produce the best looking result.

The problem of creating the smoothest palette out of the given set of colors is equivalent to the Traveling Salesperson Problem. Of course a precise solution of that is not necessary, and any fast approximation might do.

Upvotes: 7

Related Questions