Skip to main content
gamedev.net
🔒 Locked

COLORREF Conversion - Easy

Started by Tooko Aug 2, 2002 at 2:32 AM 2 replies 2.1k views
Original Post
Tooko
Tooko
I have a problem. In order for my lighting engine to work, I need to be able to convert a COLORREF to RGB. So, how could I write the following function (in C++): void ColorrefToRGB(int *R, int *G, int *B, COLORREF clr) { // What to do! Oh, what to do! Argh!!! } Yeah, you get the point. And for those who are wondering, my lighting engine uses HDC SetPixel and GetPixel so, that''s why I need to decode the COLORREF into RGB. it makes it easier. MBDYProductions - Mike Brough - Programmer
the future is just like the past, just later. - TANSTAAFL
Colin Jeanne
Colin Jeanne
COLORREF has the form of 0x00bbggrr. RGB has the form 0x00rrggbb. Use your |, &, <<, and >> operators to extract and insert the correct information.

Or, rewrite your lighting engine to be much faster and write/read directly to the bitmap.



I will not make a list of links... I will not make a list of links... I will not make a list of links...
Invader''s Realm
Zipster
Zipster
Two things. First, use references, they're easier to work with in this situation, and second, make R G and B bytes instead of integers, because that's all the precision you need. My code will assume that.

A 2-second search through MSDN reveals the following about COLORREF:

0x00bbggrr

Mask clr to get the R, G, and B values.


R = clr & 0x000000FF;
G = clr & 0x0000FF00;
B = clr & 0x00FF0000;


Or do some spify bitshifting:

R = clr;
G = clr >> 8;
B = clr >> 16;


If masking or bitshifting is unfamiliar, take a look around online for explanations.

[edited by - Zipster on August 2, 2002 4:13:23 AM]
merlin9x9
merlin9x9
There are macros in the Windows API specifically to extract the color component values: GetRValue, GetGValue, and GetBValue. It''s better to use those rather than assuming a particular layout for the COLORREF type since, as a typedef, it''s intended as an abstraction.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.