2010-08-18 113 views
3

我想知道如何构建一个给出颜色代码的函数,并且 会显示此颜色的渐变。例如:从PHP生成渐变颜色

function generate_color(int colorindex) 
{ ....... 
    ....... 
    Generate 10 pale colors of this color. 


} 

请帮我

+0

请说明您的意思是“渐变”和“苍白的颜色”。具有图像或数字值的真实世界的例子将是最好的。 – 2010-08-18 10:40:45

回答

2

在这个问题的答案在于你的解决方案,只有在Javascript ...

Generate lighter/darker color in css using javascript

我不打算把它写但一个简单的谷歌搜索'减淡十六进制颜色php'产量:

function colourBrightness($hex, $percent) { 
// Work out if hash given 
$hash = ''; 
if (stristr($hex,'#')) { 
    $hex = str_replace('#','',$hex); 
    $hash = '#'; 
} 
/// HEX TO RGB 
$rgb = array(hexdec(substr($hex,0,2)), hexdec(substr($hex,2,2)), hexdec(substr($hex,4,2))); 
//// CALCULATE 
for ($i=0; $i<3; $i++) { 
    // See if brighter or darker 
    if ($percent > 0) { 
    // Lighter 
    $rgb[$i] = round($rgb[$i] * $percent) + round(255 * (1-$percent)); 
    } else { 
    // Darker 
    $positivePercent = $percent - ($percent*2); 
    $rgb[$i] = round($rgb[$i] * $positivePercent) + round(0 * (1-$positivePercent)); 
    } 
    // In case rounding up causes us to go to 256 
    if ($rgb[$i] > 255) { 
    $rgb[$i] = 255; 
    } 
} 
//// RBG to Hex 
$hex = ''; 
for($i=0; $i < 3; $i++) { 
    // Convert the decimal digit to hex 
    $hexDigit = dechex($rgb[$i]); 
    // Add a leading zero if necessary 
    if(strlen($hexDigit) == 1) { 
    $hexDigit = "0" . $hexDigit; 
    } 
    // Append to the hex string 
    $hex .= $hexDigit; 
} 
return $hash.$hex; 
} 

http://lab.pxwebdesign.com.au/?p=14

您的Google和我一样好!

+1

你可以给我一些在PHP中的东西 – eni 2010-08-18 11:02:25

5

迈克尔引用的代码是相当可怕的。但解决方案很简单。如果您仅考虑灰度图像,则可能会更清晰:

function create_pallette($start, $end, $entries=10) 
{ 
    $inc=($start - $end)/($entries-1); 
    $out=array(0=>$start); 
    for ($x=1; $x<$entries;$x++) { 
     $out[$x]=$start+$inc * $x; 
    } 
    return $out; 
} 

仅使用3D矢量(RGB)代替1D矢量。

C.