C# 寻找相似颜色
我想调用带有参数颜色的方法。但有很多颜色仅存在色度差异。我怎样才能找到与我的颜色只有一点点不同的颜色,例如 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);
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, for example AntiqueWhite and Bisque. Here's 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);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
您可以使用这样的方法吗:
该方法采用两种颜色和一个容差,并根据它们的 RGB 值返回这两种颜色是否接近。我认为这应该可以解决问题,但您可能需要扩展以包括亮度和饱和度。
Could you use a method like this:
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.
我在此处找到了这个例程:
I found this routine here:
您可以从 KnownColors 枚举中获取最接近的颜色。
以及支持方法
You can get the closest color from the KnownColors enum.
And the support method
分析此示例
使用 C# 查找最接近的颜色
。希望给你一个想法。Analyze this example
Find the Nearest Color with C#
. Hope gives you an idea.我认为要找到相似的颜色,您应该查看 HSL 或 HSB 颜色空间,而不是RGB 颜色,因为这样可以更容易地找到相似的颜色。
在 .Net 中,您可以调用
GetHue( )
,GetSaturation()
和GetBrightness()
方法从给定颜色获取这些值并比较这些值以查找相似的值。如果您需要从 HSB 值返回到颜色,您还可以使用 此方法。
I think to find similar colors you should take a look at the HSL or HSB color space instead of the RGB one, cause with this it is a lot easier to find similar colors.
Within .Net you can call the
GetHue()
,GetSaturation()
andGetBrightness()
method to get these values from a given color and compare these values to find similar ones.If you need the way back from an HSB value to a color you can also use this method.
我在下面使用了凯文·霍尔迪奇的答案。但我为了自己的目的修改了它。此版本使用异或,因此只有一个值可以超出容差,但仍返回 true。 (容差也是 <= 而不仅仅是 <。)
I used Keven Holditch's answer below. But I modified it for my own purposes. This version uses exclusive-or so that only one value can be off by the tolerance and still return true. (Tolerance is also <= instead of just <.)