Color Mapping Using Color Objects

The System.Drawing.Imaging namespace provides three color objects that can be used to apply color mappings to images. These three objects are ColorMap, ColorMatrix, and ColorPalette. In this section we will discuss the use and importance of these objects.

8.3.1 The Color Remap Table

A color remap table is used to convert the existing colors of an image to new colors by applying a color mapping to them. The ColorMap class represents a color remap table. It defines the mapping between existing colors and the new colors to which they will be converted. When the map is applied to an image, any pixel of the old color is converted to the new color.

The ColorMap class has only two propertiesNewColor and OldColorboth of type Color. OldColor represents an existing color, and NewColor represents the new color to which the existing color will be converted.

A color map is applied to an image through the ImageAttributes parameter of DrawImage. The ImageAttributes class provides the SetRemapTable method, which is used to apply a ColorMap object array to the image attributes.

Note

Each ColorMap object maps a single color. To map multiple colors, you must create multiple ColorMap objects.

To see ColorMap in action, we create a Windows application and add a MainMenu control to the form. We also add three menu items to the main menu and use their menu item click event handlers to test our code.

Listing 8.8 gives code for the ColorMap menu click event handler. As usual, we create Graphics and Image objects. We will map the red, yellow, and blue colors to green, navy, and aqua, respectively. We create three ColorMap objects and a ColorMap array from these objects, and we set their OldColor and NewColor properties to the desired colors. Then we create an ImageAttributes object and apply the ColorMap array to it by calling the SetRemapTable method. After that the ImageAttributes object is used as a parameter of DrawImage.

Listing 8.8 Applying the color remap table

private void ColorMap_Click(object sender,
 System.EventArgs e)
{
 // Create a Graphics object
 Graphics g = this.CreateGraphics();
 g.Clear(this.BackColor);
 // Create an Image object
 Image image = new Bitmap("Sample.bmp");
 // Create ImageAttributes
 ImageAttributes imageAttributes =
 new ImageAttributes();
 // Create three ColorMap objects
 ColorMap colorMap1 = new ColorMap();
 ColorMap colorMap2 = new ColorMap();
 ColorMap colorMap3 = new ColorMap();
 // Set the ColorMap objects' properties
 colorMap1.OldColor = Color.Red;
 colorMap1.NewColor = Color.Green;
 colorMap2.OldColor = Color.Yellow;
 colorMap2.NewColor = Color.Navy;
 colorMap3.OldColor = Color.Blue;
 colorMap3.NewColor = Color.Aqua;
 // Create an array of ColorMap objects
 // because SetRemapTable takes an array
 ColorMap[] remapTable =
 {
 colorMap1,
 colorMap2,
 colorMap3
 };
 imageAttributes.SetRemapTable(remapTable,
 ColorAdjustType.Bitmap);
 // Draw image
 g.DrawImage(image, 10, 10, image.Width, image.Height);
 // Draw image with color map
 g.DrawImage(
 image,
 new Rectangle(150, 10, image.Width, image.Height),
 0, 0, image.Width, image.Height,
 GraphicsUnit.Pixel,
 imageAttributes);
 // Dispose of objects
 image.Dispose();
 g.Dispose();
}

Figure 8.7 shows the output from Listing 8.8. The original image is on the left; the image on the right shows remapped colors. On your system you will notice that the red, yellow, and blue colors are converted to green, navy, and aqua.

Figure 8.7. Applying a color remap table

graphics/08fig07.jpg

8.3.2 The Color Matrix

The ColorMatrix class defines a 5x5 matrix that contains coordinates for the ARGB (alpha, red, green, and blue) space (from 0,0 to 4,4). The Item property of this class represents a cell of the matrix and can be used to get and set cell values. Besides the Item property, the ColorMatrix class provides 25 MatrixXY properties, which represent items of the matrix at the xth row and yth column. The MatrixXY properties can be used to get and set item values.

You can use an array of points to initialize a ColorMatrix object, or you can assign values directly to the ColorMatrix properties. The following code snippet creates an array of points that is used as an argument to the ColorMatrix constructor, and then sets the values of Matrix34 and Matrix11.


 
float[][] ptsArray ={
 new float[] {1, 0, 0, 0, 0},
 new float[] {0, 1, 0, 0, 0},
 new float[] {0, 0, 1, 0, 0},
 new float[] {0, 0, 0, 0.5f, 0},
 new float[] {0, 0, 0, 0, 1}};
ColorMatrix clrMatrix = new ColorMatrix(ptsArray);
if( clrMatrix.Matrix34 <= 0.5) //3rd row, 4th col
{
 clrMatrix.Matrix34 = 0.8f;
 clrMatrix.Matrix11 = 0.3f; //1st row, 1st col
}

 

The SetColorMatrix method of the ImageAttributes class uses a color matrix. We will see how to use a color matrix in your applications in the sample applications that follow. Chapter 10 discusses ColorMatrix in more detail.

8.3.3 The Color Palette

A color palette defines an array of colors that make up a color palette. The colors in the palette are limited to 32-bit ARGB colors (8 bits each for the alpha, red, green, and blue components). The color palette can be used to increase the color intensity without increasing the number of colors used. This process creates a halftone, and it offers increased contrast at a cost of decreased resolution.

The ColorPalette class defines an array of colors that make up a color palette. This class has only two properties: Entries and Flags. The Entries property returns an array of colors, and the Flags property represents how the color information is interpreted. Table 8.6 lists valid values for the Flags property.

Table 8.6. ColorPalette.Flags values

Value

Description

0x00000001

The color values in the array contain alpha information.

0x00000002

The colors in the array are grayscale values.

0x00000004

The colors in the array are halftone values.

GDI+: The Next-Generation Graphics Interface

Your First GDI+ Application

The Graphics Class

Working with Brushes and Pens

Colors, Fonts, and Text

Rectangles and Regions

Working with Images

Advanced Imaging

Advanced 2D Graphics

Transformation

Printing

Developing GDI+ Web Applications

GDI+ Best Practices and Performance Techniques

GDI Interoperability

Miscellaneous GDI+ Examples

Appendix A. Exception Handling in .NET



GDI+ Programming with C#
GDI+ Programming with C#
ISBN: 073561265X
EAN: N/A
Year: 2003
Pages: 145

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