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?
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.
Try this:
foreach (KnownColor knownColor in Enum.GetValues(typeof(KnownColor)))
{
Trace.WriteLine(string.Format("{0}", knownColor));
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With