PDA

View Full Version : Random, unique colors that are completely different



nightmarez77
07-04-2014, 04:03 AM
I'm currently working on a chart drawing program with pie and bar charts, and am trying to create a function to generate a random color for each segment. I am currently using rand() to generate a random float[3] array for each and every segment, however some of the colors generated might look very similar to each other. (e.g 0.115f, 0.f, 0.f and 0.1684f, 0.f, 0.f). I was thinking of generating a big vector to store all the required colors and checking for similarities, but it seems to be a very tedious method.

Is there easier way to generate a unique set of colors instead of using just rand() and checking? The maximum number of colors i need to generate is around 25.

Jesse
07-04-2014, 09:54 AM
I'm currently working on a chart drawing program with pie and bar charts, and am trying to create a function to generate a random color for each segment. I am currently using rand() to generate a random float[3] array for each and every segment, however some of the colors generated might look very similar to each other. (e.g 0.115f, 0.f, 0.f and 0.1684f, 0.f, 0.f). I was thinking of generating a big vector to store all the required colors and checking for similarities, but it seems to be a very tedious method.

Is there easier way to generate a unique set of colors instead of using just rand() and checking? The maximum number of colors i need to generate is around 25.

Why can't you just hardcode the colors and map them to integers? Do they need to be randomly generated? RGB space has 256^3 color combinations which are far more than enough to pick 25 distinct colors.

You could also partition the interval [0, 255] into 25 disjoint closed intervals. The midpoint of each closed interval is then mapped to some integer from the list 0,1,2,..,24. So now just pick three random numbers from 0-24 and use them in a color map to create an RGB triple (x,y,z), where x,y,z are integers in [0,255]. Of course, you would then need to remap the three integers in [0,255] to three floating point types in [0,1] before using the color.