Welcome to MSDN Blogs Sign in | Join | Help

A Simple LINQ Example with System.Reflection and System.Drawing.Color

Recently I needed to get see all the RGB values for the System.Drawing.Color values (for example System.Drawing.Color.Red, System.Drawing.Color.Aqua, etc.).

It's a very simple task that shows some of the expressive simplicity of LINQ.

Here's the sample using the LINQ method syntax:

using System;
using System.Linq;

namespace CmdLineTest
{
    internal class Program
    {
        [STAThread]
        private static void Main(string[] args)
        {
            var color_type = typeof(System.Drawing.Color);

            var color_properties = color_type.GetProperties()
                .Where(m => m.PropertyType == color_type );

            foreach (var color_property in color_properties)
            {
                var cur_color = (System.Drawing.Color)color_property.GetValue(null, null);
                uint rgb_int = (uint)cur_color.ToArgb() & (0x00ffffff);
                Console.WriteLine(" {0} = ({1},{2},{3}) = 0x{4:X00000000}",
                    color_property.Name,
                    cur_color.R, cur_color.G, cur_color.B,
                    rgb_int );
            }
        }
    }
}

Here's the sample using the LINQ query syntax:

using System;
using System.Linq;

namespace CmdLineTest
{
    internal class Program
    {
        [STAThread]
        private static void Main(string[] args)
        {
            var color_type = typeof(System.Drawing.Color);

            var color_properties = from prop in color_type.GetProperties()
                                   where prop.PropertyType == color_type
                                   select prop;

            foreach (var color_property in color_properties)
            {
                var cur_color = (System.Drawing.Color)color_property.GetValue(null, null);
                uint rgb_int = (uint)cur_color.ToArgb() & (0x00ffffff);
                Console.WriteLine(" {0} = ({1},{2},{3}) = 0x{4:X00000000}",
                    color_property.Name,
                    cur_color.R, cur_color.G, cur_color.B,
                    rgb_int );
            }
        }
    }
}

The output will look like this

Transparent = (255,255,255) = 0xFFFFFF
AliceBlue = (240,248,255) = 0xF0F8FF
AntiqueWhite = (250,235,215) = 0xFAEBD7
Aqua = (0,255,255) = 0xFFFF
Aquamarine = (127,255,212) = 0x7FFFD4
Azure = (240,255,255) = 0xF0FFFF
Beige = (245,245,220) = 0xF5F5DC
Bisque = (255,228,196) = 0xFFE4C4
Black = (0,0,0) = 0x0
BlanchedAlmond = (255,235,205) = 0xFFEBCD
Blue = (0,0,255) = 0xFF
BlueViolet = (138,43,226) = 0x8A2BE2
Brown = (165,42,42) = 0xA52A2A
BurlyWood = (222,184,135) = 0xDEB887
CadetBlue = (95,158,160) = 0x5F9EA0
Chartreuse = (127,255,0) = 0x7FFF00

For the record: my favorite is System.Drawing.Color.Tomato

Published Monday, August 04, 2008 9:02 AM by saveenr

Attachment(s): colors.txt

Comment Notification

If you would like to receive an email when updates are made to this post, please register here

Subscribe to this post's comments using RSS

Comments

# re: A Simple LINQ Example with System.Reflection and System.Drawing.Color

With LINQ, one of the ways to know that there may be an additional optimization you could make is to see if you have any foreach iterations in your code. In your case, you can be a bit more declaritive as follows:

var color_type = typeof(System.Drawing.Color);

var color_properties = from prop in color_type.GetProperties()

                      where prop.PropertyType == color_type

                      let color = (System.Drawing.Color)prop.GetValue(null, null)

                      let rgb_int = (uint)color.ToArgb() & (0x00ffffff)

                      select new { prop, color, rgb_int };

color_properties.ToList().ForEach(

   item => Console.WriteLine(" {0} = ({1},{2},{3}) = 0x{4:X00000000}",

        item.prop.Name, item.color.R, item.color.G, item.color.B, item.rgb_int));

Monday, August 04, 2008 4:40 PM by Jim Wooley

# re: A Simple LINQ Example with System.Reflection and System.Drawing.Color

Even better, thanks Jim!

Monday, August 04, 2008 5:35 PM by saveenr

Leave a Comment

(required) 
required 
(required) 

  
Enter Code Here: Required
 
Page view tracker