Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generate unique colours

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. :)

like image 521
Nick Avatar asked Apr 21 '09 15:04

Nick


2 Answers

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.

like image 75
Brian R. Bondy Avatar answered Jan 02 '23 05:01

Brian R. Bondy


Whats your sample space... how many items are we talking.

You could build up an array of RGB Triples from

for(int r = 0; r < 255; r = r+16)
   for(int g = 0; g < 255; g = g+16)
      for(int b = 0; b < 255; b = b+16)
           // take r, g, b and add it to a list

Then randomise your list and iterate through it. that'd give you 16^3 (4096) different colors before a repeated color.

like image 22
Eoin Campbell Avatar answered Jan 02 '23 03:01

Eoin Campbell