使用Java,我可以将人类可读的颜色名称转换为整数数组吗? [英] Using Java, can I convert a human readable color name into an integer array?

查看:261
本文介绍了使用Java,我可以将人类可读的颜色名称转换为整数数组吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Java,如何将人类可读的颜色名称转换为三元素整数数组?

Using Java, how do I convert a human readable color name into a three-element integer array?

例如, p>

For example, I have a color represented by:

int[] RGB_COLOR = {128,25,25};

我想要能够转换静态值 Color.BLUE 一个三元素整数数组,类似于上面的一个。

And I want to be able to convert the static value Color.BLUE into a three-element integer array that is similar to the one above. Is this possible or do I have to hack my way around this?

推荐答案

你的意思是这样...

Do you mean like this...

Color c = Color.BLUE;
int[] RGB = { c.getRed(), c.getGreen(), c.getBlue() };

要将颜色名称转换为颜色,您可以使用反射,提供自己的内置颜色。如果你想要更多的颜色,你需要使用地图。

To convert a colour name to a Color you can use reflections, provided its own of the built in colours. If you want more colours you would need to use a map.

public static Color colorOf(String color) {
    try {
        return (Color) Color.class.getDeclaredField(color).get(null);
    } catch(Exception notAvailable) {
        return null; // ??
    }
}

这篇关于使用Java,我可以将人类可读的颜色名称转换为整数数组吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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