Click here to Skip to main content
6,597,576 members and growing! (18,261 online)
Email Password   helpLost your password?
Languages » C# » General     Intermediate

Use Reflection To Generate Complete Color Chart

By Softomatix

Artcile on use of reflection to get color values to draw the complete color chart
C#, Windows, .NET 1.0, Visual Studio, Dev
Posted:15 Apr 2002
Views:55,810
Bookmarked:15 times
Announcements
Loading...
 
Search    
Advanced Search
Add to IE Search
printPrint   add Share
      Discuss Discuss   Broken Article?Report  
8 votes for this article.
Popularity: 3.53 Rating: 3.91 out of 5

1

2

3
1 vote, 50.0%
4
1 vote, 50.0%
5

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.

  • Get Type of System.Drawing.Color strcuture.
  • Call GetProperties method on Type instance. Make sure that you use the appropriate BindingFlags attribute to get only the properties that are of interest. In our case we are only interested in public static properties. Therefore we will use BindingFlags.Public, BindingFlags.Static and BindingFlags.DeclaredOnly ORed together. The purpose of specifying BindingFlags.DeclaredOnly attribute is to get only the static properties that are declared only in this class only. We are not interested in properties of any parent class or structure.
  • Iterate through each item in PropertyInfo array value returned by GetProperties method. Call Name property on PropertyInfo object to get the name of each color. And call GetValue method to get the actual value of Color.
  • Rest is just implementation detail and book keeping on how to arrange all the colors on the bitmap and display it as a chart.

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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Softomatix


Member
To learn more about us, Please visit us at http://www.netomatix.com
Occupation: Web Developer
Location: United States United States

Other popular C# articles:

Article Top
You must Sign In to use this message board.
FAQ FAQ 
 
Noise Tolerance  Layout  Per page   
 Msgs 1 to 4 of 4 (Total in Forum: 4) (Refresh)FirstPrevNext
GeneralSame result without reflection? PinmemberAdrian Cole19:33 2 Aug '09  
GeneralExample Code Fix PinmemberCode Deamon4:32 28 Aug '07  
GeneralAn HTML chart? Pinmemberorcastorm17:31 16 Apr '05  
GeneralPNG image format PinmemberDrew Noakes8:06 24 Nov '04  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 15 Apr 2002
Editor: Nishant Sivakumar
Copyright 2002 by Softomatix
Everything else Copyright © CodeProject, 1999-2009
Web11 | Advertise on the Code Project