【问题标题】:c# finding similar colorsc#寻找相似的颜色
【发布时间】:2011-12-12 09:01:06
【问题描述】:

我想调用一个带有参数颜色的方法。但是有很多颜色只有一个阴影不同。我怎样才能找到与我的颜色略有不同的颜色,例如 AntiqueWhite 和 Bisque。 Here's 调色板。

Bitmap LogoImg = new Bitmap("file1.jpeg");//the extension can be some other
System.Drawing.Color x = LogoImg.GetPixel(LogoImg.Width-1, LogoImg.Height-1);
LogoImg.MakeTransparent(x);
image1.Source = GetBitmapSource(LogoImg);

【问题讨论】:

  • 使用 R、G 或 B(或全部三个)的最大变化阈值
  • 你想对你的结果做什么?
  • @Erno 我看到了,但对我没有帮助。
  • @alexn 获取参数颜色的方法,使该颜色透明。

标签: c# colors


【解决方案1】:

你能用这样的方法吗:

 public bool AreColorsSimilar(Color c1, Color c2, int tolerance)
 {
     return Math.Abs(c1.R - c2.R) < tolerance &&
            Math.Abs(c1.G - c2.G) < tolerance &&
            Math.Abs(c1.B - c2.B) < tolerance;
 }

此方法采用两种颜色和一个容差,并根据它们的 RGB 值返回这两种颜色是否接近。我认为这应该可以解决问题,但您可能需要扩展以包括亮度和饱和度。

【讨论】:

  • 要考虑的其他事情是做同样的事情,但在 HSV 颜色空间中。
【解决方案2】:

我发现了这个套路here

Color nearest_color = Color.Empty;
foreach (object o in WebColors)
{
    // compute the Euclidean distance between the two colors
    // note, that the alpha-component is not used in this example
    dbl_test_red = Math.Pow(Convert.ToDouble(((Color)o).R) - dbl_input_red, 2.0);
    dbl_test_green = Math.Pow(Convert.ToDouble
        (((Color)o).G) - dbl_input_green, 2.0);
    dbl_test_blue = Math.Pow(Convert.ToDouble
        (((Color)o).B) - dbl_input_blue, 2.0);

    temp = Math.Sqrt(dbl_test_blue + dbl_test_green + dbl_test_red);
    // explore the result and store the nearest color
    if(temp == 0.0)
    {
        nearest_color = (Color)o;
        break;
    }
    else if (temp < distance)
    {
        distance = temp;
        nearest_color = (Color)o;
    }
}

【讨论】:

  • 这是一个调色板。看看here 将其转换为不同的东西
  • 不过,没有真正的理由取平方根。最接近的值将是最接近的值,即使不添加占用大量 CPU 的操作。
【解决方案3】:

您可以从 KnownColors 枚举中获取最接近的颜色。

// A color very close to Rosy Brown
var color = Color.FromArgb(188, 143, 142);

var colors = Enum.GetValues(typeof (KnownColor))
                .Cast<KnownColor>()
                .Select(Color.FromKnownColor);

var closest = colors.Aggregate(Color.Black, 
                (accu, curr) =>
                ColorDiff(color, curr) < ColorDiff(color, accu) ? curr : accu);

以及支持方式

private int ColorDiff(Color color, Color curr)
{
    return Math.Abs(color.R - curr.R) + Math.Abs(color.G - curr.G) + Math.Abs(color.B - curr.B);
}

【讨论】:

    【解决方案4】:

    分析这个例子Find the Nearest Color with C#。希望能给你一个想法。

    Color nearest_color = Color.Empty;
    foreach (object o in WebColors)
    {
        // compute the Euclidean distance between the two colors
        // note, that the alpha-component is not used in this example
        dbl_test_red = Math.Pow(Convert.ToDouble(((Color)o).R) - dbl_input_red, 2.0);
        dbl_test_green = Math.Pow(Convert.ToDouble
            (((Color)o).G) - dbl_input_green, 2.0);
        dbl_test_blue = Math.Pow(Convert.ToDouble
            (((Color)o).B) - dbl_input_blue, 2.0);
        // it is not necessary to compute the square root
        // it should be sufficient to use:
        // temp = dbl_test_blue + dbl_test_green + dbl_test_red;
        // if you plan to do so, the distance should be initialized by 250000.0
        temp = Math.Sqrt(dbl_test_blue + dbl_test_green + dbl_test_red);
        // explore the result and store the nearest color
        if(temp == 0.0)
        {
            // the lowest possible distance is - of course - zero
            // so I can break the loop (thanks to Willie Deutschmann)
            // here I could return the input_color itself
            // but in this example I am using a list with named colors
            // and I want to return the Name-property too
            nearest_color = (Color)o;
            break;
        }
        else if (temp < distance)
        {
            distance = temp;
            nearest_color = (Color)o;
        }
    }
    

    【讨论】:

    • 您刚刚发布了来自同一个站点的相同例程 :)
    • 正如您在时间戳工具提示中看到的,两个答案都是在关闭时间创建的。
    • @user966638 WebColors = GetWebColors(); 你可以在解决方案中找到。
    【解决方案5】:

    我在下面使用了 Keven Holditch 的答案。但是我出于自己的目的对其进行了修改。此版本使用异或,因此只有一个值可以通过容差关闭,并且仍然返回 true。 (公差也是

    private bool AreColorsSimilar(Color c1, Color c2, int tolerance)
    {
        return Math.Abs(c1.R - c2.R) <= tolerance ^
               Math.Abs(c1.G - c2.G) <= tolerance ^
               Math.Abs(c1.B - c2.B) <= tolerance;
    }
    

    【讨论】:

      【解决方案6】:

      我认为要找到相似的颜色,您应该查看HSL or HSB color space 而不是 RGB 的颜色,因为这样可以更容易找到相似的颜色。

      在 .Net 中,您可以调用 GetHue()GetSaturation()GetBrightness() 方法从给定颜色中获取这些值并比较这些值以找到相似的值。

      如果您需要从 HSB 值返回到颜色的方法,您也可以使用 this method

      【讨论】:

        猜你喜欢
        • 2013-02-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-09
        • 1970-01-01
        • 1970-01-01
        • 2016-12-08
        • 2011-05-09
        相关资源
        最近更新 更多