• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

want algorithm to generate 100+ distinct colors

 
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 3324
32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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).

 
Sheriff
Posts: 22781
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I tried Rob's suggestion and got 100 squares, some in nice colours
 
Ranch Hand
Posts: 56
Android Mac Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 3324
32
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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)

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

a good question.
studied
 
Campbell Ritchie
Marshal
Posts: 79151
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
jack Long, welcome to JavaRanch
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 11497
19
Android Google Web Toolkit Mac Eclipse IDE Ubuntu Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ralph Cook
Ranch Hand
Posts: 479
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic