Question

I have a byte array:

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

How to convert into byte array of RGB?

And also how can I get the real RGB value of the color?

Was it helpful?

Solution

How to convert ARGB array into 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.

OTHER TIPS

Assuming it's the first element that's the A component:

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 into byte array of 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?

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;
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top