If I were you, I would stick to multiplicative additive operations in the functions of packing / unpacking. Something like that
unsigned long RGBA2DWORD(int iR, int iG, int iB, int iA)
{
return ((iA * 256 + iR) * 256 + iG) * 256 + iB;
}
with symmetrical unpacking function
RGBA DWORD2RGBA(unsigned long dwColor)
{
RGBA tmp;
tmp.B = dwColor % 256; dwColor /= 256;
tmp.G = dwColor % 256; dwColor /= 256;
tmp.R = dwColor % 256; dwColor /= 256;
tmp.A = dwColor % 256;
return tmp;
}
Note that there is only one “magic constant” in the entire code.
, , , , .
unsigned long RGBA2DWORD(int iR, int iG, int iB, int iA)
{
return (((((iA << 8) + iR) << 8) + iG) << 8) + iB;
}
RGBA DWORD2RGBA(unsigned long dwColor)
{
RGBA tmp;
tmp.B = dwColor & 0xFF; dwColor >>= 8;
tmp.G = dwColor & 0xFF; dwColor >>= 8;
tmp.R = dwColor & 0xFF; dwColor >>= 8;
tmp.A = dwColor & 0xFF;
return tmp;
}
" ".
/ , , /.