zendevelop

learn & share

The Color Structure

In GDI+, colors are encapsulated in the Color structure, which you can employ in three ways: by using RGB values, by referencing predefined colors, or by using HSB values. GDI+ also have an alpha component.

RGB values

You can create a new instance of the Color structure by passing RGB values into a static method in the Color structure, like this:


Color c = Color.FromArgb( 100100255 );
Brush b 
= new SolidBrush( c );
g.FillRectangle( b, ClientRectangle );

Predefined Colors

Another way to specify the color is by name. There are 141 predefined colors you can reference by name. You use these colors by access public properties in the Color structure, as follow:


Color c1 = Color.LavenderBlush;
Color c2 
= Color.Red;

HSB values

The third way to specify color is to break it down into the following three components, which are collectively known as HSB model:

l        Hue: This is the actual color, based on its wave-length. Hue is defined by a floating-point value between 0 and 360. This represents an angle in degrees, which, in turn, represent the color taken from a color wheel.

Note A color wheel is a disc that contains all possible hues. There is a one-to-one relationship between the colors of the visible spectrum and the radius line of the disc.

l        Saturation: This is known as the color intensity. A high-saturation color will appear to be very pure. A low-saturation color will appear to be washed-out. The saturation level is defined by a floating-point number, with a value between 0 and 1. The saturation level has an inverse relationship with the purity of hue. The more other colors “interfere”, the more the color will tend towards white or gray and thus appear washed-out.

l        Brightness: This is the relative lightness or darkness of the color, expressed as a floating-point number between 0(black) and 1(white).

 

The Color structure contains static methods that allow you to get the HSB components: GetHue(), GetStauration(), GetBrightness(). The following show you how to use the methods:


Color c = Color.Blue;
float h = Color.GetHue();
float s = Color.GetSaturation();
float b = Color.GetBrightness();

The HSB model of color is not used much in programming. It is more common in graphic design and other such disciplines. When making custom controls, you primarily use the colors supplied by the system preferences set by the user.

posted on 2008-08-27 22:07  zendevelop  阅读(300)  评论(0编辑  收藏  举报

导航