且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

给定一个RGB值什么是***的方式来找到数据库中最接近的匹配?

更新时间:2023-08-29 14:14:10

将颜色视为三维空间中的向量,然后可以使用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.