为什么Color.IsNamedColor不是当我创建使用Color.FromArgb(彩色)的工作? [英] Why does Color.IsNamedColor not work when I create a color using Color.FromArgb()?

查看:143
本文介绍了为什么Color.IsNamedColor不是当我创建使用Color.FromArgb(彩色)的工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用我允许用户建立一个颜色,然后给他颜色的名称或值以后。如果用户选择红(全红,不红杂交),我想告诉他红。如果他挑选一些奇怪的颜色,然后十六进制值会就好了。下面是示例code演示该问题:

In my app I allow the user to build a color, and then show him the name or value of the color later on. If the user picks red (full red, not red-ish), I want to show him "red". If he picks some strange color, then the hex value would be just fine. Here's sample code that demonstrates the problem:

static string GetName(int r, int g, int b)
{
    Color c = Color.FromArgb(r, g, b);  // Note that specifying a = 255 doesn't make a difference
    if (c.IsNamedColor)
    {
        return c.Name;
    }
    else
    {
        // return hex value
    }
}

即使有很明显的颜色,如红色 IsNamedColor 永远返回true。纵观ARGB值,我的颜色和 Col​​or.Red ,我看不出有什么区别。但是,调用 Col​​or.Red.GetHash code()返回不同的散列code比 Col​​or.FromArgb(255,0, 0).GetHash code()

Even with very obvious colors like red IsNamedColor never returns true. Looking at the ARGB values for my color and Color.Red, I see no difference. However, calling Color.Red.GetHashCode() returns a different hash code than Color.FromArgb(255, 0, 0).GetHashCode().

如何创建一个颜色使用用户指定的RGB值,并有名称属性出来吧?

How can I create a color using user specified RGB values and have the Name property come out right?

推荐答案

从MSDN。

属性值类型:System.Boolean
  如果这个颜色被创造真正的   无论使用FromName方法或   该FromKnownColor方法;除此以外,   假的。

Property Value Type: System.Boolean
true if this Color was created by using either the FromName method or the FromKnownColor method; otherwise, false.

您可以建立由所有 KnownColor 取值RGB元组映射到名字,我想。

You could build a map from all KnownColors rgb tuples to names I suppose.

这篇关于为什么Color.IsNamedColor不是当我创建使用Color.FromArgb(彩色)的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆