0

When using the graphics object in the paint() method of a Java Applet,

Does anyone know how to use a method on the graphics object whereby it will return the name of the color being used?

After using g.setColor(Color.red);

Then drawing a rectangle

Ive tried using g.getColor().toString();

But this only returns a string like this: "java.awt.Applet[r:255 g:0 b:0]"

Im looking to get back "Red" if its possible

Chris
  • 33
  • 1
  • 6

1 Answers1

0

It should be possible to do something like this if you really want to. I would start by converting the color from RGB to HSV. Then partition the hues into regions. Usually red is at 0 degrees, followed by yellow at 60, green at 120, cyan at 180, blue at 240, to magenta or purple at 300 degrees, then back to red. You can use the saturation to tell if the value is grayscale or not. The value can tell you "bright", "regular" or "dark". You might want to special-case some values, such as those in the orange/brown range. And some have special names, like "light unsaturated red" is pink.

For what it's worth, I've posted an Objective-C version here. It should be fairly easy to convert to Java possibly using the wikipedia article linked above.

Community
  • 1
  • 1
user1118321
  • 25,567
  • 4
  • 55
  • 86