2014-07-25 74 views
0

我正在尋找一些代碼,以便能夠生成構成PHP中漸變過渡的十六進制顏色列表。例如:PHP:以X步驟從顏色轉換爲顏色

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

+0

我發現,20分鐘後我張貼這一個可行的解決方案。它可以在這裏找到:http://herethere.net/~samson/php/color_gradient/ –

回答

0

我在發佈問題後不久發現瞭解決方案。我猜想搜索的第一個小時是不夠的。

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"; 
?>