试图将rgb从.net颜色转换为诸如“红色”或“蓝色”的字符串
我的所有方法都以各种方式使我失败。不同的照明可以把它弄得一团糟。试图将rgb从.net颜色转换为诸如“红色”或“蓝色”的字符串
有没有人试图返回给定rgb值的名称? “红色”“绿色”“蓝色”足以满足我今天的需求。
我对我的网络摄像头中的图像进行了不安全的字节处理。
webcam http://i35.tinypic.com/2yvorau.png
好,红/绿/蓝是很容易通过检查,以确定;您需要支持哪些范围的值?
问题是,除非你用一个命名的颜色开始,否则很难将返回为1;即使您通过FromArgb创建明显的颜色,IsNamedColor
也会返回false。
如果您只需要实际的预期标准颜色,可以通过反射枚举已知颜色?
Color test = Color.FromArgb(255,0,0);
Color known = (
from prop in typeof(Color)
.GetProperties(BindingFlags.Public | BindingFlags.Static)
where prop.PropertyType == typeof(Color)
let color = (Color)prop.GetValue(null, null)
where color.A == test.A && color.R == test.R
&& color.G == test.G && color.B == test.B
select color)
.FirstOrDefault();
Console.WriteLine(known.Name);
您可能也可以使用此方法作为更复杂算法的已知颜色来源。
全谱 我从我的网络摄像头处理字节图像。 http://i35.tinypic.com/2yvorau.png – 2008-12-13 23:41:36
标准颜色的稍微简单的来源是KnownColors枚举。这样你不必使用GetProperties等(使用Color.FromKnownColor。) – 2008-12-25 19:00:44
如果你已经知道颜色与名称的列表,你可以看到它的那些已知的颜色给定目标颜色是“最接近”来,用“亲近”功能沿(F#代码)行:
let Diff (c1:Color) (c2:Color) =
let dr = (c1.R - c2.R) |> int
let dg = (c1.G - c2.G) |> int
let db = (c1.B - c2.B) |> int
dr*dr + dg*dg + db*db
无论哪个已知颜色的diff与要命名的目标颜色最小,请使用该名称。
哦,那真是太酷了 – xxxxxxx 2008-12-13 23:54:59
根据hue/saturation/brightness,我个人觉得颜色比RGB值更自然,我认为在这种情况下,这对你很有帮助。试试这个:
按照您认为合适的方式将颜色名称指定给光谱的特定范围。例如,红色可能是0-39,橙色是40-79等(这些是任意数字 - 我不知道它们是否适合任何比例或不适用)。然后根据RGB值计算色调(可以找到公式here,尽管可能有其他公式)。一旦你知道了色调,你就知道它所处的光谱范围,你可以给它一个名字。
你可以试试这个代码
static char[] hexDigits = {
'0', '1', '2', '3', '4', '5', '6', '7',
'8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
public static string ColorToHexString(Color color)
{
byte[] bytes = new byte[4];
bytes[0] = color.A;
bytes[1] = color.R;
bytes[2] = color.G;
bytes[3] = color.B;
char[] chars = new char[bytes.Length * 2];
for (int i = 0; i < bytes.Length; i++)
{
int b = bytes[i];
chars[i * 2] = hexDigits[b >> 4];
chars[i * 2 + 1] = hexDigits[b & 0xF];
}
return new string(chars);
}
下面是使用两个预选赛和颜色名称的简单的名称方案:
string ColorName(Color c)
{
List<float> hues = new List<float>()
{ 0, 15, 35, 44, 54, 63, 80, 160, 180, 200, 244, 280, 350, 360};
List<string> hueNames = new List<string>()
{ "red", "orange-red", "orange", "yellow-orange", "yellow",
"yellow-green", "green" , "blue-green" , "cyan", "blue",
"violet", "purple", "red" };
float h = c.GetHue();
float s = c.GetSaturation();
float b = (c.R * 0.299f + c.G * 0.587f + c.B *0.114f)/256f;
string name = s < 0.35f ? "pale " : s > 0.8f ? "vivid " : "";
name += b < 0.35f ? "dark " : b > 0.8f ? "light " : "";
for (int i = 0; i < hues.Count - 1; i++)
if (h >= hues[i] && h <= hues[i+1])
{
name += hueNames[i];
break;
}
return name;
}
,如果你想蓝军更可以轻松适应它区别等。
所以这个问题可以重申为给定的颜色和名称的字典,对于给定的颜色找到最接近的颜色的名称? – ICR 2008-12-14 01:24:36