Click here to Skip to main content
Licence CPOL
First Posted 6 Jan 2007
Views 36,510
Downloads 531
Bookmarked 19 times

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

By | 6 Jan 2007 | Article
A short method to find the nearest color
Sample Image - NearestColorFinder.png

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

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

About the Author

V. Thieme

Web Developer

Germany Germany

Member

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)

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralBetter results with HSL Pinmemberandre_dart0:48 3 Mar '09  
GeneralHope it solves a problem Pinmembercoleydog22:00 6 May '08  
The things one does. Where does it lead.
 
I'd bet you'd never guess... My problem is with Citrix and client PC color depth.
 
The Citrix clients run at 8-bit color versus my development laptop at 32-bit color.
 
The problem is that it is almost impossible to find a back color for a control that doesn't appear 'hashed' (making the text almost unreadable) under 8-bit screen color on a Citrix client.
 
I am hoping that the list of web colors will reflect the actual colors that work in 8-bit depth on the Citrix client.
 
I'll let you know.
GeneralTwo small suggestions. PinmemberWilli Deutschmann4:13 8 Jan '07  
AnswerRe: Two small suggestions. Pinmember_vt_6:28 8 Jan '07  
GeneralRe: Two small suggestions. PinmemberTheo Lagendijk0:44 19 Apr '09  
GeneralRe: Two small suggestions. PinmemberWilli Deutschmann4:35 20 Apr '09  
GeneralRe: Two small suggestions. PinmemberTheo Lagendijk4:51 20 Apr '09  
GeneralRe: Two small suggestions. PinmemberWilli Deutschmann6:30 20 Apr '09  
GeneralNo one uses euclidian distance PinmemberGilad Kapelushnik23:28 6 Jan '07  
GeneralRe: No one uses euclidian distance Pinmember_vt_0:51 7 Jan '07  
GeneralRe: No one uses euclidian distance PinmemberJohnnyLocust9:55 20 Sep '07  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web04 | 2.5.120529.1 | Last Updated 6 Jan 2007
Article Copyright 2007 by V. Thieme
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid