Java Applet,获取paint()中使用的颜色名称 [英] Java Applet, get the name of the colour being used in paint()

查看:23
本文介绍了Java Applet,获取paint()中使用的颜色名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 Java Applet 的paint() 方法中使用图形对象时,

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?

使用后 g.setColor(Color.red);

After using g.setColor(Color.red);

然后画一个矩形

我试过使用 g.getColor().toString();

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

但这只会返回这样的字符串:"java.awt.Applet[r:255 g:0 b:0]"

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

推荐答案

如果您真的愿意,应该可以这样做.我会先将颜色从 RGB 转换为 HSV.然后将色调划分为区域.通常红色是 0 度,然后是 60 度的黄色,120 度的绿色,180 度的青色,240 度的蓝色,到 300 度的洋红色或紫色,然后变回红色.您可以使用饱和度来判断该值是否为灰度.该值可以告诉您亮"、常规"或暗".您可能希望对某些值进行特殊处理,例如橙色/棕色范围内的值.还有一些有特殊的名字,比如浅不饱和红"就是粉红色.

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.

对于它的价值,我发布了一个 此处为 Objective-C 版本.可能使用上面链接的维基百科文章将其转换为 Java 应该相当容易.

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.

这篇关于Java Applet,获取paint()中使用的颜色名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆