Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to enumerate through Colors in WinRT?

Enumerating through colors in non-WinRT is a common question with a simple answer. But, since the Colors 'ENUM' is actually just a class with static 'color' properties you can't use the standard approach in WinRT.

How do you enumerate colors in WinRT?

like image 697
Jerry Nixon Avatar asked Dec 06 '22 11:12

Jerry Nixon


1 Answers

Like this:

Enumerating through Colors in WinRT requires using System.Reflection so you can fetch the static properties sub-classed in the container class 'Colors'. Like this:

Dictionary<string, Windows.UI.Color> Colors()
{
    var _Colors = typeof(Windows.UI.Colors)
        // using System.Reflection;
        .GetRuntimeProperties()
        .Select(c => new
        {
            Color = (Windows.UI.Color)c.GetValue(null),
            Name = c.Name
        });
    return _Colors.ToDictionary(x => x.Name, x => x.Color);
}

Source: http://codepaste.net/j3mzrw

Note: if you don't like reflection (for some reason) then there's nothing stopping you from hand coding an enumerable of the colors, too. Creating a 'dynamic' list of colors is really just fancy for fancy sake - colors don't get updated. Just write the list!

Here, I did it for you:

public class ColorList : List<Windows.UI.Color>
{
    public ColorList()
    {
        this.Add(Windows.UI.Colors.AliceBlue);
        this.Add(Windows.UI.Colors.AntiqueWhite);
        this.Add(Windows.UI.Colors.Aqua);
        this.Add(Windows.UI.Colors.Aquamarine);
        this.Add(Windows.UI.Colors.Azure);
        this.Add(Windows.UI.Colors.Beige);
        this.Add(Windows.UI.Colors.Bisque);
        this.Add(Windows.UI.Colors.Black);
        this.Add(Windows.UI.Colors.BlanchedAlmond);
        this.Add(Windows.UI.Colors.Blue);
        this.Add(Windows.UI.Colors.BlueViolet);
        this.Add(Windows.UI.Colors.Brown);
        this.Add(Windows.UI.Colors.BurlyWood);
        this.Add(Windows.UI.Colors.CadetBlue);
        this.Add(Windows.UI.Colors.Chartreuse);
        this.Add(Windows.UI.Colors.Chocolate);
        this.Add(Windows.UI.Colors.Coral);
        this.Add(Windows.UI.Colors.CornflowerBlue);
        this.Add(Windows.UI.Colors.Cornsilk);
        this.Add(Windows.UI.Colors.Crimson);
        this.Add(Windows.UI.Colors.Cyan);
        this.Add(Windows.UI.Colors.DarkBlue);
        this.Add(Windows.UI.Colors.DarkCyan);
        this.Add(Windows.UI.Colors.DarkGoldenrod);
        this.Add(Windows.UI.Colors.DarkGray);
        this.Add(Windows.UI.Colors.DarkGreen);
        this.Add(Windows.UI.Colors.DarkKhaki);
        this.Add(Windows.UI.Colors.DarkMagenta);
        this.Add(Windows.UI.Colors.DarkOliveGreen);
        this.Add(Windows.UI.Colors.DarkOrange);
        this.Add(Windows.UI.Colors.DarkOrchid);
        this.Add(Windows.UI.Colors.DarkRed);
        this.Add(Windows.UI.Colors.DarkSalmon);
        this.Add(Windows.UI.Colors.DarkSeaGreen);
        this.Add(Windows.UI.Colors.DarkSlateBlue);
        this.Add(Windows.UI.Colors.DarkSlateGray);
        this.Add(Windows.UI.Colors.DarkTurquoise);
        this.Add(Windows.UI.Colors.DarkViolet);
        this.Add(Windows.UI.Colors.DeepPink);
        this.Add(Windows.UI.Colors.DeepSkyBlue);
        this.Add(Windows.UI.Colors.DimGray);
        this.Add(Windows.UI.Colors.DodgerBlue);
        this.Add(Windows.UI.Colors.Firebrick);
        this.Add(Windows.UI.Colors.FloralWhite);
        this.Add(Windows.UI.Colors.ForestGreen);
        this.Add(Windows.UI.Colors.Fuchsia);
        this.Add(Windows.UI.Colors.Gainsboro);
        this.Add(Windows.UI.Colors.GhostWhite);
        this.Add(Windows.UI.Colors.Gold);
        this.Add(Windows.UI.Colors.Goldenrod);
        this.Add(Windows.UI.Colors.Gray);
        this.Add(Windows.UI.Colors.Green);
        this.Add(Windows.UI.Colors.GreenYellow);
        this.Add(Windows.UI.Colors.Honeydew);
        this.Add(Windows.UI.Colors.HotPink);
        this.Add(Windows.UI.Colors.IndianRed);
        this.Add(Windows.UI.Colors.Indigo);
        this.Add(Windows.UI.Colors.Ivory);
        this.Add(Windows.UI.Colors.Khaki);
        this.Add(Windows.UI.Colors.Lavender);
        this.Add(Windows.UI.Colors.LavenderBlush);
        this.Add(Windows.UI.Colors.LawnGreen);
        this.Add(Windows.UI.Colors.LemonChiffon);
        this.Add(Windows.UI.Colors.LightBlue);
        this.Add(Windows.UI.Colors.LightCoral);
        this.Add(Windows.UI.Colors.LightCyan);
        this.Add(Windows.UI.Colors.LightGoldenrodYellow);
        this.Add(Windows.UI.Colors.LightGray);
        this.Add(Windows.UI.Colors.LightGreen);
        this.Add(Windows.UI.Colors.LightPink);
        this.Add(Windows.UI.Colors.LightSalmon);
        this.Add(Windows.UI.Colors.LightSeaGreen);
        this.Add(Windows.UI.Colors.LightSkyBlue);
        this.Add(Windows.UI.Colors.LightSlateGray);
        this.Add(Windows.UI.Colors.LightSteelBlue);
        this.Add(Windows.UI.Colors.LightYellow);
        this.Add(Windows.UI.Colors.Lime);
        this.Add(Windows.UI.Colors.LimeGreen);
        this.Add(Windows.UI.Colors.Linen);
        this.Add(Windows.UI.Colors.Magenta);
        this.Add(Windows.UI.Colors.Maroon);
        this.Add(Windows.UI.Colors.MediumAquamarine);
        this.Add(Windows.UI.Colors.MediumBlue);
        this.Add(Windows.UI.Colors.MediumOrchid);
        this.Add(Windows.UI.Colors.MediumPurple);
        this.Add(Windows.UI.Colors.MediumSeaGreen);
        this.Add(Windows.UI.Colors.MediumSlateBlue);
        this.Add(Windows.UI.Colors.MediumSpringGreen);
        this.Add(Windows.UI.Colors.MediumTurquoise);
        this.Add(Windows.UI.Colors.MediumVioletRed);
        this.Add(Windows.UI.Colors.MidnightBlue);
        this.Add(Windows.UI.Colors.MintCream);
        this.Add(Windows.UI.Colors.MistyRose);
        this.Add(Windows.UI.Colors.Moccasin);
        this.Add(Windows.UI.Colors.NavajoWhite);
        this.Add(Windows.UI.Colors.Navy);
        this.Add(Windows.UI.Colors.OldLace);
        this.Add(Windows.UI.Colors.Olive);
        this.Add(Windows.UI.Colors.OliveDrab);
        this.Add(Windows.UI.Colors.Orange);
        this.Add(Windows.UI.Colors.OrangeRed);
        this.Add(Windows.UI.Colors.Orchid);
        this.Add(Windows.UI.Colors.PaleGoldenrod);
        this.Add(Windows.UI.Colors.PaleGreen);
        this.Add(Windows.UI.Colors.PaleTurquoise);
        this.Add(Windows.UI.Colors.PaleVioletRed);
        this.Add(Windows.UI.Colors.PapayaWhip);
        this.Add(Windows.UI.Colors.PeachPuff);
        this.Add(Windows.UI.Colors.Peru);
        this.Add(Windows.UI.Colors.Pink);
        this.Add(Windows.UI.Colors.Plum);
        this.Add(Windows.UI.Colors.PowderBlue);
        this.Add(Windows.UI.Colors.Purple);
        this.Add(Windows.UI.Colors.Red);
        this.Add(Windows.UI.Colors.RosyBrown);
        this.Add(Windows.UI.Colors.RoyalBlue);
        this.Add(Windows.UI.Colors.SaddleBrown);
        this.Add(Windows.UI.Colors.Salmon);
        this.Add(Windows.UI.Colors.SandyBrown);
        this.Add(Windows.UI.Colors.SeaGreen);
        this.Add(Windows.UI.Colors.SeaShell);
        this.Add(Windows.UI.Colors.Sienna);
        this.Add(Windows.UI.Colors.Silver);
        this.Add(Windows.UI.Colors.SkyBlue);
        this.Add(Windows.UI.Colors.SlateBlue);
        this.Add(Windows.UI.Colors.SlateGray);
        this.Add(Windows.UI.Colors.Snow);
        this.Add(Windows.UI.Colors.SpringGreen);
        this.Add(Windows.UI.Colors.SteelBlue);
        this.Add(Windows.UI.Colors.Tan);
        this.Add(Windows.UI.Colors.Teal);
        this.Add(Windows.UI.Colors.Thistle);
        this.Add(Windows.UI.Colors.Tomato);
        this.Add(Windows.UI.Colors.Transparent);
        this.Add(Windows.UI.Colors.Turquoise);
        this.Add(Windows.UI.Colors.Violet);
        this.Add(Windows.UI.Colors.Wheat);
        this.Add(Windows.UI.Colors.White);
        this.Add(Windows.UI.Colors.WhiteSmoke);
        this.Add(Windows.UI.Colors.Yellow);
        this.Add(Windows.UI.Colors.YellowGreen);
    }
}

Either works.

like image 154
Jerry Nixon Avatar answered Dec 10 '22 03:12

Jerry Nixon