So far, I've been drawing the ellipse in my form using a built-in dark blue brush. A brush, as you'll see, is for filling the interior of a shape, whereas a pen is used to draw the edge of a shape. Either way, suppose I'm not quite happy with the dark blue brush. Instead, I'd like a brush composed from one of the more than 16 million colors available to me. Color is modeled in .NET via the Color structure: namespace System.Drawing { struct Color { // No color public static readonly Color Empty; // Built-in colors public static Color Transparent { get; } public static Color AliceBlue { get; } ... public static Color YellowGreen { get; } // Properties public byte A { get; } public byte R { get; } public byte G { get; } public byte B { get; } public bool IsEmpty { get; } public bool IsKnownColor { get; } public bool IsNamedColor { get; } public bool IsSystemColor { get; } public string Name { get; } // Methods public static Color FromArgb(int argb); public static Color FromArgb(int alpha, Color baseColor); public static Color FromArgb(int red, int green, int blue); public static Color FromArgb( int alpha, int red, int green, int blue); public static Color FromKnownColor(KnownColor color); public static Color FromName(string name); public float GetBrightness(); public float GetHue(); public float GetSaturation(); public int ToArgb(); public KnownColor ToKnownColor(); } } A Color object represents four values: the amount of red, green, and blue color and the amount of opacity. The red, green, and blue elements are often referred to together as RGB (red-green-blue), and each ranges from 0 to 255, with 0 being the smallest amount of color and 255 being the greatest. The degree of opacity is specified by an alpha value, which is sometimes seen together with RGB as ARGB (Alpha-RGB). The alpha value ranges from 0 to 255, where 0 is completely transparent and 255 is completely opaque. Instead of using a constructor, you create a Color object by using the static Color. FromArgb method, passing brightness settings of red, green, and blue: Color red = Color.FromArgb(255, 0, 0); // 255 R, 0 G, 0 B Color green = Color.FromArgb(0, 255, 0); // 0 R, 255 G, 0 B Color blue = Color.FromArgb(0, 0, 255); // 0 R, 0 G, 255 B Color white = Color.FromArgb(255, 255, 255); Color black = Color.FromArgb(0, 0, 0); If you'd like to specify the degree of transparency as well, you pass an alpha value: Color blue25PercentOpaque = Color.FromArgb(255 * 1/4, 0, 0, 255); The three 8-bit color values and the 8-bit alpha value make up the four parts of a single value that defines the 32-bit color that modern video display adapters can handle. If you prefer to pass the four values combined into the single 32-bit value, you can use another of the overloads, although it's fairly awkward and therefore usually avoided: // A=191, R=0, G=0, B=255 Color blue75PercentOpaque = Color.FromArgb(-1090518785); Known ColorsOften, the color you're interested in already has a well-known name, and this means that it is already available from the static fields of Color that define known colors, or from the KnownColor enumeration, or by name: Color blue1 = Color.BlueViolet; Color blue2 = Color.FromKnownColor(KnownColor.ActiveBorder); Color blue3 = Color.FromName("ActiveBorder"); In addition to 141 colors with names such as AliceBlue and OldLace, the KnownColor enumeration has 33 values describing the current colors assigned to various parts of the Windows UI, such as the color of the border on the active window and the color of the default background of a control. These colors are handy when you're doing custom drawing and you'd like to match the rest of the system. The system color values of the KnownColor enumeration are shown here:
namespace System.Drawing { enum KnownColor { // Nonsystem colors elided... ActiveBorder = 1, ActiveCaption = 2, ActiveCaptionText = 3, AppWorkspace = 4, ButtonFace = 168, // New ButtonHighlight = 169, // New ButtonShadow = 170, // New Control = 5, ControlDark = 6, ControlDarkDark = 7, ControlLight = 8, ControlLightLight = 9, ControlText = 10, Desktop = 11, GradientActiveCaption = 171, // New GradientInactiveCaption = 172, // New GrayText = 12, Highlight = 13, HighlightText = 14, HotTrack = 15, InactiveBorder = 16, InactiveCaption = 17, InactiveCaptionText = 18, Info = 19, InfoText = 20, Menu = 21, MenuBar = 173, // New MenuHighlight = 174, // New MenuText = 22, ScrollBar = 23, Window = 24, WindowFrame = 25, WindowText = 26 } } If you'd like to use one of the system colors without creating your own instance of the Color class, they are already created for you and exposed as properties of the SystemColors class:
namespace System.Drawing { sealed class SystemColors { public static Color ActiveBorder { get; } public static Color ActiveCaption { get; } public static Color ActiveCaptionText { get; } public static Color AppWorkspace { get; } public static Color ButtonFace { get; }// New public static Color ButtonHighlight { get; } // New public static Color ButtonShadow { get; } // New public static Color Control { get; } public static Color ControlDark { get; } public static Color ControlDarkDark { get; } public static Color ControlLight { get; } public static Color ControlLightLight { get; } public static Color ControlText { get; } public static Color Desktop { get; } public static Color GradientActiveCaption { get; } // New public static Color GradientInactiveCaption { get; } // New public static Color GrayText { get; } public static Color Highlight { get; } public static Color HighlightText { get; } public static Color HotTrack { get; } public static Color InactiveBorder { get; } public static Color InactiveCaption { get; } public static Color InactiveCaptionText { get; } public static Color Info { get; } public static Color InfoText { get; } public static Color Menu { get; } public static Color MenuBar { get; } // New public static Color MenuHighlight { get; } // New public static Color MenuText { get; } public static Color ScrollBar { get; } public static Color Window { get; } public static Color WindowFrame { get; } public static Color WindowText { get; } } } As an accessibility feature for vision-impaired users, you should use the SystemColors enumeration when your application needs to support high-contrast UIs.[5]
The following two lines yield Color objects with the same color values, and you can use whichever one you like: Color color1 = Color.FromKnownColor(KnownColor.GrayText); Color color2 = SystemColors.GrayText; Color TranslationIf you have a color in one of three other formatsHTML, Object Linking and Embedding (OLE), or Win32or you'd like to translate to one of these formats, you can use ColorTranslator, as shown here for HTML: Color htmlBlue = ColorTranslator.FromHtml("#0000ff"); string htmlBlueToo = ColorTranslator.ToHtml(htmlBlue); When you have a Color, you can get its alpha, red, blue, and green values (Color.A, Color.R, Color.B, Color.G) as well as the color's name (Color.Name), whether it's a known color (Color.IsKnownColor) or a system color (Color.IsSystemColor). You can also use these values to fill and frame shapes using brushes and pens, respectively. |