<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Visual Core &#187; Windows</title>
	<atom:link href="http://visualcore.com/index.php/tag/windows/feed/" rel="self" type="application/rss+xml" />
	<link>http://visualcore.com</link>
	<description>An amazing repository of useless junk</description>
	<lastBuildDate>Sat, 18 Jun 2011 05:12:17 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Finding the Nearest Known Color</title>
		<link>http://visualcore.com/index.php/2008/01/finding-the-nearest-known-color/</link>
		<comments>http://visualcore.com/index.php/2008/01/finding-the-nearest-known-color/#comments</comments>
		<pubDate>Mon, 21 Jan 2008 05:49:38 +0000</pubDate>
		<dc:creator>Jeremy Cowles</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[VB.NET]]></category>
		<category><![CDATA[Windows]]></category>

		<guid isPermaLink="false">http://visualcore.com/wp/?p=113</guid>
		<description><![CDATA[I recently ran across an interesting question in a usenet post; How do you convert an arbitrary color to a known / system color?
After some thought, I reasoned: in order to find the nearest known color, you must get the difference for each known color based on your sampled color. You could do this by [...]]]></description>
			<content:encoded><![CDATA[<p>I recently ran across an interesting question in a usenet post; How do you convert an arbitrary color to a known / system color?</p>
<p>After some thought, I reasoned: in order to find the nearest known color, you must get the difference for each known color based on your sampled color. You could do this by looping through each known color in the enumeration Drawing.KnownColor. </p>
<p>Next, you could compare the R, G, and B values (or H, S, V values if that is more appropriate) for the known to the sample. To get the distance, you could either do a simple sum of the absolute differences, or if you are a Man or Woman of science, you would use the <A href="http://en.wikipedia.org/wiki/Root_mean_square">RMS</A> (Root Mean Square) value. Finally, you keep the value with the smallest difference and return it.</p>
<p>In my sample code below, I also created a structure for returning the Name, Color and the Distance &#8211; this is just a little sugar to help the medicine go down, not really necessary.<br />
<code><br />
Private R As New Random</p>
<p>Private Sub Button1_Click(ByVal sender As System.Object, _<br />
              ByVal e As System.EventArgs) _<br />
                      Handles Button1.Click</p>
<p>    Dim randColor As Color = _<br />
                      Color.FromArgb(R.Next(0, 255), _<br />
                                     R.Next(0, 255), _<br />
                                     R.Next(0, 255))</p>
<p>    Me.RandomColorBox.BackColor = randColor<br />
    Me.RandomColorLabel.Text = randColor.ToString</p>
<p>    '// now match to nearest known color<br />
    Dim nearest As ColorName = FindNearestKnown(randColor)<br />
    Me.KnownColorLabel.Text = nearest.Name<br />
    Me.NearestColorBox.BackColor = nearest.Color</p>
<p>End Sub</p>
<p>Structure ColorName<br />
    Public Color As Color<br />
    Public Name As String<br />
    Public Distance As Integer<br />
End Structure</p>
<p>Public Function FindNearestKnown(ByVal c As Color) _<br />
                                        As ColorName<br />
    Dim best As ColorName</p>
<p>    best.Name = Nothing</p>
<p>    For Each colorName As String In _<br />
                    [Enum].GetNames(GetType(KnownColor))<br />
        Dim known As Color = Color.FromName(colorName)<br />
        Dim dist As Integer</p>
<p>        dist = Math.Abs(CInt(c.R) - known.R) _<br />
                + Math.Abs(CInt(c.G) - known.G) _<br />
                + Math.Abs(CInt(c.B) - known.B)</p>
<p>        If best.Name Is Nothing _<br />
                 OrElse dist < best.Distance Then<br />
            best.Color = known<br />
            best.Name = colorName<br />
            best.Distance = dist<br />
        End If<br />
    Next</p>
<p>    Return best<br />
End Function<br />
</code></p>
]]></content:encoded>
			<wfw:commentRss>http://visualcore.com/index.php/2008/01/finding-the-nearest-known-color/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

