Home

Applications Accessories: Color

     

Introduction

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.

   

Composing a Color

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:

Bits
Alpha
7 6 5 4 3 2 1 0
Red 
7 6 5 4 3 2 1 0
Green 
7 6 5 4 3 2 1 0
Blue 
7 6 5 4 3 2 1 0

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

= 255

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:

Color 
23 22 21 20 19 18 17 16 15 14 13 12 11 10 9 8 7 6 5 4 3 2 1 0
Blue Green Red
Value

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:

Pixel

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.

Using a Color

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:

private void btnBackColor_Click(object sender, EventArgs e)
{
            panel1.BackColor = Color.Turquoise;
}

Extracting a Color

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 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);

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:

private void btnBackColor_Click(object sender, EventArgs e)
{
            panel1.BackColor = Color.FromArgb(26, 69, 174);
}

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 static Color FromName(string name);

This method expects as argument the name of the color. Here is an example:

private void btnBackColor_Click(object sender, EventArgs e)
{
            panel1.BackColor = Color.FromName("LightBlue");
}

When calling this method, make sure you know the name of the color you want to use. If you provide an unregnized 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 static Color FromKnownColor(KnownColor name);

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:

private void btnBackColor_Click(object sender, EventArgs e)
{
            panel1.BackColor = Color.FromKnownColor(KnownColor.DarkTurquoise);
}

Extracting a Color

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 int ToArgb();

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 KnownColor ToKnownColor();

This method returns a value that is based on the KnownColor enumeration. 

 
 

Home Copyright © 2010-2011 C#Key