Bradley Pymm
Bradley Pymm

Reputation: 41

Methods for quantifying the similarity between two colours?

This question may be more to do with human psychology

I am trying to make a program that recreates a bit map image using a limited colour palette. I naively thought that I could simply find the squared difference of the Red/Green/Blue values and use that to quantify the squared difference between colours. However in practice this doesn't seem to work very well, shades of red are being matched to shades of purple and so on.

Does anyone have any suggestions of alternate comparison methods 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;
}

Upvotes: 3

Views: 698

Answers (2)

ThunderStruct
ThunderStruct

Reputation: 1504

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 picked colors (foreground and background) to make sure the foreground is readable. So I made my research and wrote this little C++ library that calculates delta-E using CIE76:

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

Sample:

// 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 validated using this calculator

Upvotes: 0

Kel Solaar
Kel Solaar

Reputation: 4070

This is called colour difference in colour science and there are multiple algorithms to compute it, here are a few of them:

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

Bruce Lindbloom has a calculator and Colour provides a vectorised Python implementation. You will have to convert your RGB values to CIE Lab before being able to use any Delta E implementation.

There are also available colour difference computations implemented on top of CIECAM02 colour appearance model, and especially CAM02-UCS colourspace that is a more uniform version of CIECAM02 underlying colourspace.

Upvotes: 2

Related Questions