Colors


So far, we've been drawing the ellipse in our 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 we're not quite happy with the dark blue brush. Instead, we'd like a brush of one of the more than 16 million colors that doesn't come prebuilt for us, and this means that we first need to specify the color in which we're interested. Color is modeled in .NET via the Color structure:

 
 Structure Color   ' No color   Public Shared ReadOnly Empty As Color   ' Built-in colors   Public Shared AliceBlue As Color   ' ...   Public Shared YellowGreen As Color   ' Properties   Property A() As Byte   Property B() As Byte   Property G() As Byte   Property IsEmpty() As Boolean   Property IsKnownColor() As Boolean   Property IsNamedColor() As Boolean   Property IsSystemColor() As Boolean   Property Name() As String   Property R() As Byte   ' Methods   Function Overloads FromArgb(alpha As Integer, baseColor As Color) _ As Color   Function Overloads FromArgb(alpha As Integer, red As Integer, _        green As Integer, blue As Integer) As Color   Function Overloads FromArgb(argb As Integer) As Color   Function Overloads FromArgb(red As Integer, green As Integer, _        blue As Integer) As Color   Function FromKnownColor(color As KnownColor) As Color   Function FromName(name As Sgtring) As Color   Public Function GetBrightness() As Double   Public Function GetHue() As Double   Public Function GetSaturation() As Double   Public Function ToArgb() As Integer   Public Function ToKnownColor() As KnownColor End Structure 

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:

 
 Dim red As Color = Color.FromArgb(255, 0, 0) ' 255 red, 0 green, 0 blue Dim green As Color = Color.FromArgb(0, 255, 0) ' 0 red, 255 green, 0 blue Dim blue As Color = Color.FromArgb(0, 0, 255) ' 0 red, 0 green, 255 blue Dim white As Color = Color.FromArgb(255, 255, 255) Dim black As Color = Color.FromArgb(0, 0, 0) 

If you'd like to specify the degree of transparency as well, you pass an alpha value:

 
 Blue25PercentOpaque As Color = Color.FromArgb(255*1/4, 0, 0, 255) Dim Blue75PercentOpaque As Color = 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 Dim blue75PercentOpaque = 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:

 
 Blue1 As Color = Color.BlueViolet Dim Blue2 As Color = Color.FromKnownColor(KnownColor.BlueViolet) Dim Blue3 As Color = 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    ' Non-system 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 End Enum 

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:

 
 NotInheritable Class SystemColors    ' Properties   Shared ReadOnly Property ActiveBorder() As Color   Shared ReadOnly Property ActiveCaption() As Color   Shared ReadOnly Property ActiveCaptionText() As Color   Shared ReadOnly Property AppWorkspace() As Color   Shared ReadOnly Property Control() As Color   Shared ReadOnly Property ControlDark() As Color   Shared ReadOnly Property ControlDarkDark() As Color   Shared ReadOnly Property ControlLight() As Color   Shared ReadOnly Property ControlLightLight() As Color   Shared ReadOnly Property ControlText() As Color   Shared ReadOnly Property Desktop() As Color   Shared ReadOnly Property GrayText() As Color   Shared ReadOnly Property Highlight() As Color   Shared ReadOnly Property HighlightText() As Color   Shared ReadOnly Property HotTrack() As Color   Shared ReadOnly Property InactiveBorder() As Color   Shared ReadOnly Property InactiveCaption() As Color   Shared ReadOnly Property InactiveCaptionText() As Color   Shared ReadOnly Property Info() As Color   Shared ReadOnly Property InfoText() As Color   Shared ReadOnly Property Menu() As Color   Shared ReadOnly Property MenuText() As Color   Shared ReadOnly Property ScrollBar() As Color   Shared ReadOnly Property Window() As Color   Shared ReadOnly Property WindowFrame() As Color    Shared ReadOnly Property WindowText() As Color End Class 

The following two lines yield Color objects with the same color values, and you can use whichever one you like:

 
 Color1 As Color = Color.FromKnownColor(KnownColor.GrayText) Dim Color2 As Color = 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:

 
 htmlBlue As Color = ColorTranslator.FromHtml("#0000ff") Dim htmlBlueToo As String = 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 Visual Basic .NET
Windows Forms Programming in Visual Basic .NET
ISBN: 0321125193
EAN: 2147483647
Year: 2003
Pages: 139

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