# getting the R,G and B values from a 16bit color?

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

## Recommended Posts

what is the fastest way doing this?  I forgat, the color structure is R5 G6 B5

##### Share on other sites
I wouldn't care about fastest as the compiler will usually optimize that bit quite good.

Just don't use weird algorithms.

With R5G6B5 i'd use:

R = ( Color & 0xf800 ) >> 11;  [R from 0 to 31]G = ( Color & 0x07c0 ) >> 5;   [G from 0 to 63]B = ( Color & 0x001f );        [B from 0 to 31]

If you want to have R,G,B in 0 to 255 range you can decide:

a) faster but not completely accurate (shift up and fill with 1s or 0s, depending on your choice, 1s will have no real black, 0s will have no real white)

b) multiply by 255 and divide by range (accurate, but probably not the fastest)

c) multiply R and B by 8, G by 4. Can be done with bit shifts, but you won't have complete white (Same as point a with 0s filling).

##### Share on other sites
Thanks Endurion!
one more quick question, now that I have the RGB values, how can I build again a 16bit 565 color?
I really need to learn how to work with bits...
Thanks.

##### Share on other sites
Color = (((R<<6)|G)<<5)|B;
Assuming that your R and B are less than 32 and your G is less than 64.

##### Share on other sites
Quote:
 Just don't use weird algorithms.With R5G6B5 i'd use:R = ( Color & 0xf800 ) >> 11; [R from 0 to 31]G = ( Color & 0x07c0 ) >> 5; [G from 0 to 63]B = ( Color & 0x001f ); [B from 0 to 31]

I'd say this is not really readable (especially the 0xf800...)
I'd do as follow

template
inline T getBits(const T& value, int begin, int length)
{
return (value >> begin) & ((1 << length) - 1);
}

R = getBits(Color, 11, 5);
G = getBits(Color, 5, 6);
B = getBits(Color, 0, 5);

Thanks guys.

##### Share on other sites
Unions are sometimes discouraged, but to me this sounds like the perfect opportunity to use a union between a 16bit integer and a bit field

struct PIXEL{    union{	unsigned red:5, green:6, blue:5;	unsigned short data;    };};

##### Share on other sites
Quote:
 Original post by AssDruidI really need to learn how to work with bits...

Ok, you have 16 bits in R5G6B5 format. So the bits look like this in binary:
rrrrrggggggbbbbb
Ok, you want to get just the blue bits, so you want to mask against 0000000000011111. If you put windows calculator into advanced mode, you can enter that in binary mode, and then change to hex. That converts binary to hex. Which gives you 0x1f. So that means that to get the blue value, you AND the colour with 0x1f (or 0x001f if you add leading zeros). So you get B = Colour & 0x001f;.
Next is green. You want to mask with 0000011111100000 which is 0x7e0 (or 0x07e0 with leading zeros). However, your value is still 5 bits to the left. So you need to shift it back 5 bits to the right. So you get: G = (Colour & 0x07e0) >> 5;
Lastly is red. You mask with 1111100000000000 which is 0xf800 in hex. This time the bits are (5+6)=11 bits to the left. So you shift right by 11 bits. Which gives you R = (Colour & 0xf800) >> 11

Hope that helps

• ### Game Developer Survey

We are looking for qualified game developers to participate in a 10-minute online survey. Qualified participants will be offered a \$15 incentive for your time and insights. Click here to start!

• 15
• 22
• 17
• 46