5,702,067 members and growing! (15,323 online)
Email Password   helpLost your password?
General Programming » Algorithms & Recipes » General     Beginner

Find the nearest color with C# - Using the Euclidean distance between two colors

By V. Thieme

A short method to find the nearest color
C# 2.0, C#, Windows, .NET, .NET 2.0VS2005, Visual Studio, Dev

Posted: 6 Jan 2007
Updated: 6 Jan 2007
Views: 13,958
Bookmarked: 12 times
Announcements
Loading...



Search    
Advanced Search
Sitemap
5 votes for this Article.
Popularity: 2.10 Rating: 3.00 out of 5
1 vote, 20.0%
1
1 vote, 20.0%
2
0 votes, 0.0%
3
2 votes, 40.0%
4
1 vote, 20.0%
5
Note: This is an unedited contribution. If this article is inappropriate, needs attention or copies someone else's work without reference then please Report This Article

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 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.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

V. Thieme


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)
Occupation: Web Developer
Location: Germany Germany

Other popular Algorithms & Recipes articles:

Article Top
Sign Up to vote for this article
You must Sign In to use this message board.
FAQ FAQ Noise ToleranceSearch Search Messages 
 Layout  Per page   
 Msgs 1 to 6 of 6 (Total in Forum: 6) (Refresh)FirstPrevNext
GeneralHope it solves a problemmembercoleydog23:00 6 May '08  
GeneralTwo small suggestions.memberWilli Deutschmann5:13 8 Jan '07  
AnswerRe: Two small suggestions.member_vt_7:28 8 Jan '07  
GeneralNo one uses euclidian distancememberGilad Kapelushnik0:28 7 Jan '07  
GeneralRe: No one uses euclidian distancemember_vt_1:51 7 Jan '07  
GeneralRe: No one uses euclidian distancememberJohnnyLocust10:55 20 Sep '07  

General General    News News    Question Question    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

PermaLink | Privacy | Terms of Use
Last Updated: 6 Jan 2007
Editor:
Copyright 2007 by V. Thieme
Everything else Copyright © CodeProject, 1999-2008
Web12 | Advertise on the Code Project