Programmatically choose high-contrast colors

前端 未结 10 671
野的像风
野的像风 2020-12-14 07:29

This should be a simple question, but I haven\'t been able to find a way to make it work.

Essentially, I have a silly localhost page that I use in my webdevelopment.

10条回答
  •  时光说笑
    2020-12-14 07:59

    For best contrast use this code

    function lumdiff($R1,$G1,$B1,$R2,$G2,$B2){
    
        $L1 = 0.2126 * pow($R1/255, 2.2) +
              0.7152 * pow($G1/255, 2.2) +
              0.0722 * pow($B1/255, 2.2);
    
        $L2 = 0.2126 * pow($R2/255, 2.2) +
              0.7152 * pow($G2/255, 2.2) +
              0.0722 * pow($B2/255, 2.2);
    
        if($L1 > $L2){
            return ($L1+0.05) / ($L2+0.05);
        }else{
            return ($L2+0.05) / ($L1+0.05);
        }
    }
    
    function get_the_contrast($c1, $c2) {
        return (lumdiff(hexdec(substr($c1,0,2)),
            hexdec(substr($c1,2,2)),hexdec(substr($c1,4,2)),
            hexdec(substr($c2,0,2)),hexdec(substr($c2,2,2)),
            hexdec(substr($c2,4,2))));
    }
    

    The method above ( AVG(red,green,blue) > 128 ) is not realy good.

提交回复
热议问题