CColor - RGB and HLS combined in one class | CodeGuru

CColor – RGB and HLS combined in one class

The CColor class emerged from the need to manipulate color in the HLS modell, this means to directly change attributes like luminance or saturation. The HLS modell makes it very easy, to draw such things as color gradients between any color, too. Perhaps NT 5.0 will support the HLS (or the similary HSB) color modell, […]

Written By
CodeGuru Staff
CodeGuru Staff
Feb 23, 1999
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

The CColor class emerged from the need to manipulate color
in the HLS modell, this means to directly change attributes like luminance or saturation.
The HLS modell makes it very easy, to draw such things as color
gradients
between any color, too. Perhaps NT 5.0 will support the HLS (or the similary
HSB) color modell, but I do not want to wait until the release, on the other side, my code
should also work under NT 4.0 and Win95/98. The CColor class
encapsulates the long known type COLORREF and extends it with the HLS color modell. An CColor object represents a color, whose RGB and HLS properties
can be directly read and manipulated. It makes the use of type COLORREF and the associated
macros superfluous.

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.

colorapp.jpg (28672 Byte)

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

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.