【发布时间】:2016-11-04 18:51:02
【问题描述】:
我想将 int 颜色转换为 rgb 字节数组。我正在使用 ColorPickerDialog (ColorPickerDialog)。
如果我选择一种颜色(比如说蓝色),我会得到整数值:-16775425。
这符合十六进制的 0xFF 00 06 FF。
据我所知:红色:0x0,绿色:0x06,蓝色:0xFF。如果我在 MS-Paint (0006FF) 中测试它,我会得到蓝色。
如果我尝试使用以下代码将整数值转换为字节数组:
public byte [] getColorByte(int color1){
byte[] color = new byte[3];
color[2] = (byte) (color1 & 0xFF);
color[1] = (byte) ((color1 >> 8) & 0xFF);
color[0] = (byte) ((color1 >> 16) & 0xFF);
return color;
我会得到一个 [0,6,-1] 的字节数组。
但如果我想用 Color.rgb 函数设置按钮的背景颜色:
btn.setBackgroundColor(Color.rgb(getColorByte(color1)[0],getColorByte(int color1)[1],getColorByte(int color1)[2]));
我只得到一个白色按钮。
在我看来,问题在于 255 = FF != -1。那是对的吗? 如何将整数拆分为 RGB 值? (3字节数组)。
谢谢!!!
仅供参考:我知道我可以用整数更改背景颜色,但我想获得 RGB 数组:-)
【问题讨论】:
-
尝试将颜色数组存储为
int[]而不是byte[],这样您就不会得到负数。 -
谢谢,还有一个问题:如何从 [0,6,-1] 字节数组返回到 -16775425 Int 值?谢谢!!
标签: java android colors int rgb