I know this is quite old, but I wanted to give also a solution with unions and without any bitwise operations.
union Color
{
unsigned int hex;
struct { unsigned char b, g, r; };
};
This way you can convert from HEX to RGB and from RGB to HEX, easily.
union Color hex;
hex.hex = 0x07C73C;
union Color rgb;
rgb.r = 7;
rgb.g = 199;
rgb.b = 60;
printf("RGB(%d, %d, %d), HEX(%06x)", hex.r, hex.g, hex.b, rgb.hex);
Output:
RGB(7, 199, 60), HEX(07c73c)
and to map the values in the range of 0.0 to 1.0 you simply divide it by 255.0 :)
EDIT:
The code above^^ is only supported under Little-Endian CPU's architecture, so a better way is to check what endianness does the system runs on.
Then you can check the endianness to define the order of the struct variables.
union Color
{
unsigned int hex;
#if IS_BIG_ENDIAN
struct { unsigned char a, r, g, b; };
#else
struct { unsigned char b, g, r, a; };
#endif
};
This code also supports alpha ( transparency ) in RGBA.