PHP Gradient Generator

April 28, 2011
Not really sure what the use of this will be, maybe to trick out a news feed? Use PHP to generate HTML hexadecimal color codes in a linear gradient.

Simply enter the starting color in Hex (000000 through FFFFFF) and ending color, then the count of graduations you'd like to use.

Download Original
  1. function gradient($startcol,$endcol,$graduations=10)
  2. {
  3. $graduations--;
  4.  
  5. $startcoln['r'] = hexdec(substr($startcol,0,2));
  6. $startcoln['g'] = hexdec(substr($startcol,2,2));
  7. $startcoln['b'] = hexdec(substr($startcol,4,2));
  8.  
  9. $GSize['r'] = (hexdec(substr($endcol,0,2))-$startcoln['r'])/$graduations; //Graduation Size Red
  10. $GSize['g'] = (hexdec(substr($endcol,2,2))-$startcoln['g'])/$graduations;
  11. $GSize['b'] = (hexdec(substr($endcol,4,2))-$startcoln['b'])/$graduations;
  12.  
  13. for($i=0;$i<=$graduations;$i++)
  14. {
  15. $HexR = dechex(intval($startcoln['r']+($GSize['r']*$i)));
  16. $HexG = dechex(intval($startcoln['g']+($GSize['g']*$i)));
  17. $HexB = dechex(intval($startcoln['b']+($GSize['b']*$i)));
  18.  
  19. //Make HTML Happy
  20. if (strlen($HexR)==1) $HexR = "0$HexR";
  21. if (strlen($HexG)==1) $HexG = "0$HexG";
  22. if (strlen($HexB)==1) $HexB = "0$HexB";
  23. $HexCol[] = "$HexR$HexG$HexB";
  24. }
  25. RETURN $HexCol;
  26. }

Function Use


Download Original
  1. <?PHP
  2. $Gradient = gradient("000000","123456",10);
  3. foreach ($Gradient as $color)
  4. {
  5. echo "<div style='color:#$color;margin:0px;padding:0px;'>&#9632;&#9632;&#9632;&#9632; Hexadecimal Color: #$color &#9632;&#9632;&#9632;&#9632;</div>";
  6. }


$Gradient above returns the following array:
Array
(
    [0] => 000000
    [1] => 020509
    [2] => 040b13
    [3] => 06111c
    [4] => 081726
    [5] => 0a1c2f
    [6] => 0c2239
    [7] => 0e2842
    [8] => 102e4c
    [9] => 123456
)


End output of this function results in this:

Gradient Example


Name:

No comments yet! Be the first!