将RGB值转换为颜色 [英] Converting RGB values into color

查看:198
本文介绍了将RGB值转换为颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个返回如下所示十进制值的java代码

I have a java code that returns decimal values as shown below

 [1.0, 0.0, 0.0] for Red  
 [0.0, 1.0, 0.0] for Green 

第一个值表示Red的颜色代码,第二个值表示绿色的颜色代码,第三个值表示蓝色的颜色代码。

the first value denotes the color code for Red, the second value denotes color code for Green and the third value denotes Color code for Blue.

有没有办法,我们可以将这些RGB值转换成相应的颜色?

Is there any way we can convert these RGB values into the respective color in java?

推荐答案

有一个 example 返回依赖于使用Reflection获取颜色名称的颜色名称(java.awt.Color)

There are an example to return the color name which depend on using Reflection to get the name of the color (java.awt.Color)

public static String getNameReflection(Color colorParam) {
        try {
            //first read all fields in array
            Field[] field = Class.forName("java.awt.Color").getDeclaredFields();
            for (Field f : field) {
                String colorName = f.getName();
                Class<?> t = f.getType();
                if (t == java.awt.Color.class) {
                    Color defined = (Color) f.get(null);
                    if (defined.equals(colorParam)) {
                        System.out.println(colorName);
                        return colorName.toUpperCase();
                    }
                }
            }
        } catch (Exception e) {
            System.out.println("Error... " + e.toString());
        }
        return "NO_MATCH";
    }

和主要

        Color colr = new Color(1.0f, 0.0f, 0.0f);
        Main m = new Main();
        m.getNameReflection(colr);
    }






java.awt.Color定义此颜色:

white
WHITE
lightGray
LIGHT_GRAY
gray
GRAY
darkGray
DARK_GRAY
black
BLACK
red
RED
pink
PINK
orange
ORANGE
yellow
YELLOW
green
GREEN
magenta
MAGENTA
cyan
CYAN
blue
BLUE

这篇关于将RGB值转换为颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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