Java Applet, get the name of the colour being used in paint()

我与影子孤独终老i 提交于 2019-12-11 20:11:42

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/19454398/java-applet-get-the-name-of-the-colour-being-used-in-paint

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!