我的应用程序中有一些滑块允许用户更改ARGB颜色,但是我需要将这些值转换为十六进制值,如0xff000000,它是纯黑的。将RGBA值转换为十六进制颜色代码
这是我到目前为止有:
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;
}
然而在获得整数值如下图所示,我得到了输入字符串NumberFormatException异常:“0xccffffff”:
int color = toHex(new Color(153f, 153f, 153f, 0.80f));
任何想法上如何获得这个整数?谢谢。
啊,谢谢。现在排序了。我为我的字体使用Slick,现在我已将Color切换为java.awt.Color。 – Kaikz