GDIplus Graphics Transformation—Color Transformation and the Color Matrix


Jump to: navigation, search
CSharp-Online.NET:Articles
.NET Articles

GDI+ Graphics Transformation

© 2004 Pearson Education, Inc.

Color Transformation and the Color Matrix

So far we have seen the transformation of graphics shapes from one state to another, but have you ever thought about transforming colors? Why would you want to transform an image’s colors? Suppose you wanted to provide grayscale effects, or needed to adjust the contrast, brightness, or even "redness" of an image. For example, images retrieved from video and still cameras often need correction. In these cases, a color matrix is very useful.

As we discussed in earlier chapters, the color of each pixel of a GDI+ image or bitmap is represented by a 32-bit number, of which 8 bits each are used for the red, green, blue, and alpha components. Each of the four components is a number from 0 to 255. For red, green, and blue, 0 represents no intensity and 255 represents full intensity. For the alpha component, 0 represents transparent and 255 represents fully opaque. A color vector includes four items: A, R, G, and B. The minimum values for this vector are (0, 0, 0, 0), and the maximum values are (255, 255, 255, 255).

GDI+ allows the use of values between 0 and 1, where 0 represents the minimum intensity and 1 the maximum intensity. These values are used in a color matrix to represent the intensity and opacity of color components. For example, the color vector with minimum values is (0, 0, 0, 0), and the color vector with maximum values is (1, 1, 1, 1).

In a color transformation we can apply a color matrix on a color vector by multiplying a 4×4 matrix. However, a 4×4 matrix supports only linear transformations such as rotation and scaling. To perform nonlinear transformations such as translation, we must use a 5×5 matrix. The element of the fifth row and the fifth column of the matrix must be 1, and all of the other entries in the five columns must be 0.

The elements of the matrix are identified according to a zero-based index. The first element of the matrix is M[0][0], and the last element is M[4][4]. A 5×5 identity matrix is shown in Figure 10.21. In this matrix the elements M[0][0], M[1][1], M[2][2], and M[3][3] represent the red, blue, green, and alpha factors, respectively. The element M[4][4] means nothing, and it must always be 1.


Image:GraphicsProgGDIp10-21.gif
Figure 10.21. An identity matrix


Now if we want to double the intensity of the red component of a color, we simply set M[0][0] equal to 2. For example, the matrix shown in Figure 10.22 doubles the intensity of the red component, decreases the intensity of the green component by half, triples the intensity of the blue component, and decreases the opacity of the color by half (making it semitransparent).


Image:GraphicsProgGDIp10-22.gif
Figure 10.22. A matrix whose components have different intensities


In the matrix shown in Figure 10.22, we multiplied the intensity values. We can also add intensity values by using other matrix elements. For example, the matrix shown in Figure 10.23 will double the intensity of the red component and add 0.2 to each of the red, green, and blue component intensities.


Image:GraphicsProgGDIp10-23.gif
Figure 10.23. A color matrix with multiplication and addition


Previous_Page_.gif Next_Page_.gif


Personal tools