8

我所有的方法都以各种方式让我失望。不同的照明也会把事情搞砸。

有没有人试图返回一个给定 rgb 值的名字?“红色”“绿色”“蓝色”足以满足我今天的需求。

我对来自网络摄像头的图像进行了不安全的字节处理。

摄像头

颜色

4

5 回答 5

5

如果您有一个带有名称的已知颜色列表,您可以使用(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

无论哪种已知颜色与您要命名的目标颜色的差异最小,请使用该名称。

于 2008-12-13T23:46:28.063 回答
3

你可以试试这段代码

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);
        }
于 2012-10-10T05:06:48.403 回答
2

我个人觉得从色相/饱和度/亮度的角度来考虑颜色比 RGB 值更自然,我认为在这种情况下这对你很有效。尝试这个:

将颜色名称分配给光谱的某些范围,如您所见。例如,红色可能是 0-39,橙色是 40-79,等等(这些是任意数字——我不知道它们是否适合任何规模)。然后根据您的 RGB 值计算色调(您可以在此处找到一个公式,尽管可能还有其他公式)。一旦你知道了色调,你就知道它在光谱的哪个范围内,你可以给它一个名字。

于 2008-12-14T04:19:13.643 回答
1

这是一个使用两个限定符和一个颜色名称的简单名称方案:

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

如果您希望蓝调更加与众不同等,您可以轻松调整它。

于 2016-08-25T10:25:10.153 回答
0

好吧,红色/绿色/蓝色很容易通过检查来识别;您需要支持什么范围的值?

问题是除非你从一个命名的颜色开始,否则很难回到一个。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);

您也许还可以使用这种方法作为更复杂算法的已知颜色来源。

于 2008-12-13T23:38:18.220 回答