c#找到相似的颜色

pet*_*ski 6 c# colors

我想用参数颜色调用一个方法.但是有很多颜色只有阴影才有区别.我怎样才能找到与我的颜色不同的颜色,例如AntiqueWhite和Bisque.这是调色板.

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);
Run Code Online (Sandbox Code Playgroud)

Kev*_*tch 11

你能用这样的方法:

 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;
 }
Run Code Online (Sandbox Code Playgroud)

此方法采用两种颜色和公差,并根据RGB值返回这两种颜色是否接近.我认为应该这样做,但你可能需要扩展到包括亮度和饱和度.

  • 要考虑的其他事情,是做同样的事情,但在HSV色彩空间. (3认同)

Mar*_*rco 6

我在这里发现了这个例程:

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;
    }
}
Run Code Online (Sandbox Code Playgroud)