【问题标题】:PHP colour recognition script returns wrong colourPHP颜色识别脚本返回错误的颜色
【发布时间】: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. 叹息
  • 请问?说出你的想法。

标签: php function colors


【解决方案1】:

使用$closestmatch = getColor($color); 而不是$closestmatch = getColor("0x".$color);

$color = '336633';
echo getColor($color); // GREEN

【讨论】:

  • 它最初确实有效,但在更改输入颜色 t 'ea0a2f' 后,当它应该完全等于红色时,我返回蓝色!
猜你喜欢
  • 1970-01-01
  • 2020-11-11
  • 1970-01-01
  • 2011-12-18
  • 1970-01-01
  • 2012-02-08
  • 1970-01-01
  • 2023-04-03
  • 2021-10-01
相关资源
最近更新 更多