Find nearest color match

Let's say you have a color, and you want to find the closest match in defined color scheme set. The first problem is defining what closest means. It's not a simple problem to solve.

There are a number of approaches to this, the simplest being to measure the euclidean distance between two colors in a 3D color space - for example RGB, HSL, HSV etc. Here's and article that shows how to do that.

However, these mathematical approaches do not really adjust for color perception. Another color model  CIEL*a*b*, has used various algorithms over the years to create better matches to take account of perceived color differences

CIEDE2000

This is the latest CIE algorithm (from  wikipedia) for comparing two colors for closeness. It's a monster

cie color matching algorithm

I thought I may as well implement this in VBA , javaScript and google apps script to add to my color function libraries. Here's the VBA version. Luckily I found (by Gaurav Sharma, Wencheng Wu,Edul N. Dalal) this paper to get me started. The GAS version is at Color Matching in GAS


Does it work?

Amazingly, it works very well. In Looking up color table I've built up a table of schemes such as Pantone, dulux etc through various web scraping sessions, and now have getting on for 10,000 named colors in that library. To test it, I generate some random colors, then look up the closest in each of the schemes in my color table. Here's the result - I tried to find a match for the first column of random numbers in all of the color table, and also in each of the individual color schemes. Their names are shown along with their color. Pretty close in all cases. Of course the html color scheme is rather short, but still close results.


The gory details

It's not pretty, but here are the main functions in color match algorithm. 

comparing colors use CIE algorithm


and here' the test procedure that generated the above

comparing colors test procedure



Comments