2014-01-12 26 views

回答

2

在這裏你可以找到兩種方法,將HTML六的ColorCode爲十進制和背部。

Converter methods

使用這些你可以解析十六進制顏色代碼到三個小數號碼。在此之後,你可以做你的轉變。我不是一個色彩專家,但你可以讓他們15%更加激烈。我不確定這是你的意圖,但你可以這樣做:

function html2rgb($color) 
{ 
    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; 

    $r = hexdec($r); $g = hexdec($g); $b = hexdec($b); 

    return array($r, $g, $b); 
} 

function rgb2html($r, $g=-1, $b=-1) 
{ 
    if (is_array($r) && sizeof($r) == 3) 
     list($r, $g, $b) = $r; 

    $r = intval($r); $g = intval($g); 
    $b = intval($b); 

    $r = dechex($r<0?0:($r>255?255:$r)); 
    $g = dechex($g<0?0:($g>255?255:$g)); 
    $b = dechex($b<0?0:($b>255?255:$b)); 

    $color = (strlen($r) < 2?'0':'').$r; 
    $color .= (strlen($g) < 2?'0':'').$g; 
    $color .= (strlen($b) < 2?'0':'').$b; 
    return '#'.$color; 
} 

$hexa = "#CCDDEE"; 
$rgb = html2rgb($hexa) 

// Do the transformation you would like to do 
$rgb[0] *=1.15; 
$rgb[2] *=1.15; 
$rgb[3] *=1.15; 

$hexa = rgb2html($rgb); 

我沒有測試這個解決方案。這只是一個想法,你可以去。

也許它會更容易使用CSS。 CSS支持rgb顏色,如:

p 
{ 
    background-color:rgb(255,0,255); 
} 

這樣可以節省轉換的所有工作並避免這些轉換的可能問題。