Given the color, how do I find which color it is closest to?

Let's say that I have a list of valid color values, such as [0x67FF82, 0x808080, 0xffffff, ...] and the given input color, in hexadecimal format, I want to find which color is in the list of acceptable colors, that the input color is close to.

My idea is that I will find a color in which the absolute value of the difference between the red, green and blue values ​​is the smallest. Is it correct?

+4
source share
1 answer

It sounds like you're looking for a way to quantify the "distance" between colors - in mathematics they will call it a metric. For example, many people are intuitively quite comfortable with the Euclidean metric - it's just the distance between two points measured by a ruler. In the case of flowers, things are more complicated due to the subjective perception of different colors.

There's a pretty mother Wikipedia article on color difference , which includes links to various implementations.

The difference or distance between two colors is a metric of interest to the science of colors. This allows people to quantify a concept that would otherwise be described with adjectives, to the detriment of someone whose work is critical in color. General definitions use Euclidean distance in a device-independent color space.

In particular, there is Python Colormath , an implementation in python that converts various color encodings, and also appears to have a function to calculate the distance between two colors. If you happen to code in python, this sounds useful, although unfortunately I have no personal experience with this tool. Similar resources are also available there for MATLAB and Excel, provided by the authors of CIE2000 , the leading color formula.

+7
source

Source: https://habr.com/ru/post/1392551/


All Articles