File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes want algorithm to generate 100+ distinct colors Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "want algorithm to generate 100+ distinct colors" Watch "want algorithm to generate 100+ distinct colors" New topic
Author

want algorithm to generate 100+ distinct colors

Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
I would like to know how to calculate a set of colors that will be distinct from each other.

My application will involve many (up to 100 or more?) distinct items that I want to graph; I would like each one to have its own color, and for that color to be distinguishable from other colors on the graph. I suppose if I just get 100 (or 200) distinct colors I can put them in an array and use them that way, but I still have to figure out what they would be.

I've searched enough to convince myself that, if this can be found, I don't know the right keywords to use.

Does anyone have color-generating algorithms that take into account whether the colors are liable to be visibly distinct?

A secondary goal, of course, is to have them reasonably pleasant to look at.

tia, rc
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

How are you holding the colors ? Is there a Color class with red, green, blue ? Is it an hex String like #FFFFFF ?

A secondary goal, of course, is to have them reasonably pleasant to look at.

"Pleasant" is very subjective. And in that case, I'm not sure you can automatically generate that. But I don't have enough graphics background to tell for sure.


[My Blog]
All roads lead to JavaRanch
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
I have no idea how to generate 100 different colors that are evenly distributed about the spectrum but you might find the HSL Color easier to work with. Maybe you can start by creating 30 different Hues. Then for each hue you create 4 different tones/shades.
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
Perhaps I should have said "100 Color objects". I don't care whether the algorithm to generate them uses hex, int, or sanskrit, as long as I can get a color object out each one.

I don't care if they are uniformly distributed -- not a goal here. I've tried loops of integers making different red, green, and blue values to make colors with, and have generated enough palettes to make me dizzy. But it seems like the colors are all garish, or too hard to distinguish from one another, and I'm hoping for something that will do better.

Perhaps there isn't such a thing -- perhaps color theory hasn't gotten this far yet, and all the people who deal with things that need colors get a palette from some human that did it manually. But I keep hoping (and, in this case, asking).

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19695
    
  20

There are, with java.awt.Color, 16 million different colors possible (actually 16,777,216 or 256^3). They can all be represented as ints between 0 and 16,777,215. If we change that to HEX you get FFFFFF, better known as white. So what you can do is start at color 0 (HEX 000000, black), then add a number each time (e.g. FFFFFF / 100). Color has a constructor that can take such an int.

That said, this will indeed generate random colors. That's because to a computer, a color is nothing more than a number (or a set of numbers if you separate into RGB). It can't tell if color FF46A3 is a nice color or not.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
I tried Rob's suggestion and got 100 squares, some in nice colours
Misha van Tol
Ranch Hand

Joined: Jan 02, 2010
Posts: 56

The maximum number of colors that you can visually differentiate will be around 20 (color wheel). If you need more colors pick them evenly from the spectrum.

Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2182
    
    7
It really depends on what you are looking for. Here is a simple example using the two Rob's suggestions. (Don't forget to download the HSLColor class from the link I provided)

jack Long
Greenhorn

Joined: Oct 27, 2010
Posts: 3

a good question.
studied
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
jack Long, welcome to JavaRanch
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
OP here -- I think I've satisfied myself that the answer to my question is: "There isn't one, really".

There is the set of "Internet Safe Colors", which start from 00 00 00 and add 33 hex to each of the red, green, and blue values in turn to get a set of RGB colors; this is the KIND of algorithm I was looking for, but the resulting colors are not always easily distinguishable from each other, and the 33 hex appears arbitrary, chosen for its calculation convenience as opposed to anything about the resulting colors.

There's the "X11 color" set, which (according to Wikipedia) is just a set that's been included in X installations for so long no one really remembers where it came from. The latter's primary purpose appears to be to map each color to a name so it can be specified by name. I have not detected a pattern to the numbers that result in the colors that I can use to calculate them.

Someone pointed out that regular JPG files only have 256 colors, and that asking to get 100 that were all distinct is likely a tall order. It's good to keep that in mind.

So I'm back to storing a list of colors for what I want to do, and going through the tedious job of choosing such colors (something I wish I had more talent for).

I discovered an additional requirement -- since I am putting text in the same box as the color, I need the color to be light enough that black text will show up reasonably well.

Thanks to all that posted -- I did learn things about colors from the suggestions and code posted here. If someone's really interested, I'll post my set of colors when I feel like I've got a reasonable set.

rc
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10376
    
    8

I am seeing this thread late and am not sure if it is still relavent. Java offers a component called JColorChooser which defaults to a nice palette with visually distinguishable colors. You can check out the source code to see how it is done
http://download.oracle.com/javase/tutorial/uiswing/components/colorchooser.html


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Ralph Cook
Ranch Hand

Joined: May 29, 2005
Posts: 479
Thanks for the link. This does not do what I want either; many pairs of colors in that palette are not easily distinguished from each other. Second, what I want (or wanted) was an algorithm to generate those colors, and the example program just uses the standard java JColorChooser. I suppose I could look up the source for that, but it doesn't actually do what I want.

Thanks, though. It's somewhat relevant.

rc
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: want algorithm to generate 100+ distinct colors