23

I want to make a ComboBox filled with all the colors from System.Drawing.Color

But I can't seem to collect all the colors from that collection

I've already tried using a foreach to do the job like this:

foreach (Color clr in Color)
     {

     }

But all I get is an error.

So how can I loop trough all the colors?

Any help will be appreciated.

Pieter888
  • 4,882
  • 13
  • 53
  • 74

8 Answers8

51

You could take color from KnownColor

KnownColor[] colors  = Enum.GetValues(typeof(KnownColor));
foreach(KnownColor knowColor in colors)
{
  Color color = Color.FromKnownColor(knowColor);
}

or use reflection to avoid color like Menu, Desktop... contain in KnowColor

Type colorType = typeof(System.Drawing.Color);
// We take only static property to avoid properties like Name, IsSystemColor ...
PropertyInfo[] propInfos = colorType.GetProperties(BindingFlags.Static | BindingFlags.DeclaredOnly | BindingFlags.Public);
foreach (PropertyInfo propInfo in propInfos) 
{
  Console.WriteLine(propInfo.Name);
}
Julien Hoarau
  • 48,964
  • 20
  • 128
  • 117
  • Thank you! this was exactly what I needed. I would have settled for simply all colors and then find a way to filter them to avoid the menu and desktop colors myself. Thank you very much! – Pieter888 Sep 29 '10 at 11:46
13

Similar to @madgnome’s code, but I prefer the following since it doesn’t require parsing the string names (a redundant indirection, in my opinion):

foreach (var colorValue in Enum.GetValues(typeof(KnownColor)))
    Color color = Color.FromKnownColor((KnownColor)colorValue);
Konrad Rudolph
  • 530,221
  • 131
  • 937
  • 1,214
8

My way to get colors. I think it is the best way via Reflection library.

private List<Color> GetAllColors()
{
    List<Color> allColors = new List<Color>();

    foreach (PropertyInfo property in typeof(Color).GetProperties())
    {
        if (property.PropertyType == typeof(Color))
        {
            allColors.Add((Color)property.GetValue(null));
        }
    }

    return allColors;
}
ZygD
  • 22,092
  • 39
  • 79
  • 102
Artxzta
  • 107
  • 1
  • 4
4

This is what I think you want:

foreach (Color color in new ColorConverter().GetStandardValues())
{
    MessageBox.Show(color.ToString());
}

it will loop through all the standard values for color, and should work for what you need

Richard J. Ross III
  • 55,009
  • 24
  • 135
  • 201
2

Based on @Julien Hoarau answer what i use to lookup a color is dump them in LinqPad like this:

void Main()
{
    ((KnownColor[])Enum.GetValues(typeof(KnownColor)))
        .Select(knowColor => System.Drawing.Color.FromKnownColor(knowColor))
        .Where(color => color.R == color.R)
        .ToList()
        .ForEach(color =>   
    {

        var _label = new Label();
        _label.Background = new SolidColorBrush(System.Windows.Media.Color.FromArgb(color.A, color.R, color.G, color.B));
        _label.Foreground = new SolidColorBrush(System.Windows.Media.Color.FromArgb(color.A, (byte)~color.R, (byte)~color.G, (byte)~color.B));
        _label.Content = $"{color.Name} R:{color.R} G:{color.G} B:{color.B}";
        _label.Dump();
    });
}

Which produces nice list like this:

enter image description here

mdziadowiec
  • 396
  • 3
  • 10
0

The requirement was to have a list of the system colors to chose from, a list of the "web" colors, AKA the professional colors, and then RGB via R,G,B syntax, and finally the use of the color picker control for completeness.

I save the list of colors and system color properties for use later. The ReduceName(color) removes the "Color [Name]" components from the string. If you don't maintain a running list of the colors, you will have them show up twice in the second list. There is probably a more elegant approach to handling that, but time was more important than perfect, as is often the case.

_ListAllColors = new List<Color>();
_SystemColorProperties = typeof(SystemColors).GetProperties();
foreach (PropertyInfo propertyInfo in _SystemColorProperties)
{
    object colorObject = propertyInfo.GetValue(null, null);
    Color color = (Color)colorObject;
    if (!_ListAllColors.Contains(color))
    {
        systemColorsComboBox.Items.Add(ReduceName(color));
        _ListAllColors.Add(color);
    }
}

foreach (KnownColor colorValue in Enum.GetValues(typeof(KnownColor)))
{
    Color color = Color.FromKnownColor(colorValue);

    if (!_ListAllColors.Contains(color))
    {
        professionalColorsComboBox.Items.Add(ReduceName(color));
        _ListAllColors.Add(color);
    }
}

System Colors

Professional Colors

Nathan Tuggy
  • 2,237
  • 27
  • 30
  • 38
0
 var knownColors = Enum.GetValues(typeof(KnownColor))
               .Cast<KnownColor>()
               .Where(color => color != KnownColor.ActiveBorder
                              && color != KnownColor.ActiveCaptionText
                              && color != KnownColor.ActiveCaption
                              && color != KnownColor.AppWorkspace
                              && color != KnownColor.ButtonFace
                              && color != KnownColor.ButtonHighlight
                              && color != KnownColor.ButtonShadow
                              && color != KnownColor.Control
                              && color != KnownColor.ControlDark
                              && color != KnownColor.ControlDarkDark
                              && color != KnownColor.ControlLight
                              && color != KnownColor.ControlLightLight
                              && color != KnownColor.ControlText
                              && color != KnownColor.Desktop
                              && color != KnownColor.GradientActiveCaption
                              && color != KnownColor.GradientInactiveCaption
                              && color != KnownColor.GrayText
                              && color != KnownColor.Highlight
                              && color != KnownColor.HighlightText
                              && color != KnownColor.HotTrack
                              && color != KnownColor.InactiveBorder
                              && color != KnownColor.InactiveCaption
                              && color != KnownColor.InactiveCaptionText
                              && color != KnownColor.Info
                              && color != KnownColor.InfoText
                              && color != KnownColor.Menu
                              && color != KnownColor.MenuBar
                              && color != KnownColor.MenuHighlight
                              && color != KnownColor.MenuText
                              && color != KnownColor.ScrollBar
                              && color != KnownColor.Window
                              && color != KnownColor.WindowFrame
                              && color != KnownColor.WindowText)
               .ToArray();
David Morrow
  • 262
  • 4
  • 9
0

To separate System Colors from KnownColors use this

List<KnownColor> KnowColors = new List<KnownColor>();

foreach (string color in Enum.GetNames(typeof(KnownColor))) { if (!Color.FromKnownColor((KnownColor)Enum.Parse(typeof(KnownColor), color)).IsSystemColor) { KnowColors.Add((KnownColor) Enum.Parse(typeof(KnownColor), color)); } }

For list of System Colors

List<KnownColor> KnowColors = new List<KnownColor>();

foreach (string color in Enum.GetNames(typeof(KnownColor))) { if (Color.FromKnownColor((KnownColor)Enum.Parse(typeof(KnownColor), color)).IsSystemColor) { KnowColors.Add((KnownColor) Enum.Parse(typeof(KnownColor), color)); } }

David Morrow
  • 262
  • 4
  • 9