Php功能顺序颜色由亮度

时间:2011-08-09 19:11:35

标签: php sorting colors

是否可以通过php在亮度上对颜色进行排序。

现在我用这个函数计算差值

public function colorDiff($rgb1,$rgb2)
{
        // do the math on each tuple
        // could use bitwise operates more efeceintly but just do strings for now.
        $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($rgreen1 - $green2) + abs($blue2 - $blue2) ;

}

但这不会对亮度的图像进行排序。

3 个答案:

答案 0 :(得分:4)

您可以使用以下公式获得合适的亮度值(perceived lightness):

$red * .3 + $green * .59 + $blue * .11

引用链接文章:

  

对这些重量的解释是由于对于等量的颜色,眼睛对绿色最敏感,然后是红色,然后是蓝色。这意味着,对于等量的绿色和蓝色光,绿色将显得更亮。“

答案 1 :(得分:1)

1)你需要数学定义轻盈。所以它应该从颜色到整数来表示亮度

2)如果您认为(例如)它是$ red + $ green + $ blue的总和,您可以使用这种排序

 usort($colors,function ($rgb1,$rgb2){
    $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 ($red1+$green1+$blue1) - ($reg2+$green2+$blue2);
 })

答案 2 :(得分:1)

您可以将RGB颜色转换为HSL颜色空间以及L组件上的排序: http://axonflux.com/handy-rgb-to-hsl-and-rgb-to-hsv-color-model-c