我有一个字节数组:
byte[] blue_color = {-1,0,112,-64};
如何转换成RGB的字节数组?
还有如何获得颜色的真实RGB值?
假设它是 A 组件的第一个元素:
byte[] rgb = Arrays.copyOfRange(blue_color, 1, 4);
要获得“真实”颜色值,您需要撤消二进制补码表示:
int x = (int)b & 0xFF;
如何将 ARGB 数组转换为 RGB?
byte[] argb = ...;
byte[] rgb = new byte[(argb.length / 4) * 3];
int index = rgb.length - 1;
for (int i = argb - 1; i >= 0; i -= 4) {
rgb[index--] = argb[i];
rgb[index--] = argb[i - 1];
rgb[index--] = argb[i - 2];
}
如何打印整数值:
byte[] oneColor = {..., ..., ..., ...};
int alpha = oneColor[0] & 0xFF;
int red = oneColor[1] & 0xFF;
int green = oneColor[2] & 0xFF;
int blue = oneColor[3] & 0xFF;
System.out.println("Color: " + alpha + ", " + red + ", " + green ", " + blue);
System.out.println("Hexa color: 0x" + Integer.toHexString(alpha) + " " + Integer.toHexString(red) + " " + Integer.toHexString(green) + " " + Integer.toHexString(blue));
可以做得更漂亮printf
。
如何转换成RGB的字节数组?
byte[] rgb = new byte[3];
System.arraycopy(blue_color, 1, rgb, 0, 3);
还有如何获得颜色的真实RGB值?
int red = rgb[0] >= 0 ? rgb[0] : rgb[0] + 256;
int green = rgb[1] >= 0 ? rgb[1] : rgb[1] + 256;
int blue = rgb[2] >= 0 ? rgb[2] : rgb[2] + 256;