C# getting all colors from Color

Viewed 43998

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.

6 Answers

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

Related