PocketMatrix forums

A read-only archive of the PocketMatrix forums (2001–2014). Posting and accounts are closed.

How to calculate the real RGB color for GAPI?

I'm loading an 16 bit TGA file and want to calculate the color like in one GAPI tutorial (for 16 bit displays):

Code:

unsigned short PixelCol = 0;
if (g_gxdp.ffFormat | kfDirect565) {
PixelCol = (unsigned short)
((R & 0xff)<< 11 | (G & 0xff) << 5 | (B & 0xff));
} else if (g_gxdp.ffFormat | kfDirect555) {
PixelCol = (unsigned short)
((R & 0xff)<< 10 | (G & 0xff) << 5 | (B & 0xff));
}

It didnt work, so i tried to change from 0,0,0 to 255,0,0. But while changing from 0 to 255, the color changed 4 or 5 times from black to red. :?:
refractor · 19 Feb 2002, 16:13 UTC — Try this....
Think about what you're doing...

R, B, and B are presumably bytes (you said you're trying it with 255).

So, say R is 255, = (1111 1111) in binary.

You're then having a 16-bit number as a target:
0000 0000 0000 0000

And shifting the 8 bit number for red *11* to the left.. making a *19* bit number with... so firstly, you're losing the top three bits of your RGB values, or worse, shunting them into the next colour.

You also shouldn't mask the 8-bit values with an 8-bit mask, after all, you only want the top 5 or 6 bits.

You want something like:

((R & 0xF8)<<8) | ((G & 0xFC)<<3) | ((B >>3));

and

((R & 0xF8)<<7) | ((G & 0xF8)<<2) | ((B >>3));

For the 565 and 555 respectively.

Make sure the RGB colour values are unsigned too, otherwise the shifts are going to screw it all up (by being signed shifts rather than logical shifts).

Cheers,

Refractor.
Ah, now I understand how GAPI works. I had nothing else than just the Microsoft Example but I couldnt believe they could write something wrong. Thank You very much.