Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a collection of all the colors in System.Drawing.Color?

Tags:

.net

graphics

How can I extract the list of colors in the System.Drawing.Color struct into a collection or array?

Is there a more efficient way of getting a collection of colors than using this struct as a base?

like image 217
Oded Avatar asked Nov 10 '08 21:11

Oded


2 Answers

So you'd do:

string[] colors = Enum.GetNames(typeof(System.Drawing.KnownColor));

... to get an array of all the collors.

Or... You could use reflection to just get the colors. KnownColors includes items like "Menu", the color of the system menus, etc. this might not be what you desired. So, to get just the names of the colors in System.Drawing.Color, you could use reflection:

Type colorType = typeof(System.Drawing.Color);

PropertyInfo[] propInfoList = colorType.GetProperties(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public);

foreach (System.Reflection.PropertyInfo c in propInfoList) {
  Console.WriteLine(c.Name);
}

This writes out all the colors, but you could easily tailor it to add the color names to a list.

Check out this Code Project project on building a color chart.

like image 132
jons911 Avatar answered Oct 03 '22 09:10

jons911


Try this:

foreach (KnownColor knownColor in Enum.GetValues(typeof(KnownColor)))
{
   Trace.WriteLine(string.Format("{0}", knownColor));
}
like image 43
Roger Lipscombe Avatar answered Oct 03 '22 08:10

Roger Lipscombe