Methods for quantifying the similarity between two colors?

This question may be more related to human psychology.

I am trying to create a program that recreates a bitmap image using a limited color palette. I naively thought I could just find the square of the difference between the red / green / blue values ​​and use that to quantify the square of the difference between colors. However, this does not work very well in practice, shades of red correspond to shades of purple, etc.

Does anyone have any suggestions for alternative comparison methods that I could use?

int squaredDistance(colour a, colour b)
{
    return (a.r - b.r)*(a.r - b.r) + (a.g - b.g)*(a.g - b.g) + (a.b - b.b)*(a.r - b.b);
}

int findClosestColour(colour a) //Returns id of colour that most closely matches
{
    int smallestColourDistance = 195075;
    int colourId = -1;
    for(int i=0; i<NUMOFCOLOURS; i++)
    {
        if( squaredDistance(a, coloursById[i]) < smallestColourDistance)
        {
            smallestColourDistance = squaredDistance(a, coloursById[i]);
            colourId = i;
            if(smallestColourDistance == 0)
                break;
        }
    }
    return colourId;
}

      

+3


source to share


2 answers


This is called color difference in color science, and there are several algorithms to calculate it, here are a few of them:

  • Delta E CIE 1976
  • Delta E CIE 1994
  • Delta E CIE 2000
  • Delta E CMC


Bruce Lindblum has a calculator and Color provides a vector Python implementation . You will need to convert your RGB values ​​to CIE Lab before using any Delta E ..

Color differences are also available overlaid on CIECAM02 , and especially the CAM02-UCS color space, which is a more uniform version of CIECAM02, the underlying color space.

+1


source


This is an old question, but in case anyone comes across it ...

I had a similar problem where I wanted to calculate the difference between two randomly selected colors (front and back) to make sure the foreground is readable. So I did my research and wrote this little C ++ library that calculates delta-E using CIE76:

https://github.com/ThunderStruct/Color-Utilities



Example:

// Colors' Construction
ColorUtils::rgbColor c1(1.0f, 1.0f, 1.0f), c2(0.5f, 0.5f, 0.5f);

// Calculate Delta-E
std::cout << ColorUtils::getColorDeltaE(c1, c2) << '\n';

      

This outputs 46.8072

which can be checked with this calculator

0


source







All Articles