Managed C++ and .NET Development: Visual Studio .NET 2003 Edition
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.
MEMBER | DESCRIPTION |
---|---|
A | Gets the alpha component |
B | Gets the blue component |
G | Gets the green component |
GetBrightness() | Gets the brightness of the color based on the hue-saturation-brightness (HSB) value of the color |
GetHue() | Gets the hue of the color, based on the HSB value of the color |
GetSaturation() | Gets the saturation of the color, based on the HSB value of the color |
IsKnownColor() | True if it is a known color |
IsNamedColor() | True if it is a named color |
IsSystemColor() | True if it is a system color |
Name | Gets the name of a "named" color |
R | Gets the red component |
ToArgb() | Gets the 32 -bit ARGB value of the color |
ToKnownColor() | 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");
Категории