开发者

Php function order color by lightness

Is it possible to sort colors by php on lightness .

Now i calc the differenct with this function

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开发者_JAVA百科($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) ;

}

But this will not sort images on lightness.


You can get a decent value for luminance (the perceived lightness) with the following formula:

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

Quoting from the linked article:

The explanation for these weights is due to the fact that for equal amounts of color the eye is most sensitive to green, then red, and then blue. This means that for equal amounts of green and blue light the green will, nevertheless, seem much brighter."


1) You need math defination of lightness. So it should function from color to integer that represent lightness

2) If you suppose (for example) than it is sum of $red+$green+$blue you can use this kind of sort

 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);
 })


You could convert your RGB color to HSL colorspace and the sort on the L component: http://axonflux.com/handy-rgb-to-hsl-and-rgb-to-hsv-color-model-c

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜