从PHP生成渐变颜色

时间:2010-08-18 10:29:36

标签: php css

我想知道如何构建一个给出颜色和代码的函数 显示此颜色的渐变。例如:

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


}

请帮帮我

2 个答案:

答案 0 :(得分: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矢量。

下进行。

答案 1 :(得分: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和我的一样好!

相关问题