Thursday, February 12, 2009

[VC++] Extracting Red, Green, Blue values from a RGB value without using API functions

I will show you how to extract the Red, Green and Blue values from a COLORREF object. See the below code it is self explanatory.

COLORREF TestClr = RGB ( 100, 150, 50 ) ;
int nR, nG, nB ;
// Get Red value
nR = (( TestClr ) & 0xFF );
// Get Green value
nG = ((( TestClr ) >> 8 ) & 0xFF );
// Get Blue value
nB = ((( TestClr ) >> 16 ) & 0xFF );


CString csRGBVal;
csRGBVal.Format( "R = %d G = %d B = %d", nR, nG, nB );
MessageBox( csRGBVal, "RGB values" );

If the above code is executed below output message box will be shown.



No comments: