12,820,546 members (30,125 online)
alternative version

#### Stats

60.2K views
25 bookmarked
Posted 6 Jan 2007

# Find the Nearest Color with C# - Using the Euclidean Distance between Two Colors

, 6 Jan 2007 CPOL
 Rate this:
A short method to find the nearest color

## Description

This small method provides an algorithm to find the nearest (or "most similar") color in a given "colorspace" compared to a given single color. This is done by searching for the least Euclidean distance between the two colors. The Euclidean distance can be computed in an arbitrary n-dimensional space. In this implementation, the value for the alpha-component of the given color to "approximate" is NOT used. In the sample, I have used a little bit of code written by Julijan Sribar (http://www.codeproject.com/cs/miscctrl/MultiTabColorPicker.asp) to receive the "web colors" as the sample color space. The webcolor-space is actually a subspace of the RGB-space.

## The Algorithm

It is quite simple to compute the Euclidean distance between two points. First, convert the three color-components to double-values:

```double dbl_input_red = Convert.ToDouble(input_color.R);
double dbl_input_green = Convert.ToDouble(input_color.G);
double dbl_input_blue = Convert.ToDouble(input_color.B);```

Further define a "similarity measure" (actually this is THE distance). This measure has to be initialized by an arbitrary value which must be greater than the greatest possible distance (basically this is the distance between white and black).

`double distance = 500.0;`

The algorithm to find the least distance is quite simple:

```Color nearest_color = Color.Empty;
foreach (object o in WebColors)
{
// compute the Euclidean distance between the two colors
// note, that the alpha-component is not used in this example
dbl_test_red = Math.Pow(Convert.ToDouble(((Color)o).R) - dbl_input_red, 2.0);
dbl_test_green = Math.Pow(Convert.ToDouble
(((Color)o).G) - dbl_input_green, 2.0);
dbl_test_blue = Math.Pow(Convert.ToDouble
(((Color)o).B) - dbl_input_blue, 2.0);
// it is not necessary to compute the square root
// it should be sufficient to use:
// temp = dbl_test_blue + dbl_test_green + dbl_test_red;
// if you plan to do so, the distance should be initialized by 250000.0
temp = Math.Sqrt(dbl_test_blue + dbl_test_green + dbl_test_red);
// explore the result and store the nearest color
if(temp == 0.0)
{
// the lowest possible distance is - of course - zero
// so I can break the loop (thanks to Willie Deutschmann)
// here I could return the input_color itself
// but in this example I am using a list with named colors
// and I want to return the Name-property too
nearest_color = (Color)o;
break;
}
else if (temp < distance)
{
distance = temp;
nearest_color = (Color)o;
}
}```

## Use

To use the code, simply copy and paste the method `GetNearestWebColor(Color input_color)` into your project. Alternatively you can place a reference to SampleLibrary.dll.

## History

• 6th January, 2007: Initial post

## Share

 Web Developer Germany
Anesthesiologist from Germany
- first contact: 1985 - ATARI 800 XE (there was a great assembler: ATMAS II)
- special interests: my son, number theory, statistics, linear algebra, medicine (of course)

## You may also be interested in...

 First Prev Next
 Finally I Found Something James IV14-May-13 18:45 James IV 14-May-13 18:45
 Better results with HSL andre_dart3-Mar-09 1:48 andre_dart 3-Mar-09 1:48
 Hope it solves a problem coleydog6-May-08 23:00 coleydog 6-May-08 23:00
 Two small suggestions. Willi Deutschmann8-Jan-07 5:13 Willi Deutschmann 8-Jan-07 5:13
 Re: Two small suggestions. _vt_8-Jan-07 7:28 _vt_ 8-Jan-07 7:28
 Re: Two small suggestions. Theo Lagendijk19-Apr-09 1:44 Theo Lagendijk 19-Apr-09 1:44
 Re: Two small suggestions. Willi Deutschmann20-Apr-09 5:35 Willi Deutschmann 20-Apr-09 5:35
 Re: Two small suggestions. Theo Lagendijk20-Apr-09 5:51 Theo Lagendijk 20-Apr-09 5:51
 Re: Two small suggestions. Willi Deutschmann20-Apr-09 7:30 Willi Deutschmann 20-Apr-09 7:30
 No one uses euclidian distance Gilad Kapelushnik7-Jan-07 0:28 Gilad Kapelushnik 7-Jan-07 0:28
 Re: No one uses euclidian distance _vt_7-Jan-07 1:51 _vt_ 7-Jan-07 1:51
 Re: No one uses euclidian distance JohnnyLocust20-Sep-07 10:55 JohnnyLocust 20-Sep-07 10:55
 Last Visit: 31-Dec-99 19:00     Last Update: 26-Mar-17 3:11 Refresh 1