Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# want algorithm to generate 100+ distinct colors

Ralph Cook
Ranch Hand
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

Sheriff
Posts: 14691
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.

Rob Camick
Ranch Hand
Posts: 2639
9
• 1
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
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
Posts: 20709
68
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.

Campbell Ritchie
Sheriff
Posts: 50740
83
I tried Rob's suggestion and got 100 squares, some in nice colours

Misha van Tol
Ranch Hand
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
Posts: 2639
9
• 1
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
Posts: 3

a good question.
studied

Campbell Ritchie
Sheriff
Posts: 50740
83
jack Long, welcome to JavaRanch

Ralph Cook
Ranch Hand
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
Posts: 11257
16
• 1
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