user188944
user188944

Reputation:

How to get name of color using Color Chooser in java

I want name of the color which is selected from Color chooser but now it is returning me the R,G,B Values. I want the name of the color for further use.Can anybody help me?

Upvotes: 2

Views: 4719

Answers (4)

Wouter Lievens
Wouter Lievens

Reputation: 4019

This service maps many, many color names:

http://chir.ag/projects/name-that-color/

I don't know whether it can/should be used as a webservice though.

Upvotes: 0

Pascal Thivent
Pascal Thivent

Reputation: 570315

You may find some domain-specific naming schemes like X11 or HTML4 but I don't think you'll find one standard. For example, Wikipedia's color list is based on the previous mentioned scheme, Xona.com color list is slightly different, etc.

I'm not sure Java has chosen one standard (over another) and offers color names for RGB colors. So I guess you'll have to pick one and to implement a custom conversion class yourself.

Upvotes: 0

Jasoon
Jasoon

Reputation: 432

For a slightly more extensive list of colours you could consider parsing something like the X11 rgb.txt file and matching to entries within that.

Upvotes: 1

peter.murray.rust
peter.murray.rust

Reputation: 38033

There is a method getColor() in http://java.sun.com/javase/7/docs/api/javax/swing/JColorChooser.html

This returns a Color. A number of Colors have names (http://java.sun.com/javase/7/docs/api/java/awt/Color.html) but most do not. As far as I know you will need to iteratre through the special names of colors to test whether the returned colour is one of those.

EDIT ColorChooser returns a java.awt.Color, not an RGB value. I do not know what the equals() contract for color is, but I suspect you could write:

Color c = chooser.getColor();
if (Color.BLACK.equals(c)) {
    // the color is black...
}

The named colors are:

    static Color    black
          The color black.
static Color    BLACK
          The color black.
static Color    blue
          The color blue.
static Color    BLUE
          The color blue.
static Color    cyan
          The color cyan.
static Color    CYAN
          The color cyan.
static Color    DARK_GRAY
          The color dark gray.
static Color    darkGray
          The color dark gray.
static Color    gray
          The color gray.
static Color    GRAY
          The color gray.
static Color    green
          The color green.
static Color    GREEN
          The color green.
static Color    LIGHT_GRAY
          The color light gray.
static Color    lightGray
          The color light gray.
static Color    magenta
          The color magenta.
static Color    MAGENTA
          The color magenta.
static Color    orange
          The color orange.
static Color    ORANGE
          The color orange.
static Color    pink
          The color pink.
static Color    PINK
          The color pink.
static Color    red
          The color red.
static Color    RED
          The color red.
static Color    white
          The color white.
static Color    WHITE
          The color white.
static Color    yellow
          The color yellow.
static Color    YELLOW
          The color yellow.

Upvotes: 1

Related Questions