How To Construct Colour From Byte Array?
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
Solution 1:
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 = newbyte[] {(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());
Post a Comment for "How To Construct Colour From Byte Array?"