#native_company# #native_desc#
#native_cta#

Alternate Row colors in a table (2, ulimited, gradient)

By hedi
on April 24, 2001

Version: 0.99

Type: Full Script

Category: HTML

License: GNU Library Public License

Description: Alternate Row colors in a table (2 colors, ulimited colors, gradient, shade) don t look further
if u need to put some colors in your tables or any where else this code can help you,
Any comment or opinion email me

// NOTES:
// for color reference check this this URL: http://home.i1.net/~dwolfe/hexmixer/000000.shtml;
//
// 
// ------------------------------------------------------------------------
// alternate_2colors
// ------------------------------------------------------------------------
// Alternating colours with two colours
// * example 1
// <table cellpadding=0 cellspacing=0 border=0 width=100%> 
/* <?php
// include('html.php'); html.php contains our function
// $t = 0;
// while($t < 10)
// {
// echo '<TR BGCOLOR="' . $cl = alternate_2colors('#EEEEEE', '#FFFFFF', $t++). '"><td>'. $t . "</td></tr>n";
// }
// ?>
// <table>
*/
// * example 2
// $nb += 1;
// $couleur = alternate_2colors('#EEEEEE', '#FFFFFF', $nb);
// echo'<TD BGCOLOR="'.$couleur.'"align=middle vAlign=center width=30%><SPAN class=small>'. $Ttl . '</SPAN></TD>';
// echo'<TD BGCOLOR="'.$couleur.'"align=middle vAlign=center width=30%><SPAN class=small>'. $Alb . '</SPAN></TD>';
// echo'<TD BGCOLOR="'.$couleur.'"align=middle vAlign=center width=30%><SPAN class=small>'. $Art . '</SPAN></TD>';
// echo'<TD BGCOLOR="'.$couleur.'"align=middle vAlign=center width=9%><SPAN class=small>'. $Time . '</SPAN></TD>';
//
function alternate_2colors($color_1, $color_2, $cpt)
{
 $couleur = ($cpt % 2) ? $color_1 : $color_2;
 return $couleur;
}

// ------------------------------------------------------------------------
// alternate_xcolors
// ------------------------------------------------------------------------
// Alternating colors with unlimited colors
// * $colors Has to be an array
// * $t is an incremented interger 
// * $z iz the maximum value that $t can take
//
// * example
// <table cellpadding=0 cellspacing=0 border=0 width=100%> 
/* <? php
// include('html.php'); html.php contains our function
// $colors   = array();
// $colors[] = "#EDEDED";
// $colors[] = "#CCCCCC";
// $colors[] = "#FFFFFF";
// $colors[] = "#FFFFDF";
//
// $t = 0;
// while($t < 10)
// {
//  echo '<TR BGCOLOR="' . $cl = alternate_Xcolors($colors,$t++,$t). '"><td>'. $t . "</td></tr>n";
// }
// ?>
// <table>
*/
function alternate_Xcolors($colors, $t, $z)
{
 $zb = floor($z/sizeof($colors));
 $y = $z -($zb * sizeof($colors));

 while ( $x < $zb )
 {
  foreach($colors as $col)
  {
   $colr[] = $col;
  }	
 $x++;
 }

 for ($i = 0; $i <= $y-1; $i++)
 {
  $colr[] = $colors[$i];
 }	
 return $colr[$t];
}

// ------------------------------------------------------------------------
// Gradient
// ------------------------------------------------------------------------
// create a gradient of colors from two colors
// * $coldeb is the first color of the gradient in hex.
// * $colfin is the last color of the gradient in hex,
//   this color will be ajasted for mathmatical reasons.
// * $n is the size of the array returned by our function,
//   for example number of rows in a table.
//
// * example
/* <? php
// include('html.php'); html.php contains our function
// $coldeb = '99FF99';
// $colfin = 'CCFFFF';
// $n = 79;
// echo '<table cellpadding=0 cellspacing=0 border=0 width=100%>';
// foreach (Gradient($coldeb, $colfin, $n) as $gr)
// {
//  echo '<tr bgcolor="#' . $gr . '"><td>col</td><td>col2</td></tr>';
// }
// echo '</table>';
// ?>
*/

// calculate the RBG in decimal for an hex color
function SeparateRBG($col)
{
 $Color[R]= hexdec(substr($col, 0, 2 ));
 $Color[B] = hexdec(substr($col, 2, 2));
 $Color[G] = hexdec(substr($col, -2));
 
 return $Color ;
}

// return $GradientColor an array that contien the final gradient
function Gradient($coldeb, $colfin, $n)
{
 $Color[deb] = SeparateRBG($coldeb);
 $Color[fin] = SeparateRBG($colfin);
 $rbg = array('R', 'B', 'G');

 // calculate the red, the bleu, and the green gradient
 foreach ($rbg as $RBG)
 {
  $Color[enc][$RBG] = floor( (($Color[fin][$RBG]) - ($Color[deb][$RBG])) / $n );
  for ($x = 0 ; $x < $n ; $x++)
  {
   $Color[gradient][$RBG][$x] = dechex($Color[deb][$RBG] + ($Color[enc][$RBG] * $x));
   if (strlen(strval($Color[gradient][$RBG][$x])) < 2)
   {
    $Color[gradient][$RBG][$x] = '0' . $Color[gradient][$RBG][$x];
   }
  }
 }
 // build the final gradient array
 for ($i = 0 ; $i < $n ; $i++)
 {
  $GradientColor[] = $Color[gradient][R][$i] . $Color[gradient][B][$i] . $Color[gradient][G][$i];
 }
 return $GradientColor;
}

// ------------------------------------------------------------------------
// Shade
// ------------------------------------------------------------------------
// create a shade of colors from two colors
// * $coldeb is the first color of the gradient in hex.
// * $colfin is the last color of the gradient in hex,
//   this color will be ajasted for mathmatical reasons.
// * $N is the size of the array returned by our function,
//   for example number of rows in a table.
//
// * example
/* <? php
// include('html.php'); html.php contains our function, it has to contien SeparateRBG($col) also
// $coldeb = '99FF99';
// $colfin = 'CCFFFF';
// $n = 79;
// echo '<table cellpadding=0 cellspacing=0 border=0 width=100%>';
// foreach (Shade($coldeb, $colfin, $n) as $gr)
// {
//  echo '<tr bgcolor="#' . $gr . '"><td>col</td><td>col2</td></tr>';
// }
// echo '</table>';
// ?>
*/

function Shade($coldeb, $colfin, $N)
{
 $n = floor($N/2);
 $Color[deb] = SeparateRBG($coldeb);
 $Color[fin] = SeparateRBG($colfin);
 $rbg = array('R', 'B', 'G');

 // calculate the red, the bleu, and the green gradient
 foreach ($rbg as $RBG)
 {
  $Color[enc][$RBG] = floor( (($Color[fin][$RBG]) - ($Color[deb][$RBG])) / $n );
  for ($x = 0 ; $x <= $n ; $x++)
  {
   $Shade[$RBG][$x] = dechex($Color[deb][$RBG] + ($Color[enc][$RBG] * $x));
   if (strlen(strval($Shade[$RBG][$x])) < 2)
   {
    $Shade[$RBG][$x] = '0' . $Shade[$RBG][$x];
   }
  }
 }

 // build the final gradient array
 for ($i = 0 ; $i <= $n ; $i++)
 {
  // $ShadeColor is the array that contien the final garadient
  $ShadeColor[] = $Shade[R][$i] . $Shade[B][$i] . $Shade[G][$i];
 }
 $ShadeColorRev = Array_reverse($ShadeColor);
 array_pop ($ShadeColor);
 $shade = Array_merge($ShadeColor ,$ShadeColorRev );

 unset($ShadeColorRev);
 unset($ShadeColor);
 return $shade;
}


?>