Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Finding best color match - and reject if no shades of color available

Tags:

c++

colors

qt

Is there a way to identify colors, given by their hex codes, as being shades of a certain color ?
I think I found a way, not sure how accurate - but how can I tell if the color match I found is not good enough ?

I need to be able to identify color matches for specific colors - a fixed set (red, yellow, orange, blue, green, brown, purple, gray, black, white).

At the moment I am doing a color match based on distance:

Given a color c from the fixed color list above (which I set using hex colors to hopefully be in the middle of the range for that color, which I am not quite sure how to get - right now I am using a color that looks "good"), and a list of colors available list, I try to find the index from the list with the closest color.

int matchColor(QColor c, QList<QColor> list)
{
    int bestIndex = 0;
    int distance;
    int bestMatch = 0;
    int sz = list.size() - 1;

    for (int i = 0; i <= sz; ++i)
    {
        int Rdiff = 255 - qAbs(c.red() - list.at(i).red());
        int Gdiff = 255 - qAbs(c.green() - list.at(i).green());
        int Bdiff = 255 - qAbs(c.blue() - list.at(i).blue());
        distance = Rdiff + Gdiff + Bdiff;

        if (distance > bestMatch)
        {
            bestMatch = distance;
            bestIndex = i;
        }
    }

    // if(bestMatch < 600 ? or something ?) return -1;

    return bestIndex;
}

The problem is, because this match is supposed to be strictly enforced (for some standards), I must return shades of the specific color c and fail if I can't.
I am not 100% certain the color match is good - though it seems fairly ok.

But other than finding the best color match - what is a reasonable value of distance, for which I should reject a match ? (expecting distance between 0 and 765, with 765 being perfect match).
Are there certain ranges I can check ?

Would the range be different for each color ?

Note - I found some similar questions, some have references to more complex algorithms that I could not understand - but I saw nothing that can tell me how to reject a match... I would appreciate a better solution if any.

like image 461
Thalia Avatar asked Dec 25 '22 03:12

Thalia


1 Answers

It is basically impossible to tell you which tolerances are acceptable to your use case, but I would strongly suggest converting to HSV before trying to match colors. The H component is critical, and should probably have a tolerance of no more than 20 or 30 degrees. S and V are far less critical; you might be able to either ignore them, or only filter out the extreme cases where the hue disappears completely.

The conversion is described here: Algorithm to convert RGB to HSV and HSV to RGB in range 0-255 for both

like image 193
H. Guijt Avatar answered May 19 '23 09:05

H. Guijt