getting the R,G and B values from a 16bit color?
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:
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 ); <br></pre><br><br>If you want to have R,G,B in 0 to 255 range you can decide:<br><br>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)<br><br>b) multiply by 255 and divide by range (accurate, but probably not the fastest)<br><br>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).
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.
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.
Color = (((R<<6)|G)<<5)|B;
Assuming that your R and B are less than 32 and your G is less than 64.
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 ); <br><br><!–QUOTE–></td></tr></table></BLOCKQUOTE><!–/QUOTE–><!–ENDQUOTE–><br><br>I'd say this is not really readable (especially the 0xf800…)<br>I'd do as follow<br><br><br>template<br>inline T getBits(const T& value, int begin, int length)<br>{<br> return (value >> begin) & ((1 << length) - 1);<br>}<br><br>R = getBits(Color, 11, 5);<br>G = getBits(Color, 5, 6);<br>B = getBits(Color, 0, 5);<br>
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; };};
Quote:Original post by AssDruid
I 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
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement