RGB color model

VB.NET allows us to either use system-defined (built-in) color constants or define our own colors using the RGB color model.

The following is an example of the first option:

   Label1.ForeColor = Color.Red
This would assign the system-defined color Red to the ForeColor property of Label1. As you can guess, the label text would turn red on the screen.

You can use many other system-defined colors, for example:

   Color.Red
   Color.Blue
   Color.Green
   Color.Pink
   Color.Aqua

You will see a list of pre-defined colors whenever you type "color." while entering code.

These, and all other colors, are defined in terms of the RGB color model. The RGB model defines a color by giving the intensity level of red, green and blue light that mix together to create it on the display. With most of today's displays, the intensity of each color can vary from 0 to 255, which gives 16,777,216 different colors. (Older displays with less memory might only allow 256 colors, and really ancient displays might have only 16).

Color  Red  Green Blue 
Red25500
Green02550
Blue00255
Yellow2552550
Cyan0255255
Magenta2550255
White255255255
Black000

These are only a few of the possible colors. You can experiment with the RGB demonstration program to see these and others.

You can also check out this very large RGB display.

You can use the FromArgb() method of the class color to create a user-defined color. For example:

   Label1.ForeColor = Color.FromArgb(10, 100, 200)
would set the ForeColor of label1 to a bluish color.
   Label1.ForeColor = Color.FromArgb(200, 100, 10)
Would set it to an orangish color.
   Label1.ForeColor = Color.FromArgb(255,255,255)
Would set it to white, and so forth.

In these examples, FromArgb has had three arguments, but it can have an optional fourth argument. The fourth argument, if present, specifies the degree of transparency of the color. To set the transparency, you supply a fourth argument, which also runs from 0 (completely transparent) to 255 (completely opaque). For example:

   Label1.ForeColor = Color.FromArgb(127, 10, 100, 200)
would be a bluish color that was 50% transparent.

Are you wondering why the method is called FromArgb instead of FromRGB? The transparency level is typically referred to as the "alpha" level, so the method generates a color based on the alpha, red, green, and blue arguments.


Disclaimer: The views and opinions expressed on unofficial pages of California State University, Dominguez Hills faculty, staff or students are strictly those of the page authors. The content of these pages has not been reviewed or approved by California State University, Dominguez Hills.