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 Drawing Accessories

  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.

Introduction to Fonts

Overview

One of the characteristics of communication the written word. The group of symbols understandable to a particular language is its alphabet. Like everything else, an alphabet has a name, and the name of the alphabet is related to its corresponding language. For example, there is such a thing as the English alphabet, the German alphabet, and so on.

One of the particularities of a regular alphabet is that it must have a specific way to represent its primary basic symbols. In English, the basic symbols are A, B, C, D, E, F, G, H, I, J, K, L, M, N, O, P, Q, R, S, T, U, V, W, X, Y, and Z. There are two ways to represent a letter (in most Western languages): uppercase and lowercase.

Besides the regular letters of an alphabet, when creating or representing the written words, a language can add some signs to some words. This can assist in the pronunciation or the variations, which can subsequently make the language richer. For example, both the English and the French languages use the same alphabet, but the French language adds some signs to some of its letters. Examples are: "Ma marâtre est peut-être à côté du tronçon fluvial". Many other languages, derived from Latin or not, use this type of mechanism to complement their communication.

As mentioned already, the letters used in the alphabet and the digits used to represent numbers were created a long time ago. The people who created them designed a standard way of representing the letters. For example, they decided that the letter A would be represented as A. A few (hundred) years later, as aesthetic representation was developed to make things look good, some variations of representing the same letter were created. For example, here are three representations the the same word:

Notice that the S on each word is different. With the advent of computers, new demands for better and more sophisticated techniques of representing words were needed.

The Digits

The letters of an alphabet are typically used to create or represent spoken words. To count, to measure, to represent numbers, or to perform calculations, other symbols were created. The Latin-based languages use the symbols 0, 1, 2, 3, 4, 5, 6, 7, 8, and 9. Because there are 10 of them, they are referred to as digits. While most (or rather many) languages, including non-Latin-based languages, use or borrow these digits, some other languages use other means of representing numbers.

In spoken words, each of these symbols has a specific name.

To create a represent a number, you use one or a combination of digits.

Getting a Font

A font is an abstract (non-physical) object used to draw some symbols on paper, a computer monitor, or another device (PDA, oscillator, etc).

A font is created by a graphic artist or developed by a corporation. Once the font has been designed, it is created as a computer application and must be installed in the device that will use it. This means that the font must be installed in the computer, the printer, or the device that can then use it. This also means that there are various ways you can get a font to your device. Most computer-related objects (PCs, printers, oscillators, PDAs, etc) come with one or more fonts already installed. You can also purchase a font and install it on a computer. You can also download a free font from the Internet and install it.

The Characteristics of a Font

Introduction

A font is a GDI+ object, like a pen or a brush. Like any other object, the primary information of a font is its name. To see the names of fonts installed in a computer that is running Microsoft Windows, in the Control Panel, you can double-click Fonts. Here is an example:

Because is something that must be installed on, and can be removed from, the computer, it is likely that two computers would not have the same (list of) fonts. Still, because Microsoft Windows installs some default or popular fonts by default, there are fonts that are likely to be found on every regular installation of Microsoft Windows. This implies that, if you create a (commercial) application that you intend to distribute to people (customers) you do not know, you should assume that your audience would not have most or any of the fancy fonts you have in your computer.

You have two alternatives when selecting the fonts used in your application you intend to distribute. If you own the font, you can distribute it with your application. Another solution, easier solution, is to use only the most likely fonts to be found on most computers.

Creating a Font

In the .NET Framework, the primary piece of information about a font is held by a class named FontFamily. The FontFamly class is equipped with three constructors that each provides a different means of initializing a font.

The list of fonts installed in a a computer is stored in a property of the FontFamily class and that property is named Families. The FontFamily.Families property is collection member declared as a static property.

Like every GDI+ object, before using a font, you must create and initialize it.  To support fonts, the .NET Framework provides the Font class. This class is equipped with various constructors that each provides a particular means of initializing a font.

In order to use a font in an application, you must identify the FontFamily object you want to use and associate it with a Font.

The Name of a Font

The primary piece of information about a font is its name. As seen about, the names of fonts can be seen in the Fonts window of Control Panel. As mentioned already, the name of a font is held by the FontFamily class. If you know the name of the font you want to use, to create a font object (using that name), you can use the following constructor of the FontFamily class:

public FontFamily(string fontName);

This constructor takes as argument the name of the font you want to use. Here is an example of using it:

void btnWriteClick(object sender, EventArgs e)
{
    FontFamily fntWrite = new FontFamily("Times New Roman");
}

When using this constructor, you should make sure the font you use exists in the computer that will run the application. If you provide a name of a font that does not exist in the computer that is running the application, the compiler would throw an ArgumentException exception.

To get the name that a FontFamily object is using, get the value of its Name property. As you can guess, the name of a font is a string.

The Size of a Font

The size of a font is the width and the height that will be used to draw a character. It is important to know that this size refers to the visual 2-dimensional size and not to the memory size. This means that the letters i, N, g, M, l, and W obviously use different visual space but all of them use the same amount of memory space.

When creating a font, to specify its size, you can use the following constructor of the Font class:

public Font(string familyName, float emSize);

The first argument is the name of the font you want to use. The second argument is a floating-point number. Here is an example:

void btnWriteClick(object sender, EventArgs e)
{
  Font fntWrite = new Font("Verdana", 16.0F);
}

If you had stored the name of the font in a FontFamily variable and want to create a font, you can use the following constructor of the Font class:

public Font(FontFamily family, float emSize);

Here is an example of using this constructor:

void btnWriteClick(object sender, EventArgs e)
{
    FontFamily ffmWrite = new FontFamily("Times New Roman");
   Font fntWrite = new Font(ffmWrite, 16.0F);
}

To find out the size of an existing font, you can get the value of the Size property of a Font object.

The Style of a Font

To enhance the appearance of a characters, a font can be underlined, italicized, or bolded, etc. These are referred to as a style. To specify a style when creating a font, you can use the following constructor of the Font class:

public Font(string familyName, float emSize, FontStyle style);

The first argument is the name of the font. The second argument would be the size of the font. The third argument is of type FontStyle, which is an enumeration. The members of this enumeration are:

Here is an example of initializing a font by specifying its name, its size, and a style:

void btnWriteClick(object sender, EventArgs e)
{
    Font fntWrite = new Font("Verdana", 16.0F, FontStyle.Italic);
}

The above constructor is used to directly specify the name of the font. If you had stored the name of a font in a FontFamily object and you want to specify its style when initializing it, you can use the following constructor when declaring a Font variable:

public Font(FontFamily family, float emSize, FontStyle style);

Here is an example of using this constructor:

void btnWriteClick(object sender, EventArgs e)
{
    FontFamily ffmWrite = new FontFamily("Times New Roman");
    Font fntWrite = new Font(ffmWrite, 16.0F, FontStyle.Strikeout);
}

If you already have an existing font but want to apply a style to it, change its style, or apply a different style to it, you can use the following constructor of the Font class:

public Font(Font prototype, FontStyle newStyle);

Here is an example:

void btnWriteClick(object sender, EventArgs e)
{
    FontFamily ffmWrite = new FontFamily("Times New Roman");
    Font fntWrite = new Font(ffmWrite, 16.0F, FontStyle.Strikeout);
			
    . . . Use or don't use the font here
	
    fntWrite = new Font(fntWrite, FontStyle.Bold);
    . . . Use the new font here
}

You can also create a new font based on the previous one but with a different style. Here is an example:

void btnWriteClick(object sender, EventArgs e)
{
    FontFamily ffmWrite = new FontFamily("Times New Roman");
    Font fntWrite = new Font(ffmWrite, 16.0F, FontStyle.Strikeout);
			
    . . . Use or don't use the font here
	
    Font fntAnother = new Font(fntWrite, FontStyle.Bold);
    . . . Use the new font here
}

You can combine two or more styles to get a fancier effect. To combine the styles, you use the OR bit operator "|". Here are two examples:

void btnWriteClick(object sender, EventArgs e)
{
    FontFamily ffmWrite = new FontFamily("Times New Roman");
    Font fntWrite = new Font(ffmWrite, 16.0F, FontStyle.Strikeout | FontStyle.Italic);
	
    . . . Use or don't use the font here
			
    fntWrite = new Font(fntWrite, FontStyle.Italic | FontStyle.Bold | FontStyle.Underline);
	
    . . . Use the new font here
}

As opposed to applying a style, you may be interested to get the style that a font object holds:

Practical LearningPractical Learning: Ending the Lesson


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