简体   繁体   English

如何将ARGB转换为RGB字节数组?

[英]How to convert ARGB to RGB array of bytes?

I have a byte array: 我有一个字节数组:

byte[] blue_color = {-1,0,112,-64};

How to convert into byte array of RGB? 如何转换为RGB字节数组?

And also how can I get the real RGB value of the color? 还有如何获得颜色的真实RGB值?

Assuming it's the first element that's the A component: 假设它是A组件的第一个元素:

byte[] rgb = Arrays.copyOfRange(blue_color, 1, 4);

To get the "real" colour values, you need to undo the two's complement representation: 要获得“真实的”颜色值,您需要撤消两者的补码表示形式:

int x = (int)b & 0xFF;

How to convert ARGB array into RGB? 如何将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];
}

How to print integer value: 如何打印整数值:



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

Could be done prettier with printf . 可以用printf做得更好。

How to convert into byte array of RGB? 如何转换为RGB字节数组?

byte[] rgb = new byte[3];
System.arraycopy(blue_color, 1, rgb, 0, 3);

And also how can I get the real RGB value of the color? 还有如何获得颜色的真实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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM