2015-03-25 119 views
0

我必须画一些shart(Highcharts,php)。大约有20列。我需要它们是不同的颜色,但颜色应该是某种颜色的阴影(比如绿色)。我基于列上的某些值创建颜色,如:获取一些颜色的调色板

function stringToColorCode($string) { 
    $code = dechex(crc32($string)); 
    $code = substr($code, 0, 6); 
    return '#' . $code; 
} 

这就是我如何获得颜色。我该怎么做才能让所有颜色看起来像绿色?谢谢。

回答

3

有了这个功能,你可以在十六进制格式创建颜色的深或浅的色调:

/** 
* string $code Color in hex format, e.g. '#ff0000' 
* int  $percentage A number between 0 and 1, e.g. 0.3 
*   1 is the maximum amount for the shade, and thus means black 
*   or white depending on the parameter $darken. 
*   0 returns the color in $code itself. 
* boolean $darken True if the function should return a dark shade of $code 
*   or false if the function should return a light shade of $code. 
*/ 
function colorCodeShade($code, $percentage, $darken=true) { 

    // split color code in its rgb parts and convert these into decimal numbers 
    $r = hexdec(substr($code, 1, 2)); 
    $g = hexdec(substr($code, 3, 2)); 
    $b = hexdec(substr($code, 5, 2)); 

    if($darken) { 
     $newR = dechex($r * (1 - $percentage)); 
     $newG = dechex($g * (1 - $percentage)); 
     $newB = dechex($b * (1 - $percentage)); 
    } else { 
     $newR = dechex($r + (255 - $r) * $percentage); 
     $newG = dechex($g + (255 - $g) * $percentage); 
     $newB = dechex($b + (255 - $b) * $percentage); 
    } 

    if(strlen($newR) == 1) $newR = '0'.$newR; 
    if(strlen($newG) == 1) $newG = '0'.$newG; 
    if(strlen($newB) == 1) $newB = '0'.$newB; 

    return '#'.$newR.$newG.$newB; 

} 

echo '<span style="color:'.colorCodeShade('#204a96', 0.5, true).'">test</span>'; 
echo '<span style="color:'.colorCodeShade('#204a96', 0.5, false).'">test</span>';