{A}{S0}说明
这个小方法提供了一种算法来找到最近的(或quot;最similarquot)的颜色在一个给定的quot; colorspacequot相比,一个给定的单一颜色。这是通过搜索至少在两种颜色之间的欧几里德距离。欧氏距离,可以计算出在任意一个n维空间。在此实现,价值α-组件给定的颜色quot; approximatequot;不使用。在示例中,我使用一个有点Julijan Sribar()代码写入收到quot;网站colorsquot;作为样本的色彩空间。 webcolor的空间实际上是RGB空间的子空间。该算法
是很简单的计算两点之间的欧氏距离。首先,转换的三色组成部分,双值: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);
进一步明确了quot;相似性measurequot;(其实这是距离)。这项措施已被初始化一个任意值必须大于最大可能的距离(基本上这是白人和黑人之间的距离)。
该算法找到最小距离很简单:
使用
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;
}
}
使用代码,只需复制并粘贴到您的项目的方法GetNearestWebColor(彩色input_color)。另外,您可以放置一个SampleLibrary.dll参考。历史2007年1月6日:战后初期