且构网

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

区分 8 种颜色之一的最准确方法是什么?

更新时间:2023-02-27 08:43:23

简短回答:在与设备无关的色彩空间中使用欧几里得距离(来源:色差 ***文章).由于 RGB 与设备相关,因此您应该首先将颜色映射到与设备无关的颜色空间之一.

Short answer: use the Euclidean distance in a device independent color space (source: Color difference article in Wikipedia). Since RGB is device-dependent, you should first map your colors to one of the device-independent color spaces.

我建议将 RGB 转换为 Lab*.再次引用***:

I suggest to convert RGB to Lab*. To quote Wikipedia again:

与 RGB 和 CMYK 颜色模型不同,Lab 颜色旨在近似人类视觉.

Unlike the RGB and CMYK color models, Lab color is designed to approximate human vision.

这是进行转换的方法.获得 Lab 值后,计算您的颜色与所有参考颜色之间的欧几里德距离并选择最接近的一个.

Here's a recipe to do the conversion. Once you have the L, a, b values, calculate the Euclidean distance between your color and all the reference colors and choose the closest one.

实际上,Google Code 上的 python-colormath Python 模块(在 GPL v3 下)) 能够在许多不同的色彩空间之间进行转换,并且还可以计算色差.

Actually, the python-colormath Python module on Google Code (under GPL v3) is capable of converting between many different color spaces and calculates color differences as well.