【问题标题】:Generate gradient color from PHP从 PHP 生成渐变颜色
【发布时间】:2011-03-31 11:29:47
【问题描述】:

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

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


}

请帮帮我

【问题讨论】:

  • 请澄清您所说的“渐变”和“淡色”是什么意思。最好是带有图像或数值的真实示例。

标签: php css


【解决方案1】:

这个问题的答案在于您的解决方案,仅在 Javascript 中......

Generate lighter/darker color in css using javascript

我不打算把它写出来,但是在谷歌上搜索“lighten hex color 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 和我的一样好!

【讨论】:

  • 你能给我一些php中的东西吗
【解决方案2】:

Michael 引用的代码相当吓人。但解决方案很简单。如果您只考虑灰度图像可能会更清楚:

 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.

【讨论】:

    猜你喜欢
    • 2010-11-19
    • 2011-01-01
    • 1970-01-01
    • 2012-01-03
    • 2012-11-28
    • 2014-08-08
    • 1970-01-01
    • 2017-05-28
    相关资源
    最近更新 更多