3

I'd like to create a function to give N equidistant RGB colors. How do we define "distance" in this case? Well, I'm not too sure, but I was thinking to use a color wheel definition.

Color wheel

Hence, if I can create a method such as

public Color colorForAngle(int theta)

Then I would be able to divide 360/N and then extract out the N equidistant colors. Does that make sense? Anyone have a better idea how to get equidistant colors in Java? Anything built in that might help?

The point here is to find N colors that are sufficiently (or as much as possible) dissimilar. For instance, if N was 3, then the colors [255, 0, 0], [0, 255, 0], and [0, 0, 255] seem as far apart as possible.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
CodeGuy
  • 28,427
  • 76
  • 200
  • 317
  • Try this question, it could help you: http://stackoverflow.com/questions/180/function-for-creating-color-wheels – Andremoniy Jan 14 '13 at 07:30
  • P.S. And your question is more algorithmic than Java-related. – Andremoniy Jan 14 '13 at 07:33
  • True...however I did not know that that would be the case because I thought (just maybe) that Java may have something built in that someone knows of... – CodeGuy Jan 14 '13 at 07:35

3 Answers3

2

This sounds like it would be better suited to use HSV rather than RGB, as extracting equidistant colors from a circular representation of the color spectrum would be trivial.

As Andrew noted, you could use the following function: Color.getHSBColor(H,S,B)

Alex DiCarlo
  • 4,851
  • 18
  • 34
  • Actually, upon second reading of your question, it seems that `colorForAngle` would essentially use a `HSV` representation in order to return a `Color` value, so I would recommend reading the wikipedia article for more information, and then using any implementation online of `RGB` to `HSV` conversion (there are many) to achieve what you want. – Alex DiCarlo Jan 14 '13 at 07:30
  • A search on [images for 'color+wheel+hsv'](https://www.google.com.au/search?q=color+wheel+hsv&hl=en&safe=off&client=firefox-a&hs=Hxf&tbo=d&rls=org.mozilla:en-US:official&source=lnms&tbm=isch&sa=X&ei=j7XzUI_pKayciAeLm4CwCw&ved=0CAoQ_AUoAA&biw=1012&bih=975) would support your approach by the similarity to the example provided by the OP. Also look at [`Color.getHSBColor(H,S,B)`](http://docs.oracle.com/javase/7/docs/api/java/awt/Color.html#getHSBColor%28float,%20float,%20float%29). – Andrew Thompson Jan 14 '13 at 08:00
1

The important point here is that there is no exact answer, different color spaces have (slightly) different color wheels. I think that the "best" color space for this purpose would be the CIELAB as noted in an answer to Function for creating color wheels , because this was designed to approximate human vision.

However, Java has no built-in support for CIELAB, so either you write the support for it, or you can say that HSB is "good enough" (probably it is), and use Color.getHSBColor as noted in other answers.

Community
  • 1
  • 1
lbalazscs
  • 17,474
  • 7
  • 42
  • 50
  • any thoughts about the background color. the colors I chose should be distinct from a background color I provid – CodeGuy Jan 16 '13 at 04:57
  • Well, if you use a color wheel, all of your colors will have the same lightness (HSB brightness is not exactly the same as LAB lightness, but very similar), and I would use white or black or a shade of grey depending on the chosen lightness. Don't forget to upvote/accept the helpful answers :) – lbalazscs Jan 16 '13 at 08:43
-1

Well I can't say about a wheel,but long back I wrote an animation,Where i changed the colour's after a specific time.The main lines:

            colorIndex++;  // A number between 0 and 100.
            if (colorIndex > 100)
               colorIndex = 0;
            float hue = colorIndex / 100.0F;  // Between 0.0F and 1.0F.
            display.setColor( Color.getHSBColor(hue,1,1) );
joey rohan
  • 3,505
  • 5
  • 33
  • 70