Geekpedia Programming Tutorials






Generate Gradient Within Hex Range In PHP

A function that generates an array of hex colors that forms a gradient, starting from a hex color and ending with another hex color. The number of gradient steps can also be defined.

On Monday, September 29th 2008 at 03:12 AM
By Andrew Pociu (View Profile)
*****   (Rated 5 with 1 votes)
Contextual Ads
More PHP Resources
Advertisement
  1. function Gradient($HexFrom, $HexTo, $ColorSteps)
  2. {
  3.         $FromRGB['r'] = hexdec(substr($HexFrom, 0, 2));
  4.         $FromRGB['g'] = hexdec(substr($HexFrom, 2, 2));
  5.         $FromRGB['b'] = hexdec(substr($HexFrom, 4, 2));
  6.        
  7.         $ToRGB['r'] = hexdec(substr($HexTo, 0, 2));
  8.         $ToRGB['g'] = hexdec(substr($HexTo, 2, 2));
  9.         $ToRGB['b'] = hexdec(substr($HexTo, 4, 2));
  10.        
  11.         $StepRGB['r'] = ($FromRGB['r'] - $ToRGB['r']) / ($ColorSteps - 1);
  12.         $StepRGB['g'] = ($FromRGB['g'] - $ToRGB['g']) / ($ColorSteps - 1);
  13.         $StepRGB['b'] = ($FromRGB['b'] - $ToRGB['b']) / ($ColorSteps - 1);
  14.        
  15.         $GradientColors = array();
  16.        
  17.         for($i = 0; $i <= $ColorSteps; $i++)
  18.         {
  19.                 $RGB['r'] = floor($FromRGB['r'] - ($StepRGB['r'] * $i));
  20.                 $RGB['g'] = floor($FromRGB['g'] - ($StepRGB['g'] * $i));
  21.                 $RGB['b'] = floor($FromRGB['b'] - ($StepRGB['b'] * $i));
  22.                
  23.                 $HexRGB['r'] = sprintf('%02x', ($RGB['r']));
  24.                 $HexRGB['g'] = sprintf('%02x', ($RGB['g']));
  25.                 $HexRGB['b'] = sprintf('%02x', ($RGB['b']));
  26.                
  27.                 $GradientColors[] = implode(NULL, $HexRGB);
  28.         }
  29.         return $GradientColors;
  30. }
  31.  
  32. $Gradients = Gradient("FF5B5B", "FFCA5B", 32);
  33. foreach($Gradients as $Gradient)
  34. {
  35.         echo "<div style=\"background-color: #".$Gradient."; width: 100px; height: 25px;\"></div>";
  36. }
Digg Digg It!     Del.icio.us Del.icio.us     Reddit Reddit     StumbleUpon StumbleIt     Newsvine Newsvine     Furl Furl     BlinkList BlinkList

Rate Rate this code snippet
Comment Current Comments
by R2 on Monday, November 24th 2008 at 09:17 PM

This works perfect! Thanks a lot dude. it produces a valid html :)

by Sicle on Tuesday, July 14th 2009 at 06:31 PM

Hi,

I tryed this code but it didn't work quite well for me.

I tryed a couple of things but the height of every color would always be 25px.

So I deleted the div tag and replaced it with a table:

Replace:
#
foreach($Gradients as $Gradient)
#
{
#
echo "<div style=\"background-color: #".$Gradient."; width: 100px; height: 25px;\"></div>";
#
}

With:
echo '<table style="border-collapse:collapse;">';

foreach($Gradients as $Gradient)

{

echo "<tr><td style=\"background-color: #".$Gradient."; width: 100px; height: 1px; margin: 0px 0px;\"></td></tr>";

}

echo '</table>';

now the height of every gradient color can be et in the td tag, make sure the table tag has 'border-collapse:collapse;' in its style

by Will on Tuesday, April 27th 2010 at 02:12 AM

Cool beans!
minor bug:

- for($i = 0; $i <= $ColorSteps; $i )
for($i = 0; $i < $ColorSteps; $i )

the equals adds an extra value to the end that is beyond the value $HexTo.


Comment Comment on this tutorial
Name: Email:
Message:
Comment Related Source Code
There is no related code.

Comment Related Tutorials
There are no related tutorials.

Jobs PHP Job Search
My skills include:

Enter a City:

Select a State:


Advanced Search >>
Advertisement

Free Magazine Subscriptions

Today's Pictures

Today's Video

Other Resources

Latest Download

Latest Icons