dongyan5239 2012-03-16 12:32
浏览 138
已采纳

使用PHP生成颜色数组

Is it possible to create an array of colors with PHP?

What I'm trying to do:

  1. Select a start and end color
  2. Choose an amount of "steps"
  3. Create an array of those colors blending together (as a gradient) with that amount of steps.

Example:

Start color: White
End color: Black
Steps: 3

Result:

  1. "white",
  2. "gray",
  3. "black"

Of course the colors would be in RGB/hex

Anyone with a solution on this?

  • 写回答

5条回答 默认 最新

  • dqnhfbc3738 2012-03-16 12:47
    关注
     $greys=rainbow('000000','FFFFFF',3);
    
     function rainbow($start, $end, $steps)
     {
        $s=str_to_rgb($start);
        $e=str_to_rgb($end);
        $out=array();
        $r=(integer)($e['r'] - $s['r'])/$steps;
        $g=(integer)($e['g'] - $s['g'])/$steps;
        $b=(integer)($e['b'] - $s['b'])/$steps;
        for ($x=0; $x<$steps; $x++) {
           $out[]=rgb_to_str(
              $s['r']+(integer)($r * $x),
              $s['g']+(integer)($g * $x),
              $s['b']+(integer)($b * $x));
        }
        return $out;
     }
     function rgb_to_str($r, $g, $b)
     {
          return str_pad($r, 2, '0', STR_PAD_LEFT)
              .str_pad($g, 2, '0', STR_PAD_LEFT)
              .str_pad($b, 2, '0', STR_PAD_LEFT);
     }
     function str_to_rgb($str)
     {
        return array (
          'r'=>hexdec(substr($str, 0, 2)),
          'g'=>hexdec(substr($str, 3, 2)),
          'b'=>hexdec(substr($str, 5, 2))
        );
     }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 vue2登录调用后端接口如何实现