The following is an example of the first option:
Label1.ForeColor = Color.RedThis 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 |
---|---|---|---|
Red | 255 | 0 | 0 |
Green | 0 | 255 | 0 |
Blue | 0 | 0 | 255 |
Yellow | 255 | 255 | 0 |
Cyan | 0 | 255 | 255 |
Magenta | 255 | 0 | 255 |
White | 255 | 255 | 255 |
Black | 0 | 0 | 0 |
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.