Given an Rgb Value What Would Be the Best Way to Find the Closest Match in the Database

Given an RGB value what would be the best way to find the closest match in the database?

Consider a color as a vector in 3-dimensional space, you can then easily compute the difference by using 3d pythagoras:

d = sqrt((r2-r1)^2 + (g2-g1)^2 + (b2-b1)^2)

However, note that due to colors being subject to interpretation by not-so-perfect eyes, you might want to adjust the colors to avoid them having the same importance.

For instance, using a typical weighted approach:

d = sqrt(((r2-r1)*0.3)^2 + ((g2-g1)*0.59)^2 + ((b2-b1)*0.11)^2)

Since eyes are most sensitive to green, and least sensitive to blue, two colors that differ only in the blue component must thus have a larger numeric difference to be considered "more different" than one that is the same numeric difference in the green component.

There's also various ways to optimize this calculation. For instance, since you're not really interested in the actual d value, you can dispense with the square root:

d =   ((r2-r1)*0.30)^2
+ ((g2-g1)*0.59)^2
+ ((b2-b1)*0.11)^2

Note here that in many C-syntax-based programming languages (like C#), ^ does not mean "raise to the power of", but rather "binary exclusive or".

So if this was C#, you would use Math.Pow to calculate that part, or just expand and do the multiplication.

Added: Judging by the page on Color difference on Wikipedia, there's various standards that try to handle perceptual differences. For instance, the one called CIE94 uses a different formula, in the L*C*h color model that looks like it's worth looking into, but it depends on how accurate you want it to be.

Finding closest matching color name for an RGB value in Google Sheets

Using Euclidean distance and the weights given here:

=sortn( K$2:K, 1, 0, 0.3 * (H$2:H - B2)^2 + 0.59 * (I$2:I - C2)^2 + 0.11 * (J$2:J - D2)^2, true )

In the event your spreadsheet is in a locale that uses commas as decimal marks:

=sortn( K$2:K; 1; 0; 0,3 * (H$2:H - B2)^2 + 0,59 * (I$2:I - C2)^2 + 0,11 * (J$2:J - D2)^2; true )

Given an RGB value what would be the best way to find the closest match in the database?

Consider a color as a vector in 3-dimensional space, you can then easily compute the difference by using 3d pythagoras:

d = sqrt((r2-r1)^2 + (g2-g1)^2 + (b2-b1)^2)

However, note that due to colors being subject to interpretation by not-so-perfect eyes, you might want to adjust the colors to avoid them having the same importance.

For instance, using a typical weighted approach:

d = sqrt(((r2-r1)*0.3)^2 + ((g2-g1)*0.59)^2 + ((b2-b1)*0.11)^2)

Since eyes are most sensitive to green, and least sensitive to blue, two colors that differ only in the blue component must thus have a larger numeric difference to be considered "more different" than one that is the same numeric difference in the green component.

There's also various ways to optimize this calculation. For instance, since you're not really interested in the actual d value, you can dispense with the square root:

d =   ((r2-r1)*0.30)^2
+ ((g2-g1)*0.59)^2
+ ((b2-b1)*0.11)^2

Note here that in many C-syntax-based programming languages (like C#), ^ does not mean "raise to the power of", but rather "binary exclusive or".

So if this was C#, you would use Math.Pow to calculate that part, or just expand and do the multiplication.

Added: Judging by the page on Color difference on Wikipedia, there's various standards that try to handle perceptual differences. For instance, the one called CIE94 uses a different formula, in the L*C*h color model that looks like it's worth looking into, but it depends on how accurate you want it to be.

Find best matching colors out of a database

I worked on a similar project earlier...They used a simple formula to determine which Color is the closest...

Say Rm,Gm,Bm is Color to be Matched....and Rx,Gx,Bx is another color....

So, we calculate e = (Rm-Rx)^2 + (Gm-Gx)^2 + (Bm-Bx)^2....The One with the lowest value was considered close...Our aim is find the (Rx,Gx,Bx) with the minimum e.

Our Query looked like this Select ColorName from Colortable order by (Rm-Rx)*(Rm-Rx)+...(Bm-Bx) TOP 10 ( i dont remember the exact query eithor...)

This gave you the top 10 best matched colors...

Note : I don't stand for the formula, but it works just fine in practical cases.

PHP - Find the closest RGB to predefined RGB from list

Try it like this:

$inputColor = array(20,40,80);

function compareColors($colorA, $colorB) {
return abs($colorA[0] - $colorB[0]) + abs($colorA[1] - $colorB[1]) + abs($colorA[2] - $colorB[2]);
}

$selectedColor = $colors[0];
$deviation = PHP_INT_MAX;

foreach ($colors as $color) {
$curDev = compareColors($inputColor, $color);
if ($curDev < $deviation) {
$deviation = $curDev;
$selectedColor = $color;
}
}

var_dump($selectedColor);

The advantage of this solution is that you can easily replace the comparison function. It might also be possible to use

Disclaimer: There might be more elegant ways of implementation, perhaps leveraging map.

Given a color, how do I find which color it's closest to?

It sounds like you're looking for a way to quantify the "distance" between colors - in math, they'd call it a metric. Many people are intuitively pretty comfortable with the Euclidean metric for example - it's simply the distance between two points as measured with a ruler. In the case of colors, things are more complicated because of subjective perception of different colors.

There's a pretty mathy wikipedia article about color difference, which includes links to different implementations.

The difference or distance between two colors is a metric of interest in color science. It allows people to quantify a notion that would otherwise be described with adjectives, to the detriment of anyone whose work is color critical. Common definitions make use of the Euclidean distance in a device independent color space.

In particular, there's Python Colormath, an implementation in python that converts between different color encodings and also seems to have a function for calculating the distance between two colors. If you happen to be coding in python, that sounds helpful, although I unfortunately don't have any personal experience with that tool. There's also similar resources available for MATLAB and Excel provided by the authors of CIE2000, a leading color-difference formula.



Related Topics



Leave a reply



Submit