尝试将rgb从.net颜色转换为字符串,如“红色”或“蓝色”

时间:2022-04-30 04:55:53

All of my methods are failing me in various ways. different lighting can mess it all up too.

我的所有方法都以各种方式让我失望。不同的灯光也会把它搞得一团糟。

has anyone every trying to return a name given a rgb value? "red" "green" "blue" would be enough to satisfy my needs for today.

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

i have unsafe byte processing of images from my web cam.

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

webcam http://i35.tinypic.com/2yvorau.png

尝试将rgb从.net颜色转换为字符串,如“红色”或“蓝色”

5 个解决方案

#1


5  

If you have a list of known colors with names, you can see which of those known colors a given target color is 'closest' to, using a 'closeness' function along the lines of (F# code):

如果你有一个带名字的已知颜色列表,你可以看到给定目标颜色中哪些已知颜色“最接近”,使用沿着(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

Whichever one of the known colors has the smallest diff to the target color you want to name, use that name.

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

#2


3  

You can try this code

您可以尝试此代码

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

#3


2  

I personally find it more natural to think of colors in terms of hue/saturation/brightness than RGB values, and I think that would work well for you in this case. Try this:

我个人觉得从色调/饱和度/亮度方面考虑颜色比RGB值更自然,我认为在这种情况下这对你很有用。试试这个:

Assign color names to certain ranges of the spectrum, as you see fit. For example, maybe red is 0-39, orange is 40-79, etc. (those are arbitrary numbers - I have no idea if they fit on any kind of scale or not). Then calculate the hue from your RGB value (you can find a formula here, although there may be others). Once you know the hue, you know what range of the spectrum it's in, and you can give it a name.

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

#4


0  

Well, Red/Green/Blue are fairly easy to identify by inspection; what range of values do you need to support?

那么,红色/绿色/蓝色很容易通过检查来识别;您需要支持哪些值?

The problem is that unless you start with a named color, it is very hard to get back to one; IsNamedColor will return false even if you create an obvious color via FromArgb.

问题是,除非你从一个命名颜色开始,否则很难回到一个;即使您通过FromArgb创建明显的颜色,IsNamedColor也将返回false。

If you only need the actual expected standard colors, you could enumerate the known colors via reflection?

如果您只需要实际预期的标准颜色,您可以通过反射枚举已知的颜色?

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

You might also be able to use this approach as a source of known colors for a more sophisticated algorithm.

您也可以将此方法用作更复杂算法的已知颜色的来源。

#5


0  

Here is a simple name scheme using two qualifiers and a color name:

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

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

You can easily adapt it if you want the blues to be more differentiated etc..

如果你想让蓝调更加与众不同,你可以很容易地适应它。

#1


5  

If you have a list of known colors with names, you can see which of those known colors a given target color is 'closest' to, using a 'closeness' function along the lines of (F# code):

如果你有一个带名字的已知颜色列表,你可以看到给定目标颜色中哪些已知颜色“最接近”,使用沿着(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

Whichever one of the known colors has the smallest diff to the target color you want to name, use that name.

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

#2


3  

You can try this code

您可以尝试此代码

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

#3


2  

I personally find it more natural to think of colors in terms of hue/saturation/brightness than RGB values, and I think that would work well for you in this case. Try this:

我个人觉得从色调/饱和度/亮度方面考虑颜色比RGB值更自然,我认为在这种情况下这对你很有用。试试这个:

Assign color names to certain ranges of the spectrum, as you see fit. For example, maybe red is 0-39, orange is 40-79, etc. (those are arbitrary numbers - I have no idea if they fit on any kind of scale or not). Then calculate the hue from your RGB value (you can find a formula here, although there may be others). Once you know the hue, you know what range of the spectrum it's in, and you can give it a name.

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

#4


0  

Well, Red/Green/Blue are fairly easy to identify by inspection; what range of values do you need to support?

那么,红色/绿色/蓝色很容易通过检查来识别;您需要支持哪些值?

The problem is that unless you start with a named color, it is very hard to get back to one; IsNamedColor will return false even if you create an obvious color via FromArgb.

问题是,除非你从一个命名颜色开始,否则很难回到一个;即使您通过FromArgb创建明显的颜色,IsNamedColor也将返回false。

If you only need the actual expected standard colors, you could enumerate the known colors via reflection?

如果您只需要实际预期的标准颜色,您可以通过反射枚举已知的颜色?

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

You might also be able to use this approach as a source of known colors for a more sophisticated algorithm.

您也可以将此方法用作更复杂算法的已知颜色的来源。

#5


0  

Here is a simple name scheme using two qualifiers and a color name:

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

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

You can easily adapt it if you want the blues to be more differentiated etc..

如果你想让蓝调更加与众不同,你可以很容易地适应它。