简体   繁体   English

C# 从 Color 获取所有 colors

[英]C# getting all colors from Color

I want to make a ComboBox filled with all the colors from System.Drawing.Color我想制作一个ComboBox填充来自System.Drawing.Color的所有 colors

But I can't seem to collect all the colors from that collection但我似乎无法从该系列中收集所有 colors

I've already tried using a foreach to do the job like this:我已经尝试使用foreach来完成这样的工作:

foreach (Color clr in Color)
     {

     }

But all I get is an error.但我得到的只是一个错误。

So how can I loop trough all the colors?那么我怎样才能遍历所有 colors 呢?

Any help will be appreciated.任何帮助将不胜感激。

You could take color from KnownColor你可以从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或使用反射来避免像Menu , Desktop...包含在 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);
}

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):类似于@madgnome 的代码,但我更喜欢以下代码,因为它不需要解析字符串名称(在我看来,这是一个多余的间接引用):

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

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;
}

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它将遍历颜色的所有标准值,并且应该可以满足您的需要

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.要求是有一个可供选择的系统颜色列表,一个“网络”颜色列表,也就是专业颜色,然后是通过 R、G、B 语法的 RGB,最后使用颜色选择器控件完整性。

I save the list of colors and system color properties for use later.我保存了颜色列表和系统颜色属性以备后用。 The ReduceName(color) removes the "Color [Name]" components from the string. ReduceName(color) 从字符串中删除“Color [Name]”组件。 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);
    }
}

系统颜色

专业色彩

Based on @Julien Hoarau answer what i use to lookup a color is dump them in LinqPad like this:根据@Julien Hoarau 的回答,我用来查找颜色的方法是将它们转储到 LinqPad 中,如下所示:

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:这会产生这样的好列表:

在此处输入图片说明

 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();

To separate System Colors from KnownColors use this要将系统 Colors 与 KnownColors 分开,请使用此

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)); foreach (Enum.GetNames(typeof(KnownColor)) 中的字符串颜色) { if (.Color.FromKnownColor((KnownColor)Enum,Parse(typeof(KnownColor).color)).IsSystemColor) { KnowColors.Add((KnownColor) Enum ,解析(类型(已知颜色);颜色)); } } } }

For list of System Colors对于系统列表 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)); foreach (Enum.GetNames(typeof(KnownColor)) 中的字符串颜色) { if (Color.FromKnownColor((KnownColor)Enum.Parse(typeof(KnownColor), color)).IsSystemColor) { KnowColors.Add((KnownColor) Enum.解析(类型(已知颜色),颜色)); } } } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM