How to compare two colors for similarity/difference
See Wikipedia's article on Color Difference for the right leads. Basically, you want to compute a distance metric in some multidimensional colorspace.
But RGB
is not "perceptually uniform", so your Euclidean RGB
distance metric suggested by Vadim will not match the human-perceived distance between colors. For a start, L*a*b*
is intended to be a perceptually uniform colorspace, and the deltaE metric is commonly used. But there are more refined colorspaces and more refined deltaE formulas that get closer to matching human perception.
You'll have to learn more about colorspaces and illuminants to do the conversions. But for a quick formula that is better than the Euclidean RGB
metric, just do this:
- Assume that your
RGB
values are in thesRGB
colorspace - Find the
sRGB
toL*a*b*
conversion formulas - Convert your
sRGB
colors toL*a*b*
- Compute deltaE between your two
L*a*b*
values
It's not computationally expensive, it's just some nonlinear formulas and some multiplications and additions.
Just an idea that first came to my mind (sorry if stupid). Three components of colors can be assumed 3D coordinates of points and then you could calculate distance between points.
F.E.
Point1 has R1 G1 B1
Point2 has R2 G2 B2
Distance between colors is
d=sqrt((r2-r1)^2+(g2-g1)^2+(b2-b1)^2)
Percentage is
p=d/sqrt((255)^2+(255)^2+(255)^2)