Search code examples
c++algorithmcolors

Find closest color to a palette algorithm other than Euclidian distance


I am trying to find the closest color in a palette, based on a pixel. The palette is reduced (3-4 colors maybe). I am using gaussian distance (language doesn't matter)

double colorRGBEuclideanDistance(const QColor& c1, const QColor& c2)
{
    return sqrt(pow(c1.red()-c2.red(), 2) +
                pow(c1.green()-c2.green(), 2) +
                pow(c1.blue()-c2.blue(), 2));
}

the problem I have is, running it with a green, darkRed and blue palette on an image like the Hydrangeas from sample pictures in windows, I get dark red leaves instead of green. Based on the original image colors, the color picked from palette in general may not be of the same type as what we perceive with our eyes.

I would like an improvement to this algorithm... but one that will not slow down TOO much finding the color.

I saw that it is possible to implement a CIELab algorithm... but I could not find any details, just mention of it.

I a also thinking that a weighted distance may be beneficial... but how to set the weights ?

I am looking for a better algorithm to find the best color... but something that will not impact performance too much. I hope someone can help ?


Solution

  • Here is all the conversion algorithms. I presume you must convert RGB to absolute color space XYZ, and then - into CIE La*b*

    And here is article explaining color difference and how to measure it with La*b* values.