GDI+ Accessories: The Color
The color is one of the most fundamental objects that enhances the aesthetic appearance of an object. The color is a non-spatial object that is added to an object to modify some of its visual aspects. To support colors, the GDI+ library provides the Color structure that is defined in the System.Drawing namespace.
A color is created as a combination of four 8-bit values. The first value is referred to as alpha but it is mostly used internally. The second is called red. The third is called green. The fourth is called blue:
Converted to decimal, each one of the red, green, and blue numbers would produce:
27 + 26 + 25 + 24 + 23 + 22 + 21 + 20
= 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1
Therefore, each number can have a value that ranges from 0 to 255 in the decimal system. The alpha section is reserved for the operating system. The other three numbers are combined to produce a single value as follows:
Converted to decimal, this number has a value of 255 * 255 * 255 = 16581375. This means that we can have approximately 16 million colors available. The question that comes to mind is how we use these colors, to produce what effect.
You computer monitor has a surface that resembles a series of tinny horizontal and vertical lines. The intersection of a horizontal line and a vertical line is called a pixel. This pixel holds, carries, or displays one color:
As the pixels close to each other have different colors, the effect is a wonderful distortion that creates an aesthetic picture. It is by changing the colors of pixels that you produce the effect of color variances seen on pictures and other graphics.
To make color selection easier, the Color structure is equipped with various properties that each represents a name for a color. Therefore, to use any of these colors, call the Color structure followed by the "." operator, followed by the desired color. All the popular names of colors are recognized and they are represented in the Color structure by static properties. These include Red, Green, Blue, Black, White, Yellow, Fuchsia, Silver, Gray, Brown, and Khaki, etc, just to name a few. There are many other colors that are not necessarily popular. Here is an example:
Imports System.Drawing Imports System.Windows.Forms Module Exercise Public Class Starter Inherits Form Private pnl As Panel Dim components As System.ComponentModel.Container Public Sub New() InitializeComponent() End Sub Public Sub InitializeComponent() pnl = New Panel() pnl.Location = New Point(10, 10) pnl.Size = New Size(200, 150) pnl.BackColor = Color.Turquoise Controls.Add(pnl) End Sub End Class Function Main() As Integer Dim frmStart As Starter = New Starter Application.Run(frmStart) Return 0 End Function End Module
This would produce:
If none of the pre-defined colors suits you, you can define your own color as a combination of red, green, and blue values. To create a color using this approach, you can declare a variable of type Color. To specify the characters of the color, the Color structure provides the FromArgb() static method overloaded in four versions as follows:
Public Shared Function FromArgb(argb As Integer) As Color Public Shared Function FromArgb(alpha As Integer, baseColor As Color) As Color Public Shared Function FromArgb(red As Integer, _ green As Integer, _ blue As Integer) As Color Public Shared Function FromArgb(alpha As Integer, red As Integer, _ green As Integer, blue As Integer ) As Color
The third version, which is the most used allows you to specify three values that each ranges from 0 to 255. Here is an example:
Public Sub InitializeComponent() pnl = New Panel() pnl.Location = New Point(10, 10) pnl.Size = New Size(200, 150) pnl.BackColor = Color.FromArgb(26, 69, 174) Controls.Add(pnl) End Sub
This would produce:
Instead of defined a color by its RGB composition, if you know the name of the color you want to use, the Color structure proposes a method named FromName that you can use. Its syntax is:
Public Shared Function FromName(name As String) As Color
This method expects as argument the name of the color. Here is an example:
Public Sub InitializeComponent() pnl = New Panel() pnl.Location = New Point(10, 10) pnl.Size = New Size(200, 150) pnl.BackColor = Color.FromName("LightBlue") Controls.Add(pnl) End Sub
This would produce:
When calling this method, make sure you know the name of the color you want to use. If you provide an unrecognized name, the compiler does not throw an exception but sets the values of the red, green, and blue so that the object may become transparent. Therefore, you should know the color to use but you cannot realistically know the names of all available colors. To assist you with identifying a color, the Color structure provides a method named FromKnownColor and its syntax is:
Public Shared Function FromKnownColor(color As KnownColor) As Color
This method takes as argument a member of an enumeration named KnownColor. The KnownColor enumeration holds the names of common colors (such as Red, Green, Blue, Yellow, Violet, et), the colors used on web pages (such as LightBlue or DarkGreen), the colors defined in Microsoft Windows (such as ActiveBorder, AppWorkspace, or ButtonFace, etc), and many others. Here is an example of calling this method:
Public Sub InitializeComponent() pnl = New Panel() pnl.Location = New Point(10, 10) pnl.Size = New Size(200, 150) pnl.BackColor = Color.FromKnownColor(KnownColor.DarkTurquoise) Controls.Add(pnl) End Sub
Whether a color was initialized with one of the Color pre-defined color properties, using the FromArgb(), the FromName(), or the FromKnownColor() methods, if you want to retrieve the red, green, and blue components of a color, you can use the R, the G, or the B properties to extract the value of each. Each one of these properties is of a byte type. Alternatively, you can call the Color.ToArgb() method. Its syntax is:
Public Function ToArgb As Integer
This method returns an integer.
We mentioned that the colors are commonly know by their names. While the ToArgb() method produces an integer that represents a color, if you instead want to get the color by its common or known name, the Color structure provides a method named ToKnownColor and whose syntax is:
Public Function ToKnownColor As KnownColor
This method returns a value that is based on the KnownColor enumeration.
|Home||Copyright © 2008-2016, FunctionX, Inc.|