将RGBA值转换为十六进制颜色代码 [英] Convert RGBA values to hex color code
本文介绍了将RGBA值转换为十六进制颜色代码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在我的应用程序中有一些滑块,允许用户更改ARGB颜色,但是我需要将这些值转换为十六进制值,如0xff000000,它是黑色的。
I have some sliders in my application that allows the user to change ARGB colors, however I need to convert these values to a hex value like 0xff000000, which is solid black.
这是我到目前为止:
protected int toHex(Color col) {
String as = pad(Integer.toHexString(col.getAlpha()));
String rs = pad(Integer.toHexString(col.getRed()));
String gs = pad(Integer.toHexString(col.getGreen()));
String bs = pad(Integer.toHexString(col.getBlue()));
String hex = "0x" + as + rs + gs + bs;
return Integer.parseInt(hex, 16);
}
private static final String pad(String s) {
return (s.length() == 1) ? "0" + s : s;
}
但是当获得如下的Integer值时,我得到一个NumberFormatException :0xccffffff:
However upon getting the Integer value like below, I get a NumberFormatException for input string: "0xccffffff":
int color = toHex(new Color(153f, 153f, 153f, 0.80f));
有关如何获得这个整数的任何想法?感谢。
Any ideas on how to get this to an Integer? Thanks.
推荐答案
颜色参数必须在1f和0f之间浮动。所以这是一个有效的颜色:
The Color parameters must be floats between 1f and 0f. So this is a valid color:
int color = toHex(new Color(1f, 1f, 1f, 1f));
这是白色。
这篇关于将RGBA值转换为十六进制颜色代码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文