Functions

This is a great little function to create an interesting effect with linear data. I copied most of it from here:

http://www.herethere.net/~samson/php/color_gradient/color_gradient_generator.php.txt

<?php

$steps = 10;

function getSeverity($value,$steps) {

    $x = round($value);
    $start = hexdec('60BB22');
    $end = hexdec('CC0000');

    if ($x >= $steps) {
        $x = $steps;
    }

    $theR0 = ($start & 0xff0000) >> 16;
    $theG0 = ($start & 0x00ff00) >> 8;
    $theB0 = ($start & 0x0000ff) >> 0;

    $theR1 = ($end & 0xff0000) >> 16;
    $theG1 = ($end & 0x00ff00) >> 8;
    $theB1 = ($end & 0x0000ff) >> 0;
    $theR = interpolate($theR0, $theR1, $x, $steps);
    $theG = interpolate($theG0, $theG1, $x, $steps);
    $theB = interpolate($theB0, $theB1, $x, $steps);

    $theVal = ((($theR << 8) | $theG) << 8) | $theB;
    $result = sprintf("#%06X", $theVal);

    return $result;
}

function interpolate($pBegin, $pEnd, $pStep, $pMax) {

    if ($pBegin < $pEnd) {
      return (($pEnd - $pBegin) * ($pStep / $pMax)) + $pBegin;
    } else {
      return (($pBegin - $pEnd) * (1 - ($pStep / $pMax))) + $pEnd;
    }

}

echo "<table cellpadding=0 cellspacing=0>\n";

for ($i = 1; $i <=$steps; $i++) {
    $colour = getSeverity($i,$steps);
    echo "
         <tr>
           <td align=center width=100 style=\"background: $colour;\">
               $colour
           </td>
         </tr>\n";
}

echo "</table>\n";
?>