c# finding similar colors

后端 未结 6 849
滥情空心
滥情空心 2021-01-05 16:39

I want to call a method with argument color. But there are a lot of colors which differ only by a shade. How can I find the colors which differ from my color only by little,

6条回答
  •  醉酒成梦
    2021-01-05 17:17

    Could you use a method like this:

     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 colours and a tolerance and returns whether those two colours are close or not based on their RGB values. I think that should do the trick but you may need to extend to include brightness and saturation.

提交回复
热议问题