CColor - RGB and HLS combined in one class
Another handy feature, which I miss since long in the Win32 API, are named colors: frequent common colors can be used by name (instead of using a RGB value). If you use only a small number of colors, for example to emphasize portions of text, the use of named colors makes code more understandible (in my humble opinion). X11 supports named colors since I remeber X11, though optimal use of the limited hardware palette was probably the original motive in those days. In the Html documentation of the Internet Explorer I discovered again name colors and decided, to integrate excactly the same into the CColor class.
CColor supports the serialization in a user friendly textformat, which is specially useful for the registry or databases.
The sample application demonstrates the use of the CColor class. In the upper area the parameters for drawing the color circle could be controlled. The lower area shows all named colors. Tooltipps over the color circle and the named colors show the color name and the RGB value. The sample applications also uses/demonstrates some tricks in MFC-tooltip-handling. The whole mechanism is implemented through the virtual function OnToolHitTest. Watch and admire (or shudder ;-)

The CColor Sample Application
ColorApp.exe (9k)
The sample application was build with Visual C++ 6.0 and therefore needs the latest DLLs
of this Compiler.
Demo Source (19k)
The source of the sample application.
CColor Reference
The detailled documentation of the CColor class.
Sample 2: Drawing of a color gradient with CColor
void DrawGradient(CDC& dc, int x, int y, CColor c1, CColor c2, int width, int height)
{
ASSERT(width > 0);
float dh = (c2.GetHue() - c1.GetHue()) / width;
float dl = (c2.GetLuminance() - c1.GetLuminance()) / width;
float ds = (c2.GetSaturation() - c1.GetSaturation()) / width;
for (int i = 0; i < width; ++i)
{
CPen pen(PS_SOLID, 1, c1);
CPen* pOld = dc.SelectObject(&pen);
dc.MoveTo(x + i, y);
dc.LineTo(x + i, y + height);
dc.SelectObject(pOld);
c1.SetHue(c1.GetHue() + dh);
c1.SetLuminance(c1.GetLuminance() + dl);
c1.SetSaturation(c1.GetSaturation() + ds);
}
}
) 1999 by Christian Rodemeyer

Comments
There are no comments yet. Be the first to comment!