C # search for similar colors

I want to call a method with an argument color. But there are many colors that differ only in shade. How can I find colors that differ only slightly from my color, such as AntiqueWhite and Bisque. Here is the color palette.

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); 
+6
source share
6 answers

I found this procedure 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; } } 
+6
source

Could you use this method:

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

This method takes two colors and tolerances and returns whether the two colors are close or not based on their RGB values. I think this should do the trick, but you may need to expand the brightness and saturation.

+11
source

You can get the closest color from the KnownColors enumeration.

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

And support method

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

Analyze this example Find the Nearest Color with C# . Hope gives you an idea.

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; } } 
+2
source

I used Keven Holditch below. But I changed it for my purposes. This version uses exclusive or so that only one value can be disabled tolerance and still returns true. (Tolerance is also <= instead of just <.)

 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; } 
+1
source

I think in order to find similar colors, you should look at the HSL or HSB color space instead of RGB, because with this it is much easier to find similar colors.

Inside .Net, you can call GetHue() , GetSaturation() and GetBrightness() to get these values ​​from a given color and compare these values ​​to find similar ones.

If you need a way back from the HSB value to the color, you can also use this method .

0
source

Source: https://habr.com/ru/post/899767/


All Articles