C#
  Home arrow C# arrow Page 2 - Color Transformation in C# GDI+ Programmin...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Mobile Linux 
App Generation ROI 
Windows Web Hosting
 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
C#

Color Transformation in C# GDI+ Programming
By: Xianzhong Zhu
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 1
    2008-07-16

    Table of Contents:
  • Color Transformation in C# GDI+ Programming
  • Introduction to Color Transformation
  • A Sample that Uses Color Transformation Matrices
  • Conclusion

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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.

    1. Double the red component

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

    More C# Articles
    More By Xianzhong Zhu


     

    C# ARTICLES

    - Working with Dates and Times in C#
    - Generics, Dictionaries, and More
    - More About Generics
    - Working with C# Collections
    - Generics
    - C# and XML
    - Pointers and Arrays in C#
    - C# 3.0 Extension Methods
    - Overloading Operators in C#
    - Iterators and Nullable Types
    - Patterns and Iterators in C#
    - C# Exceptions
    - Methods in C#
    - Delegates and Events in C#
    - Advanced C#

     
    Best Practices for Windows Vista Migration Presentation
    Dell and Microsoft recently held a series of face-to-face seminars entitled, &qu....

     
    Creating a Culture for Code Reuse
    If you oversee development teams you know that like it or not proprietary and ex....

     
    Keys to Web Application Acceleration: Advances in Delivery Systems
    Accelerate Web apps by up to 5x. Ensure significantly faster access to the Web a....

     
    Optimizing Application Monitoring
    Tired of finding out from your customers that you're offline? This white paper e....

     
    Solaris to Solaris Migration -- Migrating applications from Sun SPARC to Dell PowerEdge R900
    This comprehensive Migration Guide reviews the approach that Principled Technolo....

     




    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway
    Stay green...Green IT