Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Web Color List in C# application

Tags:

c#

colors

Hallo everbody,

if for example I set the BackColor of a Panel in Winform using Visual Studio, I can pick up the color from 3 lists:

Custom, Web, System

Is it possible to retreive only the Web colors in my C# code application? They are part of KnownColor but so far I could only find how to eliminate System Control from my list.

I would like to use the web colors because they are sorted in a nice way and I would like to insert them into a self-implemented combobox.

Thank you

like image 249
Klaus78 Avatar asked Feb 23 '11 13:02

Klaus78


People also ask

What does RGB 255 255 255 mean?

RGB Colors. All colors on a computer are made up by combining the light from three colors (red, blue, and green). Black is [0,0,0], and White is [255, 255, 255]; Gray is any [x,x,x] where all the numbers are the same.

What are the 16 basic colors?

Web Standard Color Names The World Wide Web Consortium (W3C) has listed 16 valid color names for HTML and CSS: aqua, black, blue, fuchsia, gray, green, lime, maroon, navy, olive, purple, red, silver, teal, white, and yellow.


2 Answers

var webColors = 
  Enum.GetValues(typeof(KnownColor))
    .Cast<KnownColor>()
    .Where (k => k >= KnownColor.Transparent && k < KnownColor.ButtonFace) //Exclude system colors
    .Select(k => Color.FromKnownColor(k));

EDIT:

To order the colors append:

.OrderBy(c => c.GetHue())
.ThenBy(c => c.GetSaturation())
.ThenBy(c => c.GetBrightness());
like image 169
Magnus Avatar answered Sep 19 '22 01:09

Magnus


Color struct contains all the web colors as constants (system colors are defined as constants in SystemColors class)

To get a list of these colors just do:

var webColors = GetConstants(typeof(Color));
var sysColors = GetConstants(typeof(SystemColors));

having GetConstants defined as follow:

static List<Color> GetConstants(Type enumType)
{
    MethodAttributes attributes = MethodAttributes.Static | MethodAttributes.Public;
    PropertyInfo[] properties = enumType.GetProperties();
    List<Color> list = new List<Color>();
    for (int i = 0; i < properties.Length; i++)
    {
        PropertyInfo info = properties[i];
        if (info.PropertyType == typeof(Color))
        {
            MethodInfo getMethod = info.GetGetMethod();
            if ((getMethod != null) && ((getMethod.Attributes & attributes) == attributes))
            {
                object[] index = null;
                list.Add((Color)info.GetValue(null, index));
            }
        }
    }
    return list;
}

EDIT:

To get colors sorted exactly like in VS do:

var webColors = GetConstants(typeof(Color));
var sysColors = GetConstants(typeof(SystemColors));

webColors.Sort(new StandardColorComparer());
sysColors.Sort(new SystemColorComparer());

with StandardColorComparer and SystemColorComparer defined as follows:

class StandardColorComparer : IComparer<Color>
{
    // Methods
    public int Compare(Color color, Color color2)
    {
        if (color.A < color2.A)
        {
            return -1;
        }
        if (color.A > color2.A)
        {
            return 1;
        }
        if (color.GetHue() < color2.GetHue())
        {
            return -1;
        }
        if (color.GetHue() > color2.GetHue())
        {
            return 1;
        }
        if (color.GetSaturation() < color2.GetSaturation())
        {
            return -1;
        }
        if (color.GetSaturation() > color2.GetSaturation())
        {
            return 1;
        }
        if (color.GetBrightness() < color2.GetBrightness())
        {
            return -1;
        }
        if (color.GetBrightness() > color2.GetBrightness())
        {
            return 1;
        }
        return 0;
    }
}

class SystemColorComparer : IComparer<Color>
{
    // Methods
    public int Compare(Color color, Color color2)
    {
        return string.Compare(color.Name, color2.Name, false, CultureInfo.InvariantCulture);
    }
}

N.B. :

This code has been taken from System.Drawing.Design.ColorEditor through reflector.

like image 34
digEmAll Avatar answered Sep 19 '22 01:09

digEmAll