【发布时间】:2014-01-05 02:44:53
【问题描述】:
这个脚本有问题,我不明白。
据我所知,一切都是正确的。该脚本确实可以工作,即使在循环中也可以按预期返回颜色。
问题是输出的颜色不正确...
我已经删除了扫描图像并返回颜色数组的函数,只是使用预设的绿色作为测试。脚本认为它更接近红色,即使绿色值相同!
计算出该块的最小色差:
function getColor($rgb)
{
$colors = array(BLUE =>0x0a9ef3, RED => 0xea0a2f, GREEN => 0x336633);
$largestDiff = 0;
$closestColor = "";
foreach ($colors as $name => $rgbColor)
{
if (!isset($smallestDiff)) {
$smallestDiff = colorDiff($rgbColor,$rgb);
$closestColor = $name;
} else if (colorDiff($rgbColor,$rgb) < $smallestDiff)
{
$smallestDiff = colorDiff($rgbColor,$rgb);
$closestColor = $name;
}
}
return $closestColor;
}
function colorDiff($rgb1,$rgb2)
{
// do the math on each tuple
$red1 = hexdec(substr($rgb1,0,2));
$green1 = hexdec(substr($rgb1,2,2));
$blue1 = hexdec(substr($rgb1,4,2));
$red2 = hexdec(substr($rgb2,0,2));
$green2 = hexdec(substr($rgb2,2,2));
$blue2 = hexdec(substr($rgb2,4,2));
return abs($red1 - $red2) + abs($green1 - $green2) + abs($blue1 - $blue2) ;
}
使用绿色作为测试运行脚本(通常在循环中)
$color = '336633';
$closestmatch = getColor("0x".$color);
输出为红色!救命!
这是 colorDIFF 函数的问题吗?
【问题讨论】:
-
Ive got a problem with this script that i dont understand.叹息 -
请问?说出你的想法。