Click here to Skip to main content
Click here to Skip to main content

Color class with support for named colors and HSV values

, 22 Oct 2001
Rate this:
Please Sign up or sign in to vote.
An article describing the CColor class - an RGB encapsulation which supports named colors, system colors and translation to HSV values.

Introduction

Neither ATL nor MFC have a class that encapsulates color. In the Win32 API, color can be represented as either a COLORREF value or as an OLE_COLOR value. Translation between the two is not handled automatically. The system also has its own palette which can only be translated through the use of the GetSysColor function. The .NET library defines a color class that also has named colors. And lastly, often, when working with colors, programmers need access to the HSV (hue, saturation, and brightness) properties. The CColor class is my attempt to address all of these needs.

Class Members

Constructors

CColor(ULONG clrRef = -1L)

Initializes color to clrRef value. This value can be either a COLORREF value (provided by the RGB macro) or an OLE_COLOR value. If no value is provided, color is initialized as Empty (-1L).

CColor(LPTSTR szString)

Initializes color to parsed value of szString. The string format must either be a comma separated list of the decimal RGB values (e.g. "255,128,0") or the web color string format (e.g. "#ff8000").

CColor(BYTE R, BYTE G, BYTE B)

Initializes color to the RGB values provided by R, G, and B.

CColor(int nSysColorIndex)

Initializes color to the system color value of nSysColorIndex. The list of acceptable values can be found in the documentation for the GetSysColor API function.

CColor(CColor::KnownColor knownColor)

Initializes color to the value of the enumerated type value of knownColor. This constructor is only available if __KNOWN_COLORS__ is defined for the project.

Properties

R

Gets/sets the R (red) value of the color.

G

Gets/sets the G (green) value of the color.

B

Gets/sets the B (blue) value of the color.

Name

Gets the name of the color if it is a known color. Returns NULL if color is unknown. This property is only available if __KNOWN_COLORS__ is defined for the project.

Hue

Gets the hue of the color. This property is only available if __HSV_COLORS__ is defined for the project.

Saturation

Gets the saturation of the color. This property is only available if __HSV_COLORS__ is defined for the project.

Brightness

Gets the brightness (or value) of the color. This property is only available if __HSV_COLORS__ is defined for the project.

Operators

operator COLORREF() const

Returns the current COLORREF value of the color. If the color is a system color, it is translated to the actual RGB value.

CColor& operator = (ULONG clrRef)

Sets color to clrRef value. This value can be either a COLORREF value (provided by the RGB macro) or an OLE_COLOR value.

CColor& operator = (LPTSTR szString)

Sets color to parsed value of szString. The string format must either be a comma separated list of the decimal RGB values (e.g. "255,128,0") or the web color string format (e.g. "#ff8000").

CColor& operator = (int nSysColorIndex)

Sets color to the system color value of nSysColorIndex. The list of acceptable values can be found in the documentation for the GetSysColor API function.

CColor& operator = (CColor::KnownColor knownColor)

Sets color to the value of the enumerated type value of knownColor. This operator is only available if __KNOWN_COLORS__ is defined for the project.

bool operator < (const CColor& clr) const

Returns true only if current color has a lower HSV value than clr. This operator is only available if __HSV_COLORS__ is defined for the project.

bool operator == (ULONG clrRef) const

Returns true only if the translated value of clrRef is equal to the translated value of the current color.

bool operator == (LPTSTR szString) const

Returns true only if the parsed value of szString is equal to the translated value of the current color.

bool operator == (CColor::KnownColor knownColor) const

Returns true only if the current color is equal to knownColor.

Methods

void GetHSV(double& h, double& s, double& v) const

Gets the hue (h), saturation (s), and brightness (v) of the current color. This method is only available if __HSV_COLORS__ is defined for the project.

LPTSTR GetRGBString(LPTSTR szColor, CColor::StringFormat fmt = CColor::CSV) const

Builds a string pointed to by szColor and returns its value based on the specified format (fmt). Supported formats are:

  • CSV - Comma separated values of the RGB values (e.g. "128,128,128")
  • Web - Common web color format (#rrggbb) (e.g. "#808080")
  • HexPairs - Zero padded hex values of RGB colors (RRGGBB) (e.g. "808080")

bool IsEmpty() const

Returns true only if no color has been assigned.

bool IsSystemColor() const

Returns true only if color is defined as a system color.

bool IsKnownColor() const

Returns true only if color is one of the enumerable colors defined in the KnownColor enumerated type. This method is only available if __NAMED_COLORS__ is defined for the project.

History

23 Oct 2001 - Original submission.

Example Code

//
// Example of using the CColor class
//

// Set initial values using either constructors or assignment operators
CColor clr1(CColor::Control), clr2(RGB(0,0,255)), clr3, clr4, clr5;
clr3 = "255,15,1";
clr4 = "#ff0f01";

LPCTSTR szName;
bool b;

szName = clr1.Name;          // This will return "Control"
b = clr1.IsSystemColor();    // This will return true

szName = clr3.Name;          // This will return NULL
b = clr3.IsSystemColor();    // This will return false

if (clr3 == clr4)            // This will evaluate to true
  b = clr5.IsEmpty();        // This will return true

// Set R, G, and B values
clr5.R = 128;
clr5.G = 128;
clr5.B = 128;

if (clr5 < clr1)             // This will evaluate to true since clr1 is darker than clr5
  if (clr5.IsKnownColor())   // Evaluates to true since RGB(128,128,128) is known as Gray
    if (clr5 == CColor::Gray) {
      // Get the R, G, and B values for clr1
      BYTE r,g,b;
      r = clr1.R;
      g = clr1.G;
      b = clr1.B;
    }

// Get the HSV values of clr3
double h,s,v;
h = clr3.Hue;
s = clr3.Saturation;
v = clr3.Brightness;

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

Share

About the Author

David Hall
Architect
United States United States
I have been a Windows software developer since 1991. Most of what I create fills the need for some aspect of bigger projects that I consult on.

Comments and Discussions

 
GeneralNice job, but a minor conversion bug PinmemberVu H. Pham17-Sep-09 20:12 
GeneralLOL PinmemberChristian Rodemeyer24-Oct-01 8:29 
GeneralRe: LOL PinmemberDavid Hall24-Oct-01 10:48 
GeneralRe: LOL PinmemberChristian Rodemeyer25-Oct-01 8:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 23 Oct 2001
Article Copyright 2001 by David Hall
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid