Introduction to Colors

Overview

The color is a non-spatial object that is used to indicate the presence of an object. To support colors, the .NET Framework provides a structure named Color:

public readonly struct Color : IEquatable<System.Drawing.Color>

The Color structure is defined in the System.Drawing namespace.

Practical LearningPractical Learning: Introducing Colors

  1. Start Microsoft Visual Studio
  2. Create a Windows Forms App named DrawingAccessories

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.

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, type a Color object 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 an overloaded static method named FromArgb. It is provided in various 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 defining 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 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 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);
}

The Red-Green-Blue Values of a Color

Whether a color was initialized with one of the 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 known 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. Its syntax is:

public KnownColor ToKnownColor();

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

A Color as an Object

A Color Variable

The Color structure is primarily a type like any other. For this reason, you can declare a variable of a Color object. Here is an example:

private void Draw()
{
    Color clr = Color.ForestGreen;
}

You can then ignore or use the variable as you see fit.

A Color as an Argument

You can also pass a color as argument. Here is an example:

private void Draw(Color clr)
{

}

In the body of the function or method, you can ignore or use the color.

A Property of a Color Type

In a class, you can create a property whose type is the Color structure. You can create a complete property that uses a private field. Here is an example:

public class Drawer
{
    private Color clr;

    public Drawer(Color pen)
    {
        clr = pen;
    }

    public Color Color
    {
        get
        {
            return clr;
        }
        set
        {
            clr = value;
        }
    }
}

Otherwise, you can create the property as an automatic one.

Practical LearningPractical Learning: Ending the Lesson


Previous Copyright © 2010-2024, FunctionX Thursday 25 Apil 2024, 22:15 Next