我所有的方法都以各种方式让我失望。不同的照明也会把事情搞砸。
有没有人试图返回一个给定 rgb 值的名字?“红色”“绿色”“蓝色”足以满足我今天的需求。
我对来自网络摄像头的图像进行了不安全的字节处理。
我所有的方法都以各种方式让我失望。不同的照明也会把事情搞砸。
有没有人试图返回一个给定 rgb 值的名字?“红色”“绿色”“蓝色”足以满足我今天的需求。
我对来自网络摄像头的图像进行了不安全的字节处理。
如果您有一个带有名称的已知颜色列表,您可以使用(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
无论哪种已知颜色与您要命名的目标颜色的差异最小,请使用该名称。
你可以试试这段代码
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;
}
如果您希望蓝调更加与众不同等,您可以轻松调整它。
好吧,红色/绿色/蓝色很容易通过检查来识别;您需要支持什么范围的值?
问题是除非你从一个命名的颜色开始,否则很难回到一个。IsNamedColor
即使您通过 FromArgb 创建了明显的颜色,也会返回 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);
您也许还可以使用这种方法作为更复杂算法的已知颜色来源。