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.
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.
23 Oct 2001 - Original submission.
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;
b = clr1.IsSystemColor();
szName = clr3.Name;
b = clr3.IsSystemColor();
if (clr3 == clr4)
b = clr5.IsEmpty();
clr5.R = 128;
clr5.G = 128;
clr5.B = 128;
if (clr5 < clr1)
if (clr5.IsKnownColor())
if (clr5 == CColor::Gray) {
BYTE r,g,b;
r = clr1.R;
g = clr1.G;
b = clr1.B;
}
double h,s,v;
h = clr3.Hue;
s = clr3.Saturation;
v = clr3.Brightness;