我的應用程序中有一些滑塊允許用戶更改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