【问题标题】:Web Color List in C# applicationC# 应用程序中的 Web 颜色列表
【发布时间】:2011-07-02 18:20:28
【问题描述】:

大家好,

例如,如果我使用 Visual Studio 在 Winform 中设置面板的背景颜色,我可以从 3 个列表中选择颜色:

自定义、网络、系统

是否可以在我的 C# 代码应用程序中仅检索 Web 颜色?它们是 KnownColor 的一部分,但到目前为止我只能找到如何从我的列表中消除系统控制。

我想使用网络颜色,因为它们的排序方式很好,我想将它们插入到一个自行实现的组合框中。

谢谢

【问题讨论】:

  • 你想要一个 List 的 KnownColor 枚举?
  • 基本上是的,但我希望在 Visual Studio 的 Web 选项卡中对颜色进行排序。我尝试用一​​些算法对颜色进行排序,但结果不太好。

标签: c# colors


【解决方案1】:
var webColors = 
  Enum.GetValues(typeof(KnownColor))
    .Cast<KnownColor>()
    .Where (k => k >= KnownColor.Transparent && k < KnownColor.ButtonFace) //Exclude system colors
    .Select(k => Color.FromKnownColor(k));

编辑:

要订购颜色追加:

.OrderBy(c => c.GetHue())
.ThenBy(c => c.GetSaturation())
.ThenBy(c => c.GetBrightness());

【讨论】:

  • 比其他答案简洁得多。
【解决方案2】:

Color struct 包含所有 Web 颜色作为常量(系统颜色在 SystemColors 类中定义为常量)

要获取这些颜色的列表,只需执行以下操作:

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

GetConstants 定义如下:

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

编辑:

要像在 VS 中一样对颜色进行排序:

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

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

StandardColorComparerSystemColorComparer 定义如下:

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

注意:

此代码已通过反射器从System.Drawing.Design.ColorEditor 获取。

【讨论】:

  • 节点:GetConstants(基本上)与用于BackgroundColor等的颜色选择器中使用的相同。属性
  • 感谢您的回答。事实上,两种解决方案都返回根据字符串名称排序的颜色。我希望颜色在 Visual Studio 选项卡中排序,其中红色的颜色聚集在 ecc....
  • 我试过这个解决方案,现在可以了!非常感谢您的帮助
  • @digEmAll @Klaus 嘿,我的解决方案出了什么问题? ;-) 结果相同,但代码只有 1/10
  • GetConstants 返回类型必须是 List 或 IEnumerable
【解决方案3】:

如果您需要按原色排序,您可以以此为起点:

var colors = Enum.GetValues(typeof(KnownColor))
                 .Cast<KnownColor>()
                 .Select(kc => Color.FromKnownColor(kc))
                 .OrderBy(c => c.GetHue())

【讨论】:

    猜你喜欢
    • 2017-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-28
    • 1970-01-01
    • 2011-09-15
    • 2012-09-06
    • 1970-01-01
    相关资源
    最近更新 更多