How to construct colour from byte array?

android, java

Solution

Bytes in Java are signed so the positive part can only hold values until 127, RGB goes up to 255. So you have to compensate for that:

byte b = (byte) 130;
int i = b & 0xFF;
System.out.println(i); //Prints 130 again

The int can then be passed to the Color constructor.

Edit: complete example:

byte[] values = new byte[] {(byte) 130, (byte) 150, (byte) 200, (byte) 200};
Color color = Color.argb(values[0] & 0xFF, values[1] & 0xFF, values[2] & 0xFF, values[3] & 0xFF);
System.out.println(color + " alpha " + color.getAlpha());

Problem

Im struggling with very simple task (well, i think so). I have `byte[4]` array that represents colour values like `byte[0] = alpha`, `byte [1] = red` and so on. How can I convert this byte array to an actual colour object? Thanks for an answer.

Original source