2010-12-03 73 views
3

生成十六進制顏色列表?生成平滑的十六進制漸變表

那裏嗨,

目前,我試圖生成的創造從白到黑一大致平滑漸變,其間所有顏色50進制顏色列表。

我該如何去做這個在PHP?

+4

**從黑色到白色的線性**漸變只能包含灰度色。 – zerkms 2010-12-03 04:47:47

回答

6

好,

雖然顏色可能概率是在一個更好的順序,

這是我的工作。

<?php 

function Gradient($HexFrom, $HexTo, $ColorSteps) { 
    $FromRGB['r'] = hexdec(substr($HexFrom, 0, 2)); 
    $FromRGB['g'] = hexdec(substr($HexFrom, 2, 2)); 
    $FromRGB['b'] = hexdec(substr($HexFrom, 4, 2)); 

    $ToRGB['r'] = hexdec(substr($HexTo, 0, 2)); 
    $ToRGB['g'] = hexdec(substr($HexTo, 2, 2)); 
    $ToRGB['b'] = hexdec(substr($HexTo, 4, 2)); 

    $StepRGB['r'] = ($FromRGB['r'] - $ToRGB['r'])/($ColorSteps - 1); 
    $StepRGB['g'] = ($FromRGB['g'] - $ToRGB['g'])/($ColorSteps - 1); 
    $StepRGB['b'] = ($FromRGB['b'] - $ToRGB['b'])/($ColorSteps - 1); 

    $GradientColors = array(); 

    for($i = 0; $i <= $ColorSteps; $i++) { 
    $RGB['r'] = floor($FromRGB['r'] - ($StepRGB['r'] * $i)); 
    $RGB['g'] = floor($FromRGB['g'] - ($StepRGB['g'] * $i)); 
    $RGB['b'] = floor($FromRGB['b'] - ($StepRGB['b'] * $i)); 

    $HexRGB['r'] = sprintf('%02x', ($RGB['r'])); 
    $HexRGB['g'] = sprintf('%02x', ($RGB['g'])); 
    $HexRGB['b'] = sprintf('%02x', ($RGB['b'])); 

    $GradientColors[] = implode(NULL, $HexRGB); 
    } 
    $GradientColors = array_filter($GradientColors, "len"); 
    return $GradientColors; 
} 

function len($val){ 
    return (strlen($val) == 6 ? true : false); 
} 

$count = 0; 
$steps = 9; 
$Gradients = Gradient("FFFFFF", "FF0000", $steps); 
foreach($Gradients as $Gradient) 
    echo '<div style="background-color: #' . strtoupper($Gradient) . '">' . htmlentities('<option value="' . strtoupper($Gradient) . '">' . strtoupper($Gradient) . '</option>') . '</div>'; 
$count += count($Gradients); 

$Gradients = Gradient("df1f00", "00FF00", $steps); 
foreach($Gradients as $Gradient) 
    echo '<div style="background-color: #' . strtoupper($Gradient) . '">' . htmlentities('<option value="' . strtoupper($Gradient) . '">' . strtoupper($Gradient) . '</option>') . '</div>'; 
$count += count($Gradients); 

$Gradients = Gradient("00df1f", "0000FF", $steps); 
foreach($Gradients as $Gradient) 
    echo '<div style="background-color: #' . strtoupper($Gradient) . '">' . htmlentities('<option value="' . strtoupper($Gradient) . '">' . strtoupper($Gradient) . '</option>') . '</div>'; 
$count += count($Gradients); 

$Gradients = Gradient("0000df", "000000", $steps); 
foreach($Gradients as $Gradient) 
    echo '<div style="background-color: #' . $Gradient . '">' . htmlentities('<option value="' . $Gradient . '">' . $Gradient . '</option>') . '</div>'; 
$count += count($Gradients); 

echo 'count: ' . $count; 
0

下面是函數的一些定製,它也處理情況下,只有1或2需要畢業,並且還取消對六#微性之前和發送回色的陣列開始#。這樣可以直接應用於內容。

public function gradient($from_color, $to_color, $graduations = 10) { 
     $graduations--; 
     $startcol = str_replace("#", "", $from_color); 
     $endcol = str_replace("#", "", $to_color); 
     $RedOrigin = hexdec(substr($startcol, 0, 2)); 
     $GrnOrigin = hexdec(substr($startcol, 2, 2)); 
     $BluOrigin = hexdec(substr($startcol, 4, 2)); 
     if ($graduations >= 2) { // for at least 3 colors 
      $GradientSizeRed = (hexdec(substr($endcol, 0, 2)) - $RedOrigin)/$graduations; //Graduation Size Red 
      $GradientSizeGrn = (hexdec(substr($endcol, 2, 2)) - $GrnOrigin)/$graduations; 
      $GradientSizeBlu = (hexdec(substr($endcol, 4, 2)) - $BluOrigin)/$graduations; 
      for ($i = 0; $i <= $graduations; $i++) { 
       $RetVal[$i] = strtoupper("#" . str_pad(dechex($RedOrigin + ($GradientSizeRed * $i)), 2, '0', STR_PAD_LEFT) . 
         str_pad(dechex($GrnOrigin + ($GradientSizeGrn * $i)), 2, '0', STR_PAD_LEFT) . 
         str_pad(dechex($BluOrigin + ($GradientSizeBlu * $i)), 2, '0', STR_PAD_LEFT)); 
      } 
     } elseif ($graduations == 1) { // exactlly 2 colors 
      $RetVal[] = $from_color; 
      $RetVal[] = $to_color; 
     } else { // one color 
      $RetVal[] = $from_color; 
     } 
     return $RetVal; 
    }