Jump to content
  • Advertisement

Archived

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

Teric

Extracting color values from an R5G6B5 short

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
That''s a good idea, using the & operator.

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


Red = Pixel >> 11; //Bit shift right by 11

Green = (Pixel >> 5) & 0x3F; //Bit shift right by 5, then & out all but last 6 bits

Blue = Pixel & 0x1F; //& out all but last 5 bits

Share this post


Link to post
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;

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!