Colors


This section discusses the ways that you can specify what color you want something to be drawn in.

Colors in GDI+ are represented by instances of the System.Drawing.Color struct. Generally, once you’ve instantiated this struct, you won’t do much with the corresponding Color instance - you just pass it to whatever other method you are calling that requires a Color. You’ve encountered this struct before, when you set the background color of the client area of the window in each of the examples, as well as when you set the colors of the various shapes you were displaying. The Form.BackColor property actually returns a Color instance. This section looks at this struct in more detail. In particular, it examines several different ways that you can construct a Color.

Red-Green-Blue (RGB) Values

The total number of colors that can be displayed by a monitor is huge - more than 16 million. To be exact the number is 2 to the power 24, which works out to 16,777,216. Obviously, you need some way of indexing those colors so you can indicate which of these is the color you want to display at any given pixel.

The most common way of indexing colors is by dividing them into the red, green, and blue components. This idea is based on the theory that any color that the human eye can distinguish can be constructed from a certain amount of red light, a certain amount of the green light, and a certain amount of blue light. These colors are known as components. In practice, it’s found that if you divide the amount of each component light into 256 possible intensities, then that gives a sufficiently fine gradation to be able to display images that are perceived by the human eye to be of photographic quality. You, therefore, specify colors by giving the amounts of these components on a scale of 0 to 255 where 0 means that the component is not present and 255 means that it is at its maximum intensity.

This gives you your first way of telling GDI+ about a color. You can indicate a color’s red, green, and blue values by calling the static function Color.FromArgb(). Microsoft has chosen not to supply a constructor to do this task. The reason is that there are other ways, besides the usual RGB components, to indicate a color. Because of this, Microsoft felt that the meaning of parameters passed to any constructor they defined would be open to misinterpretation:

  Color redColor = Color.FromArgb(255,0,0); Color funnyOrangyBrownColor = Color.FromArgb(255,155,100); Color blackColor = Color.FromArgb(0,0,0); Color whiteColor = Color.FromArgb(255,255,255); 

The three parameters are, respectively, the quantities of red, green, and blue. This function has a number of other overloads, some of which also allow you to specify something called an alpha-blend (that’s the A in the name of the method, FromArgb()). Alpha blending is beyond the scope of this chapter, but it allows you to paint a color semitransparently by combining it with whatever color was already on the screen. This can give some beautiful effects and is often used in games.

The Named Colors

Constructing a Color using FromArgb() is the most flexible technique, because it literally means you can specify any color that the human eye can see. However, if you want a simple, standard, well-known color such as red or blue, it’s a lot easier to just be able to name the color you want. Hence, Microsoft has also provided a large number of static properties in Color, each of which returns a named color. It was one of these properties that you used when you set the background color of your windows to white in the examples:

  this.BackColor = Color.White; // has the same effect as: // this.BackColor = Color.FromArgb(255, 255 , 255); 

Several hundred such colors exist. The full list is given in the SDK documentation. They include all the simple colors: Red, White, Blue, Green, Black, and so on, as well as such delights as MediumAquamarine, LightCoral, and DarkOrchid. There is also a KnownColor enumeration, which lists the named colors.

Tip 

Each of these named colors represents a precise set of RGB values, and they were originally chosen many years ago for use on the Internet. The idea was to provide a useful set of colors right across the spectrum whose names would be recognized by Web browsers, thus saving you from having to write explicit RGB values in your HTML code. A few years ago these colors were also important because early browsers couldn’t necessarily display very many colors accurately, and the named colors were supposed to provide a set of colors that would be displayed correctly by most browsers. These days, that aspect is less important because modern Web browsers are quite capable of displaying any RGB value correctly. Web-safe color palettes are also available that provide developers with a comprehensive list of colors that work with most browsers.

Graphics Display Modes and the Safety Palette

Although in principle monitors can display any of the more than 16 million RGB colors, in practice this depends on how you’ve set the display properties on your computer. In Windows, there are traditionally three main color options (although some machines might provide other options depending on the hardware): true color (24 bit), high color (16 bit), and 256 colors. (On some graphics cards these days, true color is actually marked as 32 bit. This has to do with optimizing the hardware, though in that case only 24 bits of the 32 bits are used for the color itself.)

Only true color mode allows you to display all of the RGB colors simultaneously. This sounds like the best option, but it comes at a cost: 3 bytes are needed to hold a full RGB value, which means that 3 bytes of graphics card memory are needed to hold each pixel that is displayed. If graphics card memory is at a premium (a restriction that’s less common now than it used to be), you might want to choose one of the other modes. High color mode gives you 2 bytes per pixel. That’s enough to give 5 bits for each RGB component. So, instead of 256 gradations of red intensity, you just get 32 gradations; the same for blue and green, which produces a total of 65,536 colors. That is just about enough to give apparent photographic quality on a casual inspection, though areas of subtle shading tend to be broken up a bit.

The 256-color mode gives you even fewer colors. However, in this mode, you get to choose which colors. What happens is that the system sets up something known as a palette. This is a list of 256 colors chosen from the 16 million RGB colors. Once you’ve specified the colors in the palette, the graphics device will be able to display just those colors. The palette can be changed at any time, but the graphics device can only display 256 different colors on the screen at any one time. The 256-color mode is only used when high performance is necessary and video memory is at a premium. Most computer games use this mode, and they can still achieve decent-looking graphics because of a very careful choice of palette.

In general, if a display device is in high-color or 256-color mode and a particular RGB color is requested, it will pick the nearest mathematical match from the pool of colors that it is able to display. It’s for this reason that it’s important to be aware of the color modes. If you are drawing something that involves subtle shading or photographic-quality images, and the user does not have 24-bit color mode selected, she might not see the image the same way you intended it. So if you’re doing that kind of work with GDI+, you should test your application in different color modes. (It is also possible for your application to programmatically set a given color mode, though that is not discussed this in this chapter for lack of space.)




Professional C# 2005 with .NET 3.0
Professional C# 2005 with .NET 3.0
ISBN: 470124725
EAN: N/A
Year: 2007
Pages: 427

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net