2011-10-21 69 views
6

我想調用帶參數顏色的方法。但是有很多顏色只是因爲陰影而有所不同。我怎樣才能找到與我的顏色不同的顏色,例如AntiqueWhite和Bisque。 Here's調色板。c#尋找類似的顏色

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

使用R,G的最大變化或B的treshold(或三個) – RvdK

+0

你要什麼對你的成績怎麼辦? – alexn

+0

[尋找類似顏色的程序]的可能重複(http://stackoverflow.com/questions/1725505/finding-similar-colors-programatically) –

回答

6

我發現這個程序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; 
    } 
} 
+0

什麼是WebColors? –

+0

這是一個調色板。看看[這裏](http://pietschsoft.com/post/2006/06/29/C-NET-Convert-SystemDrawingColor-to-HTML-color.aspx)將其轉換爲不同的東西 – Marco

+0

沒有真正的理由儘管取平方根。即使不增加CPU繁忙的操作,最接近的值也將是最接近的值。 – Nyerguds

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

這個方法有兩個顏色,一個寬容和返回這兩種顏色是否接近或不是基於他們的RGB值。我認爲這應該做的伎倆,但你可能需要擴大到包括亮度和飽和度。

+3

其他需要考慮的事情是做同樣的事情,但在HSV色彩空間。 –

2

分析這個例子Find the Nearest Color with C#。希望給你一個想法。

enter image description 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); 
    // 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; 
    } 
} 
+0

您剛從同一個網站發佈了相同的例程:) – Marco

+0

什麼是WebColors? –

+0

正如您在時間戳工具提示中看到的,這兩個答案都是在關閉時間創建的。 –

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

偉大的解決方案!在編程通用Windows應用程序時,是否可以將其更新爲使用Windows.UI.Color和Windows.UI.Colors?謝謝。 – AnAurelian

1

我用下面凱文Holditch的答案。但是我爲了自己的目的對其進行了修改。這個版本使用了exclusive或者只有一個值可以被公差關閉,並且仍然返回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; 
}