Color Transformation in C# GDI+ Programming - Introduction to Color Transformation
(Page 2 of 4 )
Tostoreand manipulateimages,GDI+ provides the Image and Bitmap classes, both of whichstore the color of each pixel as a 32-bit number: 8 bits each for red, green, blue, and alpha.Each of the four components is a number from 0 through 255, with 0 representing no intensity and 255 representing full intensity. The alpha component specifies the transparency of the color; 0 is fully transparent and 255 is fully opaque.
Author’s Note: transparency is one of the attractive characteristics of GDI+, which in some degree increases the complexity of color computation.
Therefore,a colorcan be described asa 4-dimensional vector(red, green, blue, alpha).For example, the color vector (255,0, 0, 255) represents acompleteopaque color that has nogreenor blue, but hasredat full intensity. This means that we can use a4×4colortransformationmatrix(see Figure 2) to modify any one of the four elements.
Figure 2—use a4 ×4colortransformationmatrixto change an element of a color

For example, to change color (0, 255, 0, 255) into semitransparent, we can replace the value of Alpha with 50% of its initial value. Figure 3 shows the related calculation.
Figure 3—change color (0, 255, 0, 255) into semitransparent

Wecan apply linear transformations (rotation, scaling, and the like) to color vectors by multiplying by a 4×4 matrix.However,wecannot use a 4×4 matrix to perform a nonlinear transformation, such astranslation.For this, we can introducea 5×5 matrix in order to apply any combination of linear transformations and translationssimultaneously by simply addinga dummy fifth coordinate to each of the color vectors.A transformation consisting of a linear transformation followed by a translation is called an affine transformation.A 5 ×5 matrix that represents an affine transformation is called a homogeneous matrix for a 4-space transformation. The element in the fifth row and fifth column of a 5 ×5 homogeneous matrix must be 1. All of the other entries in the fifth column must be 0.
For example, supposewewant to start with the color (25,100,100,255) andachievethe followingoperations.
Double the red component
Add 100 to the green component
We cannot achieve the two targets by just using 4x4 matrix multiplications. We have to resort to the above 5x5matrix that represents an affine transformation. The following Figure 4 shows how to perform the pair of transformations in the order listed.
Figure 4—performan affine transformationby adding a dummy coordinate

The above matrix calculation indicates that it’s necessary to use 5x5 matrixes to perform a color transformation.
However, before delving into the color transformation matrix, let’s introduce another important concept—color component saturation. If the value of the red component of a pixel is A, then the value of A/255 is called the red color component saturation.
GDI+ uses the color component saturation to represent the value of a color component in a pixel. For example, if the RGBA value of a pixel is (0, 255, 0, 255), then in terms of the color component saturation, the related representation is (0, 1, 0, 1). In other words, to change the value of a color component is to alter the color component saturation.
Now let’s examine what the color transformation matrix is.
In fact, a color transformation matrix is a kind of method to calculate the color component saturation. By default, a color transformation matrix can be defined as shown in Figure 5.
Figure 5—use identity matrix to define a default color transformation matrix

According to the definition of matrix calculation, any 1x5 vector multiplied by the above 5x5 identify matrix will keep the initial value. However, to alter the value in the main diagonal corresponds to performing multiplication with RGB components, while modifying other values relates to performing addition or subtraction with the RGB component saturation.
In GDI+, it’s through the function SetColorMatrix() of the ImageAttributes object that we achieve the alteration to a color transformation matrix. The following snippet lists several outlined signatures of the SetColorMatrix() function:
ImageAttributes.SetColorMatrix (ColorMatrix newColorMatrix);
ImageAttributes.SetColorMatrix (ColorMatrix newColorMatrix, ColorMatrixFlag mode);
ImageAttributes.SetColorMatrix (ColorMatrix newColorMatrix, ColorMatrixFlag mode, ColorAdjustType type);
For a more detailed explanation of the parameters used in the SetColorMatrix() function and a discussion of matrices and transformations, please refer to coordinate systems and transformations in MSDN. Now let's start to write a sample that uses color transformation matrices to change the color components.
Next: A Sample that Uses Color Transformation Matrices >>
More C# Articles
More By Xianzhong Zhu