Getting a .NET Color Name

I needed to find out the standard .NET colour name for for a Color object, assuming the Color object was a standard .NET colour! As there is no built in way of doing this I created a simple helper class, the code is below.


using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Media;

namespace Chris.Blog
{
	public static class ColorToName
	{
		private static Dictionary<string, Color> Colours = new Dictionary<string, Color>();

		static ColorToName()
		{
			System.Reflection.PropertyInfo[] pinfo = typeof(System.Windows.Media.Colors).GetProperties();
			
			foreach (var p in pinfo)
			{
				Colours.Add(p.Name,(Color)p.GetGetMethod().Invoke(null,null));
			}
		}

		public static string GetName(Color color)
		{
			if (!Colours.ContainsValue(color))
				throw new ArgumentOutOfRangeException("color", "Not a named WPF color");

			return Colours.First((c) => c.Value == color).Key;
		}
	}
}