Generate unique colours

Nick picture Nick · Apr 21, 2009 · Viewed 11.9k times · Source

I want to draw some data into a texture: many items in a row. They aren't created in order, and they may all be different sizes (think of a memory heap). Each data item is a small rectangle and I want to be able to distinguish them apart, so I'd like each of them to have a unique colour.

Now I could just use rand() to generate RGB values and hope they are all different, but I suspect I won't get good distribution in RGB space. Is there a better way than this? E.g. what is a good way of cycling through different colours before they (nearly) repeat?

The colours don't have to match with any data in the items. I just want to be able to look at many values and see that they are different, as they are adjacent.

I could figure something out but I think this is an interesting question. :)

Answer

Brian R. Bondy picture Brian R. Bondy · Apr 21, 2009

Using the RGB color model is not a good way to get a good color mix. It's better to use another color model to generate your color, and then convert from that color model to RGB.

I suggest you the HSV or HSL color model instead, in particular you want to vary the Hue.

If you want X different color values, vary them from 0 to 360 with a step size of 360 divided by X.