# OpenGL Random, but intelligent, colour selection

This topic is 4506 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Hi, Im writing an OpenGl application that draws database data on top of a textured map. Currently im working on grouping the data by different fields and selecting a colour for the different groups. At the moment my colour choosing algorithm simply chooses a random red, green and blue value for each field but this often chooses poor colours and there are also duplicates (obviously). I just wondered if there was some sort of algorithm that could generate a set number of colours that werent too similar? Regards, Matt

##### Share on other sites
I would create by hand a color palette and get a random color from this palette

##### Share on other sites
Thanks,

I would do this but sometimes upto 100 colours may be required so an algorithm would be ideal!!

Any other ideas?

Matt

##### Share on other sites
You should look for a function that will look something like this:
F((R1, G1, B1), (R2, G2, B2))
The larger the value of the function, the better a human eye can differ between the two colors.
I am not sure if it is easy to find this sort of function, but you probabbly need to look into image processing to find something like this.
After having this function F, you should find an algorithem that gives you n colors so that the minimum of F between all these n colors, is as large as possible.
Hope that helps.

##### Share on other sites
Quote:
 Original post by SoapsurgeonThanks, I would do this but sometimes upto 100 colours may be required so an algorithm would be ideal!!Any other ideas?Matt

You could make an array with N slots and generate colors into that with a for loop. For distinct colors, I would suggest using 8 red shades, 8 green shades and 8 blue shades. (with the lowest being 0 and the highest being 255, the other 6 between these two) With this you can make 8*8*8=512 distinct colors, put them into a table, and select a color for each group. Since there are only 8 shades of each color component is used, it's much easier to distinguish between them. If you only need 64 colors, you can use 4 shades per color channel (4*4*4=64).

Viktor

##### Share on other sites
consider your colors to be vectors in color-space: [0,1]X[0,1]X[0,1]

Now, I don't know how bright you want your colors to be, but when I do random color generation, I generate vectors on the unit sphere constrained to color space. Then, to compare how 'close' two colors are, I do a dot product, if that's below some threshold (say 0.8) then I assume they are far enough apart that I can use them both safely.

I've written a geological imaging tool, and from working with the users alot, I found that they suffer from information overload really really fast. So generally, they didn't want to see more than three types of samples at a time, and they wanted a few simple color palettes to view them with.

Now, I don't know what your application is, but I suspect you'll want to be careful about how much data you display. Also consider coloring related fields with similar colors.

##### Share on other sites
Hi,

thanks for the replies, I'll try and knock something up.

FYI my project is to do with bird watching! the users can query what birds have been seen where so if they desired they could have all birds seen in a 10km square, say. This could have up to 350 different birds which, if they choose group by bird, would need 350 different colours. This is an extreme example and for the majority of the time i guess i will only need 3,4 or 5 colours but i just wanted an algorithm to handle every scenario.

Matt

##### Share on other sites
I've used an algorithm like the one below for this a similar task, please note that I don't remeber the exact algorithm, just that I gave all components a medium range intensity [64, 128], then randomly gave one or two components extra strength:

unsigned char rgb[3];// Get random valuesrgb[0] = rand() & 0x3f;rgb[1] = rand() & 0x3f;rgb[2] = rand() & 0x3f;// Make sure they are not so darkrgb[0] += 0x40;rgb[1] += 0x40;rgb[2] += 0x40;// Make one of the primaries exta strongint i = ((rand() & 0xfff) * 3) >> 12; // [0, 2]rgb += 0x80;// Maybe make one of other primaries stand outif ((rand() & 0xfff) < 0x7fff){  i += ((rand() >> 11) & 1);  i %= 3;  rgb += 0x80;}

Disclaimer: I haven't compiled the code above...

Edit: I you want darker colors shift down:
rgb[x] = rbg[x] >> 1;
Brighter:
rgb[x] = (rbg[x] >> 1) + 0x80;

##### Share on other sites
You'll probably find it much easier to come up with an algorithm if you work in HSL colour space (hue, saturation, lightness) rather than RGB. You can then convert to RGB as a last step before drawing.

http://en.wikipedia.org/wiki/HLS_color_space

1. 1
2. 2
frob
20
3. 3
JoeJ
20
4. 4
5. 5

• 10
• 11
• 12
• 13
• 9
• ### Forum Statistics

• Total Topics
632210
• Total Posts
3004798

×