#### Archived

This topic is now archived and is closed to further replies.

# Extracting color values from an R5G6B5 short

This topic is 5543 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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]

##### Share on other sites
Red = Pixel >> 11;
Green = (Pixel >> 5) & 0x6F;
Blue = Pixel & 0x3F;
?
dunno, i hope that helps.

##### Share on other sites
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

##### Share on other sites
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;

1. 1
Rutin
32
2. 2
3. 3
4. 4
5. 5

• 11
• 13
• 87
• 11
• 10
• ### Forum Statistics

• Total Topics
632973
• Total Posts
3009615
• ### Who's Online (See full list)

There are no registered users currently online

×