给定一个RGB值什么是最好的方式来找到数据库中最接近的匹配? [英] Given an RGB value what would be the best way to find the closest match in the database?

查看:186
本文介绍了给定一个RGB值什么是最好的方式来找到数据库中最接近的匹配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个rgb值,如果它不存在于我的数据库中的颜色表中,我需要找到最接近的颜色。我在想比较所有的值,发现差异(红,绿,蓝),然后取平均值。最低平均偏差应为最接近的颜色。在我看来,应该有一个更好的方式。任何想法?

I have a rgb value and if it doesn't exist in the color table in my database I need to find the closest color. I was thinking of comparing all values and finding the difference(in red,green,and blue) then take the average. The lowest average deviation should be the closest color. There seems to me like there should be a better way. Any ideas?

推荐答案

将颜色视为三维空间中的向量,然后可以使用3d pythagoras:

Consider a color as a vector in 3-dimensional space, you can then easily compute the difference by using 3d pythagoras:

d = sqrt((r2-r1)^2 + (g2-g1)^2 + (b2-b1)^2)

但是,请注意,由于颜色容易被不太完美的解释

However, note that due to colors being subject to interpretation by not-so-perfect eyes, you might want to adjust the colors to avoid them having the same importance.

例如,使用典型的加权方法

d = sqrt(((r2-r1)*0.3)^2 + ((g2-g1)*0.59)^2 + ((b2-b1)*0.11)^2)

由于眼睛对绿色最敏感,对蓝色最不敏感,蓝色分量因此必须具有更大的数字差异,以被认为更不同比在绿色分量中相同的数字差异。

Since eyes are most sensitive to green, and least sensitive to blue, two colors that differ only in the blue component must thus have a larger numeric difference to be considered "more different" than one that is the same numeric difference in the green component.

还有各种方法优化此计算。例如,由于您对实际的 d 值不感兴趣,因此您可以省去平方根:

There's also various ways to optimize this calculation. For instance, since you're not really interested in the actual d value, you can dispense with the square root:

d =   ((r2-r1)*0.30)^2
    + ((g2-g1)*0.59)^2
    + ((b2-b1)*0.11)^2

注意,在许多基于C语法的编程语言), ^ 并不意味着提高到的权力,而是二进制独占或。

Note here that in many C-syntax-based programming languages (like C#), ^ does not mean "raise to the power of", but rather "binary exclusive or".

因此,如果这是C#,您将使用 Math.Pow 来计算该部分,或者只是展开和执行乘法。

So if this was C#, you would use Math.Pow to calculate that part, or just expand and do the multiplication.

添加:按照维基百科上的色差,有各种标准,尝试处理感知差异。例如,CIE94使用不同的公式,在 L * C * h 颜色模型,看起来像值得研究,但它取决于你想要多么准确它是。

Added: Judging by the page on Color difference on Wikipedia, there's various standards that try to handle perceptual differences. For instance, the one called CIE94 uses a different formula, in the L*C*h color model that looks like it's worth looking into, but it depends on how accurate you want it to be.

这篇关于给定一个RGB值什么是最好的方式来找到数据库中最接近的匹配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆