Translate

Friday 21 March 2014

Holi Hai ! :) List of Colors - C#

Working with colors is always fun but for that you must have the colors first.

It is like an old joke of mine -

To be or not to be a Macbeth is not the question; the real question is, Lady Macbeth has to be for Macbeth to be or not to be!

So, when I ventured to have a user select a color from a drop-down list for a software module, I was a little surprised (like the cat in the tree on seeing a dog climbing up to say hello to her) that it was not as simple as that.

Lady Macbeth may have known how to poison her husband but she had the poison and that is the crux of the matter - a resourceful woman, for her times!

Then I chanced upon the fact (Google search is almost a fact now) that Reflection (as in System.Reflection) had the solution but the real problems are faced only when you work towards the solution.

Lady Macbeth, too, must have thought it would be hunky-dory all the way until she found  that she really could not taste the poison herself to know what quantity to give to Macbeth ! :D

It is not as if she had to mix Coke or Pepsi with some other drink - she really could not know the taste of poison so, how could she ensure that her prey, her husband, would not find it distasteful and throw it away before drinking it?

The problem is that System.Drawing.Color is not really an enumeration that can be enumerated upon as it does not have a colors collection! So, how to compare the color values after the user inputs the color, when asked "Tippi, tippi, tap, what color do you want?" ?

The solution is to compare the name of the colors by storing the 'reflected' colors into an array of strings and then compare using a foreach loop to match the selected color.

Below is the code:

public class MyColorList{

private string[] colors = null;
private string colorSelected = null;

            private void listAllColors(){
                int colorIndex = 0;
                System.Type colorType = typeof(System.Drawing.Color);
                System.Reflection.PropertyInfo[] colorInfoList =                                                                                               colorType.GetProperties(System.Reflection.BindingFlags.Static |
                    System.Reflection.BindingFlags.DeclaredOnly | System.Reflection.BindingFlags.Public);

                colors = new string[colorInfoList.Length];

                foreach (System.Reflection.PropertyInfo propertyColor in colorInfoList) {
                    colors[colorIndex] = propertyColor .Name ;
                    colorIndex++;
                }
            }
            private string getColor(string colorName)
            {
                foreach (string color in colors)
                {
                    if (colorName.ToLower() == color.ToLower())
                        colorSelected= color;
                }
                return colorSelected;
            }
}
// Method Calls
            listAllColors();
            currentColor = getColor(ColorAsString);

(c) Of course, needless to say, what happened between Lady Macbeth and Macbeth was their own private affair and none of the world's business but because Shakespeare disagreed with the view that I thought of highlighting it, safe with the knowledge that it was probably not as private as the English Royalty would have it, if it was discussed so vividly in public.

No comments: