<?php

/*
 * PHP Colour Range Generator
 * Version 1.0 - August 2002
 * (c) 2002, Paul Gregg <pgregg@pgregg.com>
 * http://www.pgregg.com
 *
 * Function: GenerateColourRange( <startcolour>, <endcolour>, <steps>)
 * Takes 3 arguments, the start and end colours of the series, and the
 * number of transitions to get from start->end.
 * The two colour input arguments can be RGB values in array format or hex
 * values:
 * RGB format: array(255, 255, 0) - would represent yellow.
 * Hex format: "#ffff00"
 *
 * The function returns an array containing all the colour values in the range.
 *
 *
 * Open Source Code:   If you use this code on your site for public
 * access (i.e. on the Internet) then you must attribute the author and
 * source web site: http://www.pgregg.com/projects/
 * You must also make this original source code available for download
 * unmodified or provide a link to the source.  Additionally you must provide
 * the source to any modified or translated versions or derivatives.
 *
 * Donations gratefully accepted at: http://www.pgregg.com/donate/
 *
 */
 
Function GenerateColourRange($RGBstart, $RGBend, $range) {

    $ColourTable = array();
    $range--; // We need to take "range -1" steps to get from one colour to the other
    
    // extract the Start colour values
    if (is_array($RGBstart)) {
        list ($Rcolmin, $Gcolmin, $Bcolmin) = $RGBstart;
    } elseif ($RGBstart[0] == '#') {
        $Rcolmin = hexdec(substr($RGBstart, 1, 2));
        $Gcolmin = hexdec(substr($RGBstart, 3, 2));
        $Bcolmin = hexdec(substr($RGBstart, 5, 2));
    }

    // extract the End colour values
    if (is_array($RGBend)) {
        list ($Rcolmax, $Gcolmax, $Bcolmax) = $RGBend;
    } elseif ($RGBstart[0] == '#') {
        $Rcolmax = hexdec(substr($RGBend, 1, 2));
        $Gcolmax = hexdec(substr($RGBend, 3, 2));
        $Bcolmax = hexdec(substr($RGBend, 5, 2));
    }
    
        // Now lets work out the intermediate colours
    $Rrange = 15; $Rcolfrac = ($Rcolmax-$Rcolmin)/$range;
    $Grange = 15; $Gcolfrac = ($Gcolmax-$Gcolmin)/$range;
    $Brange = 15; $Bcolfrac = ($Bcolmax-$Bcolmin)/$range;
    
    for ($i=0; $i<=$range; $i++) {
        $col = sprintf("#%02x%02x%02x",
            $Rcolfrac * $i + $Rcolmin,    
            $Gcolfrac * $i + $Gcolmin,    
            $Bcolfrac * $i + $Bcolmin );

        $ColourTable[$i] = $col;
    }
    return $ColourTable;
}
?>