Extracting color values from an R5G6B5 short

Started by
2 comments, last by Teric 20 years, 8 months ago
I'm wondering what would be the best way to extract RGB values from a short which contains R5G6B5 formatted data. I plan to use the following code, but I wanted to check if there is a better way. Any suggestions? Thanks in advance!

BYTE Red;
BYTE Green;
BYTE Blue;
short Pixel;

///...<snip>... obtain pixel data into Pixel variable


Red = Pixel >> 11;
Green = (Pixel << 5) >> 10;
Blue = Pixel % 32;
[edited by - Teric on August 18, 2003 10:00:45 AM]
I am always open to feedback, positive or negative...
Advertisement
Red = Pixel >> 11;
Green = (Pixel >> 5) & 0x6F;
Blue = Pixel & 0x3F;
?
dunno, i hope that helps.
That''s a good idea, using the & operator.

But shouldn''t your hex values be as follows?

Red = Pixel >> 11;  //Bit shift right by 11Green = (Pixel >> 5) & 0x3F; //Bit shift right by 5, then & out all but last 6 bitsBlue = Pixel & 0x1F; //& out all but last 5 bits
I am always open to feedback, positive or negative...
You may also need to convert the ranges from 0-31 and 0-63 to 0-255.

The quick way is this:
    Red = ( Pixel >> (16-8) ) & 0xF8;    Green = ( Pixel >> (11-8) ) & 0xFC;    Blue = Pixel << (8-5);  

But the colors may look bad (yellow will have a green tint)

This way looks better but it is slower:
    Red = ( Pixel >> (16-5) ) * 255 / 31;    Green = ( ( Pixel >> (11-6) ) & 0x3F ) * 255 / 63;    Blue = ( Pixel & 0x1F )  * 255 / 31;  

John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

This topic is closed to new replies.

Advertisement