网站16进制色转色三色函数

刘德胜 66次浏览

摘要:有时网站需要做些特效,离不了将色彩转为透明色。functionhtml2rgb($color,$sedaio=0.3,$returnstring=false){ if($color[0]=='#') $color=substr($color,1); if(st...

有时网站需要做些特效,离不了将色彩转为透明色。

function html2rgb($color,$sedaio=0.3,$returnstring=false){
    if ($color[0] == '#') 
       $color = substr($color, 1);
    if (strlen($color) == 6)
       list($r, $g, $b) = array($color[0].$color[1],
                                $color[2].$color[3],
                                $color[4].$color[5]);
    elseif (strlen($color) == 3)
        list($r, $g, $b) = array($color[0].$color[0], $color[1].$color[1], $color[2].$color[2]);
    else
        return false;
    //$key = 1/255; // use this to get a range from 0 to 1 eg: (0.5, 1, 0.1)
    $key = 1; // use this for normal range 0 to 255 eg: (0, 255, 50)
    $r = hexdec($r)*$key;
    $g = hexdec($g)*$key;
    $b = hexdec($b)*$key;
    if($returnstring){
        return "{rgb $r $g $b}";
    }else{
        return "rgba($r,$g,$b,$sedaio)";
    }
}