{steinsoft.net}
 
 
 
 
 
Home/News
Code Snippets
Board
Projects
What's That?
I am using it actively
I planning to use/learn it
I don't plan to lean/use it
5th option
Leipzig
Home :: Programming :: Code Snippets :: MfcWin :: RGB Colour Values

[ RGB Colour Values ]

In this little and easy code snippet I will explain the most important things about using RGB colour values in your Windows applications. If you know how to use them, you will be able to use many of the Windows graphics functions.

The datatype that is used to represent colour values under Windows is called COLORRED and is defined like this:

typedef DWORD COLORREF;

COLORREF isn't a structure itself but a typedef - a second name - for a DWORD which is itself an unsigned long. This type alone doesn't help us much when using colour values so we need another helper the macro RGB with whom we can construct any colour value with the basic colours red, green and blue.

RGB(red_value, green_value, blue_value)
//Windows definition:
#define RGB(r,g,b)
  ((COLORREF)(((BYTE)(r)|((WORD)((BYTE)(g))<<8))|(((DWORD)(BYTE)(b))<<16)))

Every colour value has to be between 0 and 255 where 0 means no intensity and 255 means full intensity. So RGB(0,0,0) is black and RGB(255,255,255) is white. In total this gives us the possibility to use over 16 million colour values.

Creating new colours with RGB is really easy and can be compared with mixing colours on a palette. Beside creating new colours, there are three macros that extract the corresonding red, green or blue value from an exisiting COLORREF value.

GetRValue(colour)
GetBValue(colour)
GetGValue(colour)

//get green colour value from exisiting COLORREF colour
unsigned char red = GetGValue(colour);

To complete the code snippet, here's an example from real life:

//....
//get device context from our window
HDC hdc = GetDC(hWnd);
 
//set the text colour to red
SetTextColor(hdc,RGB(255,0,0));
 
//release DC after using it
ReleaseDC(hWnd,hdc);

You should now know everything about colour values. If you still have questions, ask them in the message board. Happy Coding by the way!

 Last edited 2003-04-23 19:41:13 by André Stein - printable version
» copyright by andré stein
» using stCM v1.0
» steinsoft.net revision 5.0