如何将单个RGB值与集合进行比较,以便使用CIE94公式找到最接近的匹配项—在Ruby中,

时间:2022-02-10 19:22:49

I have an RGB value, and I would like to find the closest match to that RGB value from a collection of 120 colours.

我有一个RGB值,我想从120种颜色的集合中找到与该RGB值最接近的匹配。

From what I gather, one of the most accurate ways of implementing such a check is to use the CIE94 formula. However, I am having trouble implementing this using Ruby.

据我所知,实现这种检查的最准确的方法之一是使用CIE94公式。然而,我在使用Ruby实现这一点上遇到了麻烦。

How would you suggest I do this?

你建议我怎么做?

          R1, G1, B1 = pixel.red, pixel.green, pixel.blue
          c = Colour.first
              R2, G2, B2 = (c.red * 256), (c.green * 256), (c.blue * 256)

              C1 = Math.sqrt((G1**)+(G2**))
              C2 = Math.sqrt((B1**)+(B2**))
              CAB = C1 - C2

              DA = G1 - G2
              DB = B1 - B2

              DH = Math.sqrt((DA** + DB** - CAB**))

              divergence = Math.sqrt( ((R1-R2)/2)** + (CAB/ (1+(0.048*C1)))** + ( DH / ( 1 + ( 0.014 * C1))** )

Following one of the answers suggestion to use LCMS, I just realized that RMagick (Imagemagick) the ruby library I am using, actually pulls in LCMS. This implies that I may have access to some of this functionality - 'pre-baked'. Is anyone aware of a way to achieve the above using RMagick?

按照其中一个使用LCMS的答案建议,我刚刚意识到我正在使用的ruby库RMagick (Imagemagick)实际上是在使用LCMS。这意味着我可以访问某些功能——“预焙”。有没有人知道使用RMagick实现上述目标的方法?

2 个解决方案

#1


6  

This is actually a lot more tricky than you might expect. For many reasons.

这实际上比你想象的要复杂得多。原因有很多。

The primary issue is that RGB space is a device dependent space - RGB values that look similar on one device need not look similar on another. (If you think this sounds unlikely - there is a huge history of issues between getting good color correspondence between mac / win / SGI due to the different assumptions made by the OS about monitor gamma.)

主要的问题是,RGB空间是一个与设备相关的空间——在一个设备上看起来相似的RGB值在另一个设备上不一定相似。(如果你认为这听起来不太可能——由于OS对monitor gamma的不同假设,在mac / win / SGI之间获得良好的颜色对应之间存在着巨大的历史问题。)

The second issue is that the perception of difference between two colors depends heavily on the surrounding colors / viewing environment. I.e. two colors may look different when viewed in a dark surround in a dark environment, but similar in a white surround in a light environment.

第二个问题是,两种颜色之间的差异感知在很大程度上取决于周围的颜色/观看环境。也就是说,在黑暗的环境中看到两种颜色可能看起来不一样,但在光明的环境中看到白色的环境则相似。

With these provisos in mind Delta CIE 94 is a decent measure of color similarity.

考虑到这些条件,cie94是一个不错的颜色相似性度量。

You'll calculate it by converting each color from RGB to XYZ and then to Lab. The RGB to XYZ transformation is device dependent - there are some simple standard transformations floating about for it, such as sRGB, but their applicability to real devices is questionable. The transformation from XYZ to Lab is complicated and can be found here - for your reference whitepoint you'd use the white point of your device (or sRGB if you're using the standard).

您将通过将每个颜色从RGB转换为XYZ,然后转换为Lab来计算它。RGB转换是与设备相关的——有一些简单的标准转换,比如sRGB,但是它们对实际设备的适用性是值得怀疑的。从XYZ到实验室的转换是复杂的,可以在这里找到——对于您的参考白点,您应该使用设备的白点(如果使用标准,则使用sRGB)。

Finally you can use the formulae you have listed above to do the final transformations.

最后,您可以使用上面列出的公式来进行最终的转换。

Since this is all relatively painful, you may have better luck calling out to a proper color management tool like LCMS

由于这一切都是相对痛苦的,你可能有更好的运气呼唤一个合适的颜色管理工具,如LCMS

#2


0  

This is a bit more tricky than you might expect. You need to convert to convert from RGB → XYZ → LAB → LCH and only then you can apply the CIE94 formula. Take a look at the implementation in Chroma.js and port the parts you need to Ruby:

这比你想象的要复杂一些。你需要从RGB转换将XYZ实验室→→→禄,只有你可以应用CIE94公式。看看Chroma中的实现。并将需要的部件移植到Ruby:

https://github.com/gka/chroma.js

https://github.com/gka/chroma.js

#1


6  

This is actually a lot more tricky than you might expect. For many reasons.

这实际上比你想象的要复杂得多。原因有很多。

The primary issue is that RGB space is a device dependent space - RGB values that look similar on one device need not look similar on another. (If you think this sounds unlikely - there is a huge history of issues between getting good color correspondence between mac / win / SGI due to the different assumptions made by the OS about monitor gamma.)

主要的问题是,RGB空间是一个与设备相关的空间——在一个设备上看起来相似的RGB值在另一个设备上不一定相似。(如果你认为这听起来不太可能——由于OS对monitor gamma的不同假设,在mac / win / SGI之间获得良好的颜色对应之间存在着巨大的历史问题。)

The second issue is that the perception of difference between two colors depends heavily on the surrounding colors / viewing environment. I.e. two colors may look different when viewed in a dark surround in a dark environment, but similar in a white surround in a light environment.

第二个问题是,两种颜色之间的差异感知在很大程度上取决于周围的颜色/观看环境。也就是说,在黑暗的环境中看到两种颜色可能看起来不一样,但在光明的环境中看到白色的环境则相似。

With these provisos in mind Delta CIE 94 is a decent measure of color similarity.

考虑到这些条件,cie94是一个不错的颜色相似性度量。

You'll calculate it by converting each color from RGB to XYZ and then to Lab. The RGB to XYZ transformation is device dependent - there are some simple standard transformations floating about for it, such as sRGB, but their applicability to real devices is questionable. The transformation from XYZ to Lab is complicated and can be found here - for your reference whitepoint you'd use the white point of your device (or sRGB if you're using the standard).

您将通过将每个颜色从RGB转换为XYZ,然后转换为Lab来计算它。RGB转换是与设备相关的——有一些简单的标准转换,比如sRGB,但是它们对实际设备的适用性是值得怀疑的。从XYZ到实验室的转换是复杂的,可以在这里找到——对于您的参考白点,您应该使用设备的白点(如果使用标准,则使用sRGB)。

Finally you can use the formulae you have listed above to do the final transformations.

最后,您可以使用上面列出的公式来进行最终的转换。

Since this is all relatively painful, you may have better luck calling out to a proper color management tool like LCMS

由于这一切都是相对痛苦的,你可能有更好的运气呼唤一个合适的颜色管理工具,如LCMS

#2


0  

This is a bit more tricky than you might expect. You need to convert to convert from RGB → XYZ → LAB → LCH and only then you can apply the CIE94 formula. Take a look at the implementation in Chroma.js and port the parts you need to Ruby:

这比你想象的要复杂一些。你需要从RGB转换将XYZ实验室→→→禄,只有你可以应用CIE94公式。看看Chroma中的实现。并将需要的部件移植到Ruby:

https://github.com/gka/chroma.js

https://github.com/gka/chroma.js