Colors


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 of one of the more than 16 million colors that doesn't come prebuilt for me, and this means that I first need to specify the color in which I'm interested. Color is modeled in .NET via the Color structure:

 struct Color {   // No color   public static readonly Color Empty;   // Built-in colors   public static Color AliceBlue { get; }   // ...   public static Color YellowGreen { get; }   // Properties   public byte A { get; }   public byte B { get; }   public byte G { get; }   public bool IsEmpty { get; }   public bool IsKnownColor { get; }   public bool IsNamedColor { get; }   public bool IsSystemColor { get; }   public string Name { get; }   public byte R { get; }   // Methods   public static Color FromArgb(int alpha, Color baseColor);   public static Color FromArgb(int alpha, int red, int green, int blue);   public static Color FromArgb(int argb);   public static Color FromArgb(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(); } 

Fundamentally, 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 amount of color. The degree of opacity is specified by an alpha value, which is sometimes seen together with RGB as ARBG (Alpha-RBG). 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 FromArbg method, passing brightness settings of red, green, and blue:

 Color red =   Color.FromArgb(255, 0, 0); // 255 red, 0 blue, 0 green Color green = Color.FromArgb(0, 255, 0); // 0 red, 255 blue, 0 green Color blue =  Color.FromArgb(0, 0, 255); // 0 red, 0 blue, 255 green Color white = Color.FromArgb(255, 255, 255); // white Color black = Color.FromArgb(0, 0, 0); // black 

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); Color blue75PercentOpaque = Color.FromArgb(  255*3/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 adaptors can handle. If you prefer to pass the four values combined into the single 32-bit value, you can do that with another of the overloads, although it's fairly awkward and therefore usually avoided:

 // A = 191, R = 0, G = 0, B = 255 Color blue75PercentOpache = Color.FromArgb(  -1090518785  ); 

Known Colors

Often, the color you're interested in already has a well-known name, and this means that it will already be available from the static fields of Color that define known colors, from the KnownColor enumeration, and by name:

 Color blue1 = Color.BlueViolet; Color blue2 = Color.FromKnownColor(KnownColor.BlueViolet); Color blue3 = Color.FromName("BlueViolet"); 

In addition to 141 colors with names such as AliceBlue and OldLace, the KnownColor enumeration has 26 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:

 enum KnownColor {   // Nonsystem colors elided...   ActiveBorder,   ActiveCaption,   ActiveCaptionText,   AppWorkspace,   Control,   ControlDark,   ControlDarkDark,   ControlLight,   ControlLightLight,   ControlText,   Desktop,   GrayText   Highlight,   HighlightText,   HotTrack,   InactiveBorder,   InactiveCaption,   InactiveCaptionText,   Info,   InfoText,   Menu,   MenuText,   ScrollBar,   Window,   WindowFrame,   WindowText, } 

If you'd like to use one of the system colors without creating your own instance of the Color class, you can access them already created for you and exposed as properties of the SystemColors class:

 sealed class SystemColors {   // Properties   public static Color ActiveBorder { get; }   public static Color ActiveCaption { get; }   public static Color ActiveCaptionText { get; }   public static Color AppWorkspace { get; }   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 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 MenuText { get; }   public static Color ScrollBar { get; }   public static Color Window { get; }   public static Color WindowFrame { get; }   public static Color WindowText { get; } } 

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 Translation

If you have a color in one of three other formats ”HTML, OLE, or Win32 ”or 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 as well as the color's name, whether it's a known color or a system color. You can also use these values to fill and frame shapes , which require brushes and pens, respectively.



Windows Forms Programming in C#
Windows Forms Programming in C#
ISBN: 0321116208
EAN: 2147483647
Year: 2003
Pages: 136
Authors: Chris Sells

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net