【问题标题】:PHP: Transition from color to color in X stepsPHP:在 X 步中从颜色过渡到颜色
【发布时间】:2014-09-17 17:57:01
【问题描述】:

我正在寻找一些代码来生成一个十六进制颜色列表,这些颜色构成 PHP 中的渐变过渡。例如:

function gradientColors($startHex, $endHex, $numberOfSteps) { // Code goes here }

所以,如果我要打电话

var_dump(gradientColors('#204E32', '#224970', 9));

它会输出这个:

array("204E32","204D39","204C41","204C49","214B51","214A58","214A60","214968","224970");

我基本上想重新创建这个页面:

http://www.strangeplanet.fr/work/gradient-generator/?c=9:204E32:224970

【问题讨论】:

标签: php colors hex transition


【解决方案1】:

我在发布问题后不久就找到了解决方案。我想第一个小时的搜索还不够。

http://herethere.net/~samson/php/color_gradient/

这是来自该链接的源代码:

<?
  $theColorBegin = (isset($_REQUEST['cbegin'])) ? hexdec($_REQUEST['cbegin']) : 0x000000;
  $theColorEnd = (isset($_REQUEST['cend'])) ? hexdec($_REQUEST['cend']) : 0xffffff;
  $theNumSteps = (isset($_REQUEST['steps'])) ? intval($_REQUEST['steps']) : 16;

  $theColorBegin = (($theColorBegin >= 0x000000) && ($theColorBegin <= 0xffffff)) ? $theColorBegin : 0x000000;
  $theColorEnd = (($theColorEnd >= 0x000000) && ($theColorEnd <= 0xffffff)) ? $theColorEnd : 0xffffff;
  $theNumSteps = (($theNumSteps > 0) && ($theNumSteps < 256)) ? $theNumSteps : 16;
?>
  <form method="GET">
    <table border='1'>
      <tr>
        <td>variable:</td>
        <td>number type</td>
        <td>minimum</td>
        <td>maximum</td>
        <td>value</td>
      </tr>
      <tr>
        <td>color begin:</td>
        <td>hex</td>
        <td>0x000000</td>
        <td>0xFFFFFF</td>
        <td><input name="cbegin" value="<? printf("%06X", $theColorBegin); ?>"></td>
      </tr>
      <tr>
        <td>color end:</td>
        <td>hex</td>
        <td>0x000000</td>
        <td>0xFFFFFF</td>
        <td><input name="cend" value="<? printf("%06X", $theColorEnd); ?>"></td>
      </tr>
      <tr>
        <td>number of steps:</td>
        <td>dec</td>
        <td>1</td>
        <td>255</td>
        <td><input name="steps" value="<? echo $theNumSteps; ?>"></td>
      </tr>
    </table>
    <input type="submit" value="generate color gradient">
  </form>
<?
  printf("<p>values are: (color begin: 0x%06X), (color end: 0x%06X), (number of steps: %d)</p>\n", $theColorBegin, $theColorEnd, $theNumSteps);

  $theR0 = ($theColorBegin & 0xff0000) >> 16;
  $theG0 = ($theColorBegin & 0x00ff00) >> 8;
  $theB0 = ($theColorBegin & 0x0000ff) >> 0;

  $theR1 = ($theColorEnd & 0xff0000) >> 16;
  $theG1 = ($theColorEnd & 0x00ff00) >> 8;
  $theB1 = ($theColorEnd & 0x0000ff) >> 0;

  // return the interpolated value between pBegin and pEnd
  function interpolate($pBegin, $pEnd, $pStep, $pMax) {
    if ($pBegin < $pEnd) {
      return (($pEnd - $pBegin) * ($pStep / $pMax)) + $pBegin;
    } else {
      return (($pBegin - $pEnd) * (1 - ($pStep / $pMax))) + $pEnd;
    }
  }

  // generate gradient swathe now
  echo "<table width='100%' cellpadding='8' style='border-collapse:collapse'>\n";
  for ($i = 0; $i <= $theNumSteps; $i++) {
    $theR = interpolate($theR0, $theR1, $i, $theNumSteps);
    $theG = interpolate($theG0, $theG1, $i, $theNumSteps);
    $theB = interpolate($theB0, $theB1, $i, $theNumSteps);

    $theVal = ((($theR << 8) | $theG) << 8) | $theB;

    $theTDTag = sprintf("<td bgcolor='#%06X'>", $theVal);
    $theTDARTag = sprintf("<td bgcolor='#%06X' align='right'>", $theVal);

    $theFC0Tag = "<font color='#000000'>";
    $theFC1Tag = "<font color='#ffffff'>";
          printf("<tr>$theTDTag$theFC0Tag%d</font></td>$theTDTag$theFC0Tag%d%%</font></td>$theTDARTag$theFC0Tag%d</font></td>$theTDARTag$theFC0Tag%06X</font></td>", $i, ($i/$theNumSteps) * 100, $theVal, $theVal);
    printf("$theTDTag$theFC1Tag%06X</font></td>$theTDTag$theFC1Tag%d</font></td>$theTDARTag$theFC1Tag%d%%</font></td>$theTDARTag$theFC1Tag%d</font></td></tr>\n", $theVal, $theVal, ($i/$theNumSteps) * 100, $i);
  }
  echo "</table>\n";
?>

【讨论】:

    猜你喜欢
    • 2021-04-01
    • 1970-01-01
    • 2011-03-27
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    • 1970-01-01
    • 2021-11-29
    相关资源
    最近更新 更多