I am playing around with colors in C# and I wanted to know how to get a color value from a 16-bit byte array. Here is a code bellow where I use a 32-bit byte array.
var colorArray = new Color[b.Length/4];
for (var i = 0; i < b.Length; i += 4)
{
var color = Color.FromArgb(b[i + 0], b[i + 1], b[i + 2], b[i + 3]);
colorArray[i / 4] = color;
}
You basically shift the most significant bits of each field to the right place in its new format and mask off any missing bits to zero. Green is split across two bytes. This would be easier if the array were an array of 16 bit ints, but if it's in bytes, then the bit manipulation for a pair of bytes is roughly like this.
+----+----+----+----+----+----+---+---+---+---+---+---+---+---+---+---+
| byte1 | byte0 |
+----+----+----+----+----+----+---+---+---+---+---+---+---+---+---+---+
| 15 | 14 | 13 | 12 | 11 | 10 | 9 | 8 | 7 | 6 | 5 | 4 | 3 | 2 | 1 | 0 |
+----+----+----+----+----+----+---+---+---+---+---+---+---+---+---+---+
| - | Red | Green | Blue |
+----+----+----+----+----+----+---+---+---+---+---+---+---+---+---+---+
B8 = (byte0 << 3) & 0xF8;
G8 = ((byte1 << 6) & 0xC0) || ((byte0 >> 2) & 0x38);
R8 = (byte1 << 1) & 0xF8;
B8G8R8 = B8 | (G8 << 8) || (R8 << 16);