Skip to main content
Email Password   helpLost your password?

Sample Image - Reflection.jpg

Introduction

In GDI+, for drawing any item we need to pick up a color for either creating a brush or a pen. .NET framework has provided System.Drawing.Color structure which contains 141 colors. That makes life pretty easy. But there are some developers like me who have hard time corelating the color names with what actually they look like.

To solve this probelm, we decided to come up with a chart that will display all the colors along with the names used in Color structure. The crude approach could have been that we use a huge switch statement with each case representing the colors defined in the structure.

But there is a very nice approach that we took. .NET framework procides a very powerful feature called Reflection. All the colors in System.Drawing.Color structure are declared as public static properties. So the idea is pretty simple.

The complete code is attached with the article. Take a look at it for details.

Color testColor = Color.AliceBlue;
Type colorType = testColor.GetType();
if (null != colorType)
{
    PropertyInfo[] propInfoList =
     colorType.GetProperties(BindingFlags.Static|BindingFlags.DeclaredOnly
        |BindingFlags.Public);
    int nNumProps = propInfoList.Length;
    for (int i = 0; i < nNumRows; i++)
    {
        PropertyInfo propInfo = (PropertyInfo)propInfoList[nIdx];
        Color color = (Color)propInfo.GetValue(null, null);
        string strColorName = propInfo.Name;
    }
}                            
                                    

The complete color chart is available at our web site at Complete Color Chart.

You must Sign In to use this message board.
 
 
Per page   
 FirstPrevNext
GeneralSame result without reflection? Pin
Adrian Cole
19:33 2 Aug '09  
GeneralExample Code Fix Pin
Code Deamon
4:32 28 Aug '07  
GeneralAn HTML chart? Pin
orcastorm
17:31 16 Apr '05  
GeneralPNG image format Pin
Drew Noakes
8:06 24 Nov '04  


Last Updated 15 Apr 2002 | Advertise | Privacy | Terms of Use | Copyright © CodeProject, 1999-2009