java – 将RGBA值转换为十六进制颜色代码

我的应用程序中有一些滑块允许用户更改ARGB颜色,但我需要将这些值转换为十六进制值,如0xff000000,它是纯黑色.

这是我到目前为止:

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”:

int color = toHex(new Color(153f,153f,0.80f));

有关如何将其转换为整数的任何想法?谢谢.

最佳答案
Color参数必须在1f和0f之间浮动.所以这是一个有效的颜色:

int color = toHex(new Color(1f,1f,1f));

哪个是白色的.

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...