微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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));

哪个是白色的.

原文地址:https://www.jb51.cc/java/438085.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐