wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Generating list of colours. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Generating list of colours." Watch "Generating list of colours." New topic
Author

Generating list of colours.

Michael Boehm
Ranch Hand

Joined: Jun 02, 2010
Posts: 51
Hi,

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 ?
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4467
    
    8

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)
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 42648
    
  65
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.


Ping & DNS - my free Android networking tools app
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
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.

rc
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Ralph Cook wrote:Well, finally, a question that is the SAME one I've asked before. I don't remember if I asked it here.

http://www.coderanch.com/t/515401/java/java/want-algorithm-generate-distinct-colors


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Michael Boehm
Ranch Hand

Joined: Jun 02, 2010
Posts: 51
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.
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
I don't suppose there's any harm in posting the result of my researches from before:



Hope I'm not breaking length restrictions or anything -- it's a lot of colors!

rc
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4467
    
    8

Of course, what you really want is to pick an optimal set of N colours from that list, for any given N.

No, I'm not going to try .
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39885
    
  28
Find out about the JColorChooser class.
 
 
subject: Generating list of colours.