1

How do you find the color name of a pixel in Java? I have found the RGB values of the pixel, but have no idea of how to get the name "yellow" from the pixel.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
user2758220
  • 21
  • 1
  • 2
  • there will not always be a certain name for a color in the rgb value settings. Sometimes it will be just a collection of red, blue, and green pixels as the name. – user2277872 Sep 08 '13 at 04:45
  • You expect 16,777,216(1) different color names? 1) the number of colors that can be represented using 256 values of R, G & B. – Andrew Thompson Sep 08 '13 at 06:43
  • Possible duplicate of [How to get name of color using Color Chooser in java](http://stackoverflow.com/questions/1654582/how-to-get-name-of-color-using-color-chooser-in-java) or [this question](http://stackoverflow.com/q/8952975/418556) or [this question](http://stackoverflow.com/q/15246526/418556).. – Andrew Thompson Sep 08 '13 at 06:45

3 Answers3

3

I haven't come across a part of the Java API that lets you do that. It is a feature you would most likely need to implement on your own, especially since not all rgb color combinations will have names.

One place you could start is by creating a lookup table using the rgb color values and names given on a site such as the Wikipedia list of colors page.

However, since not every combination of rgb values will map to a defined color, you would need to come up with an algorithm to map a set of values to the closest color. This could be something along the lines of computing the mean squared error between the rgb value for the pixel, and the rgb values for each color name. You could then pick the color name with the lowest score.

Here is a gist with some code (note however, it may not compile) that illustrates how a ColorNameLookup class that computes the mean square error to find the closest matching color name could work: https://gist.github.com/nightlark/6482130

Ryan
  • 712
  • 7
  • 21
  • Could I just use this and take your equation and your color list ? { robot= new Robot(); Color color = robot.getPixelColor(x,y); System.out.println(" Go to the point on the screen and than hover it in the box ( Gives RBG)"); System.out.println("Red = " + color.getRed()); System.out.println("Green = " + color.getGreen()); System.out.println("Blue = "+ color.getBlue()); – user2758220 Sep 08 '13 at 05:56
  • If I'm understanding your question right, then yes. No guarantees that it will be the most accurate mapping of color to the name of the color; it probably won't handle certain colors very well since you could have a reddish color that has a relatively low value for red. A version that would return color names that make more sense would probably take the HSV (Hue, Saturation, Value) representation of the color, then look up the closest match for the hue. You'd have to play around with it some to see what gives results that make the most sense. – Ryan Sep 09 '13 at 07:32
3

there are some standard colors in java and others can be generated using RGB or HSB constructors. there is no way of getting the color name as there may be many combinations of RGB values (256 x 256 x 256) . Only few combinations have name (standard colors like white, red, yellow, black etc.)

Gagan93
  • 1,826
  • 2
  • 25
  • 38
  • I am trying to get it so when I move my mouse that the color name will appear. I have where I have my RGB will appear on a window , but not the name of the color. If that is more helpful – user2758220 Sep 08 '13 at 06:01
  • *"If that is more helpful"* No, not really, given you seem to have failed to notice this answer says (basically) 'not possible'.. – Andrew Thompson Sep 08 '13 at 06:48
1

I am sure, you have got the basic problem with your question.

If you are trying to find out the color name of any possible combination of RGB values, this will require a exhaustive storage where you already have mapped all possible RGB values to a unique color name.

This means, your question cannot be answered with 100% correctness, until this map is with you. But yes you can definitely find the color name for the colors defined in java.awt.Color class.

So, you can also use reflection in java to find out the list of public static final Color properties defined in the java.awt.Color and for each value of RGB you have, you can compare it across all the declared Color in java.

dharam
  • 7,882
  • 15
  • 65
  • 93
  • So I just need to make a list and than compare it to the color names.How will I get it as close to the actual name of the color ? What would the equation look like ? – user2758220 Sep 08 '13 at 06:49
  • 1
    *"But yes you can definitely find the color name for the colors defined in `java.awt.Color` class."* All 14 of them. Out of a possible 16.7+ million that's not bad ..it's *atrocious.* – Andrew Thompson Sep 08 '13 at 06:51