使用PHP生成颜色数组

时间:2012-03-16 12:32:44

标签: php arrays colors

是否可以使用PHP创建颜色数组?

我正在尝试做什么:

  1. 选择开始和结束颜色
  2. 选择一定数量的“步骤”
  3. 创建一系列颜色,这些颜色与这些步骤混合在一起(作为渐变)。
  4. 示例:

      

    开始颜色:白色
      结束颜色:黑色
      步骤:3

    结果:

    1. “白色”,
    2. “灰色”,
    3. “黑色”
    4. 当然颜色是RGB / hex

      有解决方案吗?

5 个答案:

答案 0 :(得分:4)

 $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))
    );
 }

答案 1 :(得分:1)

//Assumption: $start and $end have the start/end colors, $steps has the step count

$start=array(255,255,255); //White
$end=array(0,0,0); //Black
$steps=3;

$colors=array($start); //You want the start color to be part of the result array
$intervals=$steps-1; //You want 3 steps to mean 2 intervals

$current=$start;
$delta=array(
   ($end[0]-$start[0])/$intervals,
   ($end[1]-$start[1])/$intervals,
   ($end[2]-$start[2])/$intervals
);

for ($i=1;$i<$intervals;$i++) {
  $current=array($current[0]+$delta[0],$current[1]+$delta[1],$current[2]+$delta[2]);
  $colors[]=array(round($current[0],$current[1],$current[2]);
}

$colors[]=$end; //You want the end color to be part of the result array

将为您提供一系列颜色,每个颜色都是r,g,b

的数组

要创建十六进制表示,请使用

function hexcolorFromArraycolor($arraycolor) {
  return '#'
     .substr('0'.dechex($arraycolor[0]),-2)
     .substr('0'.dechex($arraycolor[1]),-2)
     .substr('0'.dechex($arraycolor[2]),-2)
  ;
}

答案 2 :(得分:1)

假设您有一个数值的颜色,例如从(RGB)0x0000000x112233,首先需要提取所有值:

function hexToArray( $value){
    return array(
      'r' => ($value >> 16) & 0xff,
      'g' => ($value >> 8) & 0xff,
      'b' => ($value >> 0) & 0xff,
    );
}

您必须创建一些步进算法,最明显的是增加每个步骤中的每个组件:

Ri = Rstart + floor(i * (Rend - Rstart)/ steps)

看起来像是:

$steps--; // Due to 0
$a = hexToArray( 0x000000);
$b = hexToArray( 0x112233);
$step = array();
$result = array();

// Prepare steps
foreach( array( 'r', 'g', 'b') as $color){
    $step[$color] = ($b[$color] - $a['color'])/$steps;
}

for( $i = 0; $i <= $steps; $i++){
    $tmp = array();
    foreach( array( 'r', 'g', 'b') as $color){
        $tmp[$color] = $a['color'] + floor($step[$color]*$i);
    }
    $result[] = $tmp;
}
return $result;

您还可以将转换添加回long int或字符串。

当你有十六进制值时,你还需要其他hexToArray函数。

答案 3 :(得分:0)

有关于制作可用作起点的十六进制颜色的基本教程here。关键的想法是能够找出如何沿着单独的RGB base-16十六进制代码前进,以实现颜色的平滑进展,而不仅仅是序列号。

答案 4 :(得分:0)

我需要从驾驶员的路线中获取GPS点并按时间映射它们(使用谷歌地图)。我想我会把彩虹从最古老到最新。我发现http://www.efg2.com/Lab/ScienceAndEngineering/Spectra.htm是一个很好的起点。我重写为PHP函数...

$blue = 0;
$factor = 0;
$green = 0;
$red = 0;

function adjustColor($color) {
    global $factor;
    if ($color == 0.0) {
        return 0;
    } else {
        return round ( 255.0 * pow ( ($color * $factor), 0.80 ) );
    }
}

function setColors($wavelength) {
    global $red, $green, $blue;
    if ($wavelength >= 380 && $wavelength <= 439) {
        $red = - ($wavelength - 440.0) / (440.0 - 380.0);
        $green = 0.0;
        $blue = 1.0;

    } elseif ($wavelength >= 440 && $wavelength <= 489) {
        $red = 0.0;
        $green = ($wavelength - 440.0) / (490.0 - 440.0);
        $blue = 1.0;

    } elseif ($wavelength >= 490 && $wavelength <= 509) {
        $red = 0.0;
        $green = 1.0;
        $blue = - ($wavelength - 510.0) / (510.0 - 490.0);

    } elseif ($wavelength >= 510 && $wavelength <= 579) {
        $red = ($wavelength - 510.0) / (580.0 - 510.0);
        $green = 1.0;
        $blue = 0.0;

    } elseif ($wavelength >= 580 && $wavelength <= 644) {
        $red = 1.0;
        $green = - ($wavelength - 645.0) / (645.0 - 580.0);
        $blue = 0.0;

    } elseif ($wavelength >= 645 && $wavelength <= 780) {
        $red = 1.0;
        $green = 0.0;
        $blue = 0.0;

    } else {
        $red = 0.0;
        $green = 0.0;
        $blue = 0.0;
    }
}

function setFactor($wavelength) {
    global $factor;
    if ($wavelength >= 380 && $wavelength <= 419) {
        $factor = 0.3 + 0.7 * ($wavelength - 380.0) / (420.0 - 380.0);
    } elseif ($wavelength >= 420 && $wavelength <= 700) {
        $factor = 1.0;
    } elseif ($wavelength >= 701 && $wavelength <= 780) {
        $factor = 0.3 + 0.7 * (780.0 - $wavelength) / (780.0 - 700.0);
    } else {
        $factor = 0.0;
    }
}

然后使用这些函数获取每个点的颜色。

for($i = 0; $i < $numSteps; $i ++) {
    $lambda = round ( 380 + 400 * ($i / ($numSteps - 1)) );
    setColors ( $lambda );
    setFactor ( $lambda );
    $red = adjustColor ( $red, $factor );
    $green = adjustColor ( $green, $factor );
    $blue = adjustColor ( $blue, $factor );
    $redHex = dechex ( $red );
    $redHex = (strlen ( $redHex ) < 2) ? "0" . $redHex : $redHex;
    $greenHex = dechex ( $green );
    $greenHex = (strlen ( $greenHex ) < 2) ? "0" . $greenHex : $greenHex;
    $blueHex = dechex ( $blue );
    $blueHex = (strlen ( $blueHex ) < 2) ? "0" . $blueHex : $blueHex;
    $bgcolor = "#" . $redHex . $greenHex . $blueHex;
    echo $bgcolor;
}