Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

COLORREF troubles... 2

Status
Not open for further replies.

bitbit

Programmer
Nov 14, 2002
14
0
0
US
Hi all,

I'm trying to separate a COLORREF into the three basic color amounts. I know the structure of COLORREF is 4 bytes set like this:

0x00bbggrr

I need to extract the blue, green and red values into separate variables. Does anyone know how to do this?

Thanks!
 
GetBValue / GetGValue / GetRValue, or just mask the COLORREF yourself....
Greetings,
Rick
 
I don't know if this is the most elegant way to do this, however, logical and bitwise operators should do the job.
Code:
COLORREF crC; // 0x00bbggrr
int nR = (crC & 0xff);
int nG = ((crC & 0x0000ff00)>>2);
int nB = ((crC & 0x00ff0000)>>4);
[\code]

HTH

Ron
 
Many thanks to you both - I hadn't realized there were some API functions for this but I'm also interested in how those bitwise operations work - that's something I'm not too familiar with yet.
 
Actually those aren't API's, they're just macro's that expand to the bit masking/shifting stuff.

Be careful though; rbrunton is right with his mask, but what he probably did not realize when writing his examples is that the >> operator is a bit shifter, NOT a nibble shifter. it should be >>8 instead of >>2 and >>16 instead of >>4.
Greetings,
Rick
 
Thanks Rick. You are correct and I was too quick. :-(

Ron
 
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Sponsor

Back
Top