问题描述
在我的应用我允许用户建立一个颜色,然后给他颜色的名称或值以后。如果用户选择红(全红,不红杂交),我想告诉他红。如果他挑选一些奇怪的颜色,然后十六进制值会就好了。下面是示例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值,我的颜色和 Color.Red
,我看不出有什么区别。但是,调用 Color.Red.GetHash code()
返回不同的散列code比 Color.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?
推荐答案
的
属性值类型:System.Boolean
如果这个颜色被创造真正的 无论使用FromName方法或 该FromKnownColor方法;除此以外, 假的。
您可以建立由所有 KnownColor
取值RGB元组映射到名字,我想。
You could build a map from all KnownColor
s rgb tuples to names I suppose.
这篇关于为什么Color.IsNamedColor不是当我创建使用Color.FromArgb(彩色)的工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!