I am creating a GUI for an application where a large set of objects are represented as points and painted on a Graphics object. These points are to be grouped into k groups where the number k is specified by the user. I would like to paint the points in k different colours, but how do you easily generate k different colours when k can potentially be large ? In particular larger than the number of Color objects in awt.Color with a preset name. Is there something better than just randomly creating Colors using the Color constructor which takes the Red, Green and Blue values as input assigning random floats to each of the three parameters ?
One approach is to a HSB (Hue, Saturation, Brightness) colour model, and then equally space the hue angles (search for "colour wheel" for more information if you need it). That's a relatively simple way of generating "evenly spaced" colours.
For example:
where n = 0, 1, ...k - 1
(assumes n, k are floats - casting omitted if they're ints)
While I don't have an idea off the top of my head, I think generating colors randomly probably wouldn't work so well. You'd need to make sure that between any two colors there is a minimum distance in the R, G and B values so that they're distinguishable to the human eye. Maybe something like abs(R1-R2) + abs(G1-G2) + abs(B1-B2) > N for some value of N.
Well, finally, a question that is the SAME one I've asked before. I don't remember if I asked it here.
I've concluded, after going MUCH further down many google result pages than I usually do, that this is not something anyone has an algorithm to do automatically (or, if they did, they also write weapons systems or chess programs, and keep their algorithms secret as a matter of policy).
So I ended up finding an absolutely hilarious "color survey" by an absolutely hilarious cartoonist; he writes the cartoon "XKCD" at (oddly enough) www.xkcd.com, and his color survey can be found at http://blog.xkcd.com/2010/05/03/color-survey-results/
In it, he evidently put out a quarter of a million colors and asked people to name them. How he generated that list I do not know, but he has tables of results that I ended up using as my table of colors to assign to things. If people have different names for them, that's some indication that they are distinguishable from each other, and when I find a couple that are too close I ditch one of them. Meanwhile, it is by far the longest list of usable colors I found, and besides you get to read all the fun things he found out in his survey.
I ended up taking the source of the page that lists the full result and using an editor macro to generate a table in my java program. I suppose I could post that if someone's interested enough.
Thanks for the pointers. The colour study was a fun reference
For now I have decided to simply dynamically create "a grid" over the 256-by-256-by-256 cube with approximately as many points as needed and use the Color(int, int, int) constructor hoping that the difference in the RGB values will result in colours that are distinguishable for reasonable values of number of colours needed.