Converting a 16/24 bit hex value to R, G, B.

Started by
4 comments, last by Jabober 23 years, 8 months ago
I have seen in the tutorials many ways of converting R, G, and B values from 0-255 (24 bit) into a correct color value, but I was wondering how one would reverse the process?
Advertisement
Weel if the format for your card in 24 bit is R(8bits)G(8bits)B(8bits) (and it allmost certainly is) then if you have R 8bits, B 8bits and G 8 bits, you have to do something like:
unsigned char R,G,B;
_int32 Color = ((dword)R << 16) + ((dword)G << 8) + B
or
_int32 Color = ((dword)R << 16) & ((dword)G << 8) & (dword)B


You know, I never wanted to be a programmer...

Alexandre Moura

unsigned int RGBValue,x;
unsigned char R,G,B;

x = RGBValue;
B = RGBValue & 0xff;
x >>=8;
G = x & 0xff;
x >>=8;
R = x & 0xff;

In the last line you can remove " & 0xff" if you''re sure that the highest byte of RGBValue is 0.
Visit our homepage: www.rarebyte.de.stGA
quote:Original post by alexmoura

Weel if the format for your card in 24 bit is R(8bits)G(8bits)B(8bits) (and it allmost certainly is) then if you have R 8bits, B 8bits and G 8 bits, you have to do something like:
unsigned char R,G,B;
_int32 Color = ((dword)R << 16) + ((dword)G << 8) + B
or
_int32 Color = ((dword)R << 16) & ((dword)G << 8) & (dword)B


I think Jabober wanted the inverse operation....
and you should write | instead of &.
Visit our homepage: www.rarebyte.de.stGA
You''re right I mistook both the & and the question (or should be ?)

You know, I never wanted to be a programmer...

Alexandre Moura
    inline DWORD RGB15to24( DWORD c ){  c = ((c & 0x1F)<<3) | ((c & 0x3E0)<<6) | ((c & 0x7C00)<<9);  return c + ((c>>5) & 0x070707);}inline DWORD RGB16to24( DWORD c ){  c = ((c & 0x1F)<<3) | ((c & 0xF800)<<8);  DWORD g = (c & 0x7E0)<<5);  return c + ((c>>5) & 0x070007) + g + ((g>>6) & 0x000300);}    

This topic is closed to new replies.

Advertisement