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, 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.

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 ;-)

colorapp.jpg (28672 Byte)
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

  • Slices in the colorwheel pie graphic

    Posted by Legacy on 03/30/1999 12:00am

    Originally posted by: Gregory Franz

    I really like this class and example, but I am experiencing annoying slices in the round graphic. At various
    places, a slice in the round pie graphic appear and a color approaching gray is in the slice.
    
    

    I have used 16 bit, 24 bit and true color monitor display settings. After each setting, I reboot the machine and the slices still appear. I have even tried a new set of bitblt routines to no avail.

    I really feel this is a useful class but have no solution to make it pleasing to the eye.

    Reply
  • Great Class - but the tooltip really doesn't work under 5.0 that way

    Posted by Legacy on 03/15/1999 12:00am

    Originally posted by: Oscar

    Just let you know as you already wrote in the comments in your code the tooltips doesn't work that way in 5.0

    Very usefull class!
    Also the color circle - NICE !

    Reply
  • Excellent Color Wheel!

    Posted by Legacy on 03/01/1999 12:00am

    Originally posted by: Jake Helfert

    Great class. An excellent color wheel implementation!

    Thanks,

    Jake Helfert
    jakeh@trilobyte.net

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: April 21, 2015 @ 1:00 p.m. ET / 10:00 a.m. PT For mid-sized companies, the challenge of serving people and providing better service is forcing a re-evaluation of their contact centers to improve customer experiences. While these growing enterprises are driving much of the market's innovation, they must remain nimble in how they operate, and be very responsive to customer needs. This upcoming eSeminar reveals five ways mid-size organizations can gain needed agility and examines how delivering …

  • Data integrity and ultra-high performance dictate the success and growth of many companies. One of these companies is BridgePay Network Solutions, a recently launched and rapidly growing financial services organization that allows merchants around the world to process millions of daily credit card transactions. Due to the nature of their business, their IT team needed to strike the perfect balance between meeting regulatory-mandated data security measures with the lowest possible levels of latency and …

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date