Social Networks




Follow sproutworks on Twitter
Enter your Email


Powered by FeedBlitz

RSS Feed

Links

Blogshares Links

The BlogShares fantasy blog stock market.

BlogShares Price Tracker
I wrote this program to archive information from the BlogShares fantasy stock market. You can view graphs of any industry, and analyze your portfolio.

SproutWorks Projects

Digg Archive
A new experimental Digg page.
AJAX Pixel Editor
A Collaborative pixel editor currently in development.
Web promotion links
These tools help you get visitors on your website.
SproutPics
My photography Site
SproutSearch
I designed this blog indexing tool, and it has accumulated over 6 million blogs so far.
Products
Some of the programs I've written.
SproutTree Demo
A demo of a tree-drawing PHP script.
My Gallery

Sign In

Username:
Password:
Remember Me

sprout man
modules
./
pathfile sizelines
color.php139961
gradient2.php5591230
6,990 bytes total
<?

require_once 'color.php';

class Gradient {

    var $width;
    var $height;

    var $colors;
    var $vertColors;
    
    var $drawMode;  // where to draw into
    
    function Gradient($colors) {

        $this->colors = $colors;
        //$this->vertColors= $vertColors;

        $this->width = 300;
        $this->height = 60;
    }

    function setSize($width, $height) {
        $this->width = $width;
        $this->height = $height;
    }

    function setDrawToBuffer() {
        $this->drawMode = 'buffer';
    }

    function setVertColors($vertColors) {
        $this->vertColors = $vertColors;
    }
    
    function draw($saveAs = null) {

        $slicesX = count($this->colors) - 1;
        $slicesY = count($this->vertColors) - 1;

        $sliceWidth = $this->width / (count($this->colors) - 1);
        $sliceHeight = $this->height / (count($this->vertColors) - 1);
        $paletteImg = imagecreatetruecolor($this->width, $this->height);
        //echo count($this->vertColors);

        for ($slice = 0; $slice < count($this->colors) - 1; $slice++) {
            $startColor = $this->colors[$slice];
            $endColor = $this->colors[$slice + 1];
            $diffColor = $endColor->difference($startColor);

            $rInc = $diffColor->red;
            $gInc = $diffColor->green;
            $bInc = $diffColor->blue;

            // calculate horizontal color change

            $rInc /= $sliceWidth;
            $gInc /= $sliceWidth;
            $bInc /= $sliceWidth;

            $r = $startColor->red;
            $g = $startColor->green;
            $b = $startColor->blue;

            for ($x=$slice * $sliceWidth; $x < $slicesX * $sliceWidth; $x++) {
                $currentColor = new Color($r, $g, $b);

                for ($ySlice=0; $ySlice < $slicesY; $ySlice++) {
                    //$yColor = new Color($r, $g, $b);
                    $yStartColor = $this->vertColors[$ySlice];
                    $yEndColor = $this->vertColors[$ySlice+1];
                    
                    $a = Color::blend($currentColor, $yStartColor);
                    
                    $bb = Color::blend($currentColor, $yEndColor);
                    //list($a, $b, $c) = $yStartColor->difference($currentColor);
                    //$t = new Color($a, $b, $c);
                $diffColor = $bb->difference($a);

                $rIncY = $diffColor->red;
                $gIncY = $diffColor->green;
                $bIncY = $diffColor->blue;
                    
                    $r1 = $a->red;
                    $g1 = $a->green;
                    $b1 = $a->blue;
                    
                    // calculate vertical color intrement
                    $rIncY /= $sliceHeight;
                    $gIncY /= $sliceHeight;
                    $bIncY /= $sliceHeight;

                    for ($y=$ySlice * $sliceHeight; $y < ($ySlice+1) * $sliceHeight; $y++) {

                        $newColor = imagecolorallocate($paletteImg, round($r1), round($g1), round($b1));
                        //if ($newColor == -1) echo 'error';
                        imagesetpixel($paletteImg, $x, $y, $newColor);

                        $r1 += $rIncY;
                        $g1 += $gIncY;
                        $b1 += $bIncY;

                    }
                }
                $r += $rInc;
                $g += $gInc;
                $b += $bInc;
            }    // x slice


        } // x slice loop

        if ($this->drawMode == 'buffer')
            return $paletteImg;

        if (isset($saveAs)) {
            imagejpeg($paletteImg, 'images/gradients/' . $saveAs);
        }
            header("Content-type: image/jpg");
            imagejpeg($paletteImg);
        
    }
}

$colors = null;

// set the number of colors
if (isset($_GET['numcolors_x']))
    $numColorsX = $_GET['numcolors_x'];
else $numColorsX = 1;

if (isset($_GET['numcolors_y']))
    $numColorsY = $_GET['numcolors_y'];
else $numColorsY = 2;

if (empty($_GET['gradientmode'])) $_GET['gradientmode'] = 'palette';
if (isset($_GET['gradientmode'])) {
    switch ($_GET['gradientmode']) {
        default:
        case 'palette':
        $red = new Color(255, 0, 0);
        $orange = new Color(255, 128, 0);
        $yellow = new Color(255, 255, 51);
        $green = new Color(0, 153, 0);
        $blue = new Color(0, 0, 255);
        $purple = new Color(102, 0, 153);

        $white = new Color(255, 255, 255);
        $black = new Color(0, 0, 0);
        //$red = new Color(255, 0, 0);
        $vertColors = array($white, $black, $white);

        $colors = array($red, $orange, $yellow, $green, $blue, $purple, $red);
        break;
        // let the program chose random colors
        case 'random':
        
        if (isset($_GET['randomseed'])) {
            if ($_GET['randomseed'] == 'random')
                srand(time());
            else srand($_GET['randomseed']);
        }
        else
            srand(time() * ($numColorsX + $numColorsY));
        
        for ($i=0; $i < $numColorsX; $i++)
            $colors[] = new Color(rand(0, 255), rand(0, 255), rand(0, 255));
        $rcolors = array_reverse($colors);
        $i = 0;
        foreach($rcolors as $c) {
            //if ($i)
                array_push($colors, $c);
            $i++;
        }
        
        for ($i=0; $i < $numColorsY; $i++)
            $vertColors[] = new Color(rand(0, 255), rand(0, 255), rand(0, 255));
        $rcolors = array_reverse($vertColors);
        $i = 0;
        foreach($rcolors as $c) {
            //if ($i)
                array_push($vertColors, $c);
            $i++;
        }
        break;
        // user specifys a starting color
        case 'colorfade':
        
        if (isset($_GET['startcolor'])) {
          $startColorRGB = explode(',', $_GET['startcolor']);
            //echo $_GET['startcolor'];
            //print_r($startColorRGB);
            $startColor = new Color($startColorRGB[0], $startColorRGB[1], $startColorRGB[2]);
            $endColor = new Color($startColorRGB[0], $startColorRGB[1], $startColorRGB[2]);

                if (isset($_GET['alpha']))
                    $endColor->changeBrightness($_GET['alpha']);
            else
                    $endColor->changeBrightness(0.8);
            
            $colors = array($startColor, $endColor, $startColor);
            $vertColors = $colors;
          
          }
        
        break;
    }

    //print_r($colors);

    $gradient = new Gradient($colors);
    if (isset($vertColors)) $gradient->setVertColors($vertColors);
    if (isset($_GET['width']))
        $width = $_GET['width'];
    if (isset($_GET['height']))
        $height = $_GET['height'];

    if (isset($width) && isset($height))
    $gradient->setSize($width, $height);
    
    if (isset($_GET['generate']))
        $saveAs = $_GET['randomseed'] . '.jpg';
    else $saveAs = null;
    
    if ($_SERVER['PHP_SELF'] == '/gradient2.php')
    $gradient->draw($saveAs);
}

?>