Color
Every control defines a ForeColor and BackColor property. For different controls, these properties have slightly different meanings. In a simple control like a label or text box, the foreground color is the color of the text, while the background color is the area behind it. These values default to the Windows system-configured settings.
Colors are specified as Color structures from the System.Drawing namespace. It's extremely easy to create a color object, because you have several different options. You can create a color using:
An ARGB (alpha, red, green, blue) color value. You specify each value as integer.
An environment setting from the current color scheme. You choose the correspondingly named property from the SystemColors class.
A predefined .NET color name. You choose the correspondingly named property from the Color class.
An HTML color name. You specify this value as a string using the Color-Translator class.
An OLE color code. You specify this value as an integer (representing a hexa-decimal value) using the ColorTranslator class.
A Win32 color code. You specify this value as an integer (representing a hexadecimal value) using the ColorTranslator class.
The code listing that follows shows several ways to specify a color using the Color, ColorTranslator, and SystemColors types. In order to use this code as written, you must import the System.Drawing namespace.
// Create a color from an ARGB value
int alpha = 255, red = 0;
int green = 255, blue = 0;
ctrl.ForeColor = Color.FromARGB(alpha, red, green, blue);
// Create a color from an environment setting
ctrl.ForeColor = SystemColors.HighlightText;
// Create a color using a .NET name
ctrl.ForeColor = Color.Crimson;
// Create a color from an HTML code
ctrl.ForeColor = ColorTranslator.FromHtml("Blue");
// Create a color from an OLE color code
ctrl.ForeColor = ColorTranslator.FromOle(OxFF00);
// Create a color from a Win32 color code;
ctrl.ForeColor = ColorTranslator.FromWin32(0xA000);
The next code snippet shows how you can transform the KnownColors enumeration into an array of strings that represent color names. This can be useful if you need to display a list of valid colors (by name) in an application.
String[] colorNames;
colorNames = System.Enum.GetNames(typeof(KnownColor));
Changing a color name string back to the appropriate enumerated value is just as easy using the special static Enum.Parse() method. This method compares the string against all the available values in an enumeration, and chooses the matching one.
KnownColor myColor;
myColor = (KnownColor)System.Enum.Parse(typeOf(KnownColor), colorName);
// For example, if colorName is "Azure" then MyColor will be set
// to the enumerated value KnownColor.Azure (which is also the integer value 32).
Incidentally, you can use a few useful methods on any Color structure to retrieve color information. For example, you can use GetBrightness(), GetHue(), and GetSaturation().
Here's a complete program that puts all of these techniques to work. When it loads, it fills a list control with all the known colors. When the user selects an item, the background of the form is adjusted accordingly (see Figure 3-4).

Figure 3-4: A color changing form
public class ColorChange : System.Windows.Forms.Form
{
// (Windows designer code omitted.)
System.Windows.Forms.ListBox lstColors;
private void ColorChange_Load(object sender, System.EventArgs e)
{
string[] colorNames;
colorNames = System.Enum.GetNames(typeof(KnownColor));
lstColors.Items.AddRange(colorNames);
}
private void lstColors_SelectedIndexChanged(object sender,
System.EventArgs e)
{
KnownColor selectedColor;
selectedColor = (KnownColor)System.Enum.Parse(
typeof(KnownColor), lstColors.Text);
this.BackColor = System.Drawing.Color.FromKnownColor(selectedColor);
// Display color information.
lblBrightness.Text = "Brightness = " +
this.BackColor.GetBrightness().ToString();
lblHue.Text = "Hue = " + this.BackColor.GetHue().ToString();
lblSaturation.Text = "Saturation = " +
this.BackColor.GetSaturation().ToString();
}
}