ColorMatrix Basics - Simple Image Color Adjustment - CodeProject
ColorMatrix Basics - Simple Image Color Adjustment - CodeProject
Licence CPOL
Introduction
This article discusses color operations on digital images, using the new ColorMatrix class provided
by GDI+. The ColorMatrix is a welcome addition to the GDI library, especially with the increase in
demand of digital imaging applications, as more and more consumer products are made available. This
class, as well as many other new GDI classes, provide more control to the developer and reduce
dependence on 3rd party applications such as LEAD tools, and others. Some basic knowledge of matrix
operations (multiplication, addition, etc), the RGBA colorspace and GDI+ is assumed.
Background
ColorMatrix operations are performed in the RGBA colorspace (red, green, blue, alpha). A
ColorMatrix consists of a 5x5 matrix, with color values normalized to 1 for full intensity (255 ->
1.0). You might expect the matrix to be 4x4 ( [R, G, B, A] ), which would be sufficient if we only
needed to perform linear transformations (multiplication: scaling, rotation, etc). However, one of the
most frequent color manipulations, color adjustment, requires adding color values. This is a non-linear
operation, referred to as a translation. Adding a 5th element to the color vector ( [R, G, B, A, w] )
combines these two operations, linear and non-linear, into a single operation called an affine
transformation. The 5th element of the color vector is simply a dummy element, always with a value of
1, which only serves to allow a translation (addition) of the color vector.
The example below scales the color vector [255, 128, 102, 255] by .5 and then adds a value of 26 to
the R, G and B components, leaving the A component at full intensity. Remember that the component
values are normalized, with full intensity, 255, equal to 1.0 (values have been rounded to the nearest
tenth). Also notice the addition of the 5th element to the color vector, which is simply ignored in the
1 de 4
resultant color vector.
Now that we've covered the basic principle of the ColorMatrix and it's operations on color vectors,
we can start exploring some practical uses.
Color adjustment is one of the more common color operations applied to digital images. The code to do
this might look as follows:
sr = red / 255
sg = green / 255
sb = blue / 255
sa = alpha / 255
End Function
Try
Dim bmp As New Bitmap(img) ' create a copy of the source image
imgattr.SetColorMatrix(cm)
' draw the copy of the source image back over the original image,
2 de 4
g.DrawImage(bmp, rc, 0, 0, img.Width, img.Height, _
GraphicsUnit.Pixel, imgattr)
g.Dispose()
Return True
Catch
Return False
End Try
End Function
End Function
End Function
Color channel separations, alpha transparency adjustment, image toning (Sepia, etc) are just a few
more common operations that can be easily performed with a ColorMatrix.
License
This article, along with any associated source code and files, is licensed under The Code Project Open
License (CPOL)
Michael Combs Michael has been developing software for about 12 years primarily in
Fortran, C/C++, Visual Basic and now .NET. His previous experience includes
Architect Internet data services (communication, data storage and mapping) for the
mortgage industry, oil platform instrumentation and explosives simulation
United States and testing. He holds a B.S. in astrophysics and computer science. He is
currently working for Global Software in Oklahoma City developing law
Member enforcement and emergency services related software.
3 de 4
Comments and Discussions
65 messages have been posted for this article Visit http://www.codeproject.com/KB/GDI-
plus/colormatrix.aspx to post and view comments on this article, or click here to get a print view
with messages.
4 de 4