Color Mapping Using Color Objects in GDI+


This article has been excerpted from book "Graphics Programming with GDI+".

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 ColorPaletter. In this section we will discuss the use and importance of these objects.

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 properties-NewColor and OldColor-both 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 objects 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 out 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.

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 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.

Figure 8.7.jpg

FIGURE 8.7: Applying a color remap table

        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 [] {1, 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 application in the sample application that follows. 

The Color Palette

A color palette defines an array of colors that makes 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 return 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

0x000000001

The color values in the array contain alpha information.

0x000000002

The color in the array are grayscale values.

0x000000004

The colors in the array are halftone values.

Conclusion

Hope the article would have helped you in understanding Color Mapping Using Color Objects in GDI+. Read other articles on GDI+ on the website.

bookGDI.jpg
This book teaches .NET developers how to work with GDI+ as they develop applications that include graphics, or that interact with monitors or printers. It begins by explaining the difference between GDI and GDI+, and covering the basic concepts of graphics programming in Windows.

Up Next
    Ebook Download
    View all
    Learn
    View all