Most current display device technology defines colors by breaking them up into their three basic components: red, green, and blue. Depending on the configuration of the display device, these components usually will have a value that ranges from 0 to 255. The principle is that by combining different amounts of red, green, and blue, you can generate any color. Thus, many of today's display devices can display up to 16,777,216 (256 cubed) unique colors.

But the story doesn't end there. Colors also provide an alpha component. This component represents how transparent the color is. If the alpha value is 0, then the color is completely transparent (a kind of useless color), and a value of 255 is completely opaque. In between these two points are varying degrees of transparency that will, when drawn to the screen, merge with any color already existing at that location. You see this effect used most often in computer games.

Many of the Graphics class's Drawing methods need a System::Drawing::Color structure containing one of the colors built from the values described previously before they can be used. The Color class has a number of members (see Table 11-11) available to get color information from. You can use only three common methods to place color information into a Color structure:

  • FromArgb() returns a Color class based on the alpha, red, green, and blue values passed to it.

  • FromKnownColor() returns a Color class based on a predefined color.

  • FromName() returns a Color class based on the string color name passed.

Table 11-11: Common Color Members




Gets the alpha component


Gets the blue component


Gets the green component


Gets the brightness of the color based on the hue-saturation-brightness (HSB) value of the color


Gets the hue of the color, based on the HSB value of the color


Gets the saturation of the color, based on the HSB value of the color


True if it is a known color


True if it is a named color


True if it is a system color


Gets the name of a "named" color


Gets the red component


Gets the 32 -bit ARGB value of the color


Gets the KnownColor value of the color

You must use one of these three methods to create your color because there is no Color constructor.

There are two basic methods of defining a Color class: defining it using a combination of red, green, blue, and alpha component values or selecting the color from a list of predefined colors.

Custom Colors

To build your own custom color, you need to use the Color class's FromArgb() method. There are several overloads of the method, but you will most likely use two of them. The first method takes only the red, green, and blue components and defaults the alpha component to opaque (255). The second method allows you to specify the alpha component.

 // Pure red Color red1 = Color::FromArgb(255, 0, 0); Color red2 = Color:: FromArgb(255, 255, 0, 0); //Pure green Color green1 = Color::FromArgb(0, 255, 0); Color green2 = Color::FromArgb(255, 0, 255, 0); //Pure blue Color blue1 = Color::FromArgb(0, 0, 255); Color blue2 = Color::FromArgb(255, 0, 0, 255); 

You can make transparent or semitransparent colors by adjusting the alpha component passed to the FromArgb() method:

 Color transparentgray = Color::FromArgb(127, 127, 127, 127); 

Named Colors

The Color class provides a large number of predefined colors or named colors. There are two types of named colors. The first is a name that describes the color. These types of colors range (alphabetically) from AliceBlue to YellowGreen. The second type of color uses a name that describes its role in the Windows standard interface such as ControlText, ScrollBar, and Window.

The three ways of creating named colors are using the FromKnownColor() method, using the static named color method directly, or using the string name of the color.

 Color c1 = Color::FromKnownColor(KnownColor::AliceBlue); Color c2 = Color::AliceBlue; Color c3 = Color::FromName(S"AliceBlue"); 

Managed C++ and. NET Development
Managed C++ and .NET Development: Visual Studio .NET 2003 Edition
ISBN: 1590590333
EAN: 2147483647
Year: 2005
Pages: 169 © 2008-2017.
If you may any questions please contact us: