<?php
function dots ($im$mymood,$p_x,$p_y,$__DEBUG) {
  
$moods = array();
  
$foo "            0          | 1 | 2 | 3 | 4 | 5 | 6 | 7        | 8  | 9  | 10     | 11    | 12     | 13   |";
  
$foo "              |  Initial  |  Limits   | max dist | max abs | draw loop reps | Switches      |";
  
$foo "            Name           | r | g | b | r | g | b | over rbg | dx | dy | lines  | dots  | xy rst | dots |";
  
$moods[0]  = array( "Depressed"  ,175,175,175252525,  10      2  2  ,  20    20    1      1    );
  
$moods[1]  = array( "Angry"       ,255,  0,135,25520,255255      3  3  ,  200   250   1      1    );
  
$moods[2]  = array( "Tripping"   ,255,255,255,255,255,255255      3  3  ,  100   100   1      1    );
  
$moods[3]  = array( "Red Bd"     ,200,  0,135,255,  0,255255      3  3  ,  100   200   0      0    );
  
$moods[4]  = array( "Blue Pretty",  080,190104055255      3  3  ,  100   200   0      0    );
  
$moods[5]  = array( "Small Green",  0,255,  0105010255      3  3  ,   30   ,  50   1      1    );
  
$moods[6]  = array( "Cyan flood" ,  0,255,255105050255      3  3  ,  100   200   0      0    );
  
$moods[7]  = array( "Trippy mini",255,255,255,255,255,255255      3  3  ,   50   ,  50   1      1    );
  
$moods[8]  = array( "Green flood",  0,255,  0,  055,  0255      3  3  ,  100   200   0      0    );
  
$moods[9]  = array( "Red small"  ,200,  0,135,255,  0,255255      3  3  ,  150   ,  50   1      1    );
  
$moods[10] = array( "Black flood",  0,  0,  0,  0,  0,  0,   0      2  2  ,  300   ,  50   1      0    );
  
$programs = array();
  
$programs[0] = array(0);
  
$programs[1] = array(1);
  
$programs[2] = array(2);
  
$programs[3] = array(3);
  
$programs[4] = array(4);
  
$programs[5] = array(3,4);
  
$programs[6] = array(3,4,5);
  
$programs[7] = array(0);
  
$programs[8] = array(6);
  
$programs[9] = array(6,3,3,2);
  
$programs[10] = array(7);
  
$programs[11] = array(8,9);
  
$programs[12] = array(8,6,10,10,0);
  
$current_mood $programs[$mymood];
  foreach (
$current_mood as $mood_this) {
    
$mood_draw $moods[$mood_this];
    
//Initial Deltas on colours
    
$d_col_r rand(-2,2);
    
$d_col_g rand(-2,2);
    
$d_col_b rand(-2,2);
    if (
$__DEBUG == 1) { echo $d_col_r." ".$d_col_g." ".$d_col_b."<br>"; }
    
// Initial colour
    
$col_r $mood_draw[1];
    
$col_g $mood_draw[2];
    
$col_b $mood_draw[3];
    if (
$__DEBUG == 1) { echo $col_r."|".$col_g."|".$col_b."<br>"; }

    
//Loop lines
    
$x $p_x/2;
    
$y $p_y/2;
    if (
$__DEBUG == 1) { echo "$p_x/$p_y/$x/$y<br>"; }
    for (
$loop_j=0;$loop_j<=$mood_draw[10];$loop_j++) {
      if (
$mood_draw[13]) { imagesetpixel($im,$x,$y,0xFFFFFF); }
      if (
$mood_draw[12]) { $x $p_x/2$y $p_y/2; }
      
// Inc, leave, dec, each of the colour deltas
      
$j rand(1,3);
      switch (
$j) {
        case 
1$d_col_r -= rand(0,2); break;
        case 
2: break;
        case 
3$d_col_r += rand(0,2); break;
      }
      
$j rand(1,3);
      switch (
$j) {
        case 
1$d_col_g -= rand(0,2); break;
        case 
2: break;
        case 
3$d_col_g += rand(0,2); break;
      }
      
$j rand(1,3);
      switch (
$j) {
        case 
1$d_col_b -= rand(0,2); break;
        case 
2: break;
        case 
3$d_col_b += rand(0,2); break;
      }
      
// Cap the deltas to a sane level
      
if ($d_col_r 3) { $d_col_r 3; }
      if (
$d_col_r < -3) { $d_col_r = -3; }
      if (
$d_col_g 3) { $d_col_g 3; }
      if (
$d_col_g < -3) { $d_col_g = -3; }
      if (
$d_col_b 3) { $d_col_b 3; }
      if (
$d_col_b < -3) { $d_col_b = -3; }
      
// Add colour deltas to colour values
      
$col_r += $d_col_r;
      
$col_g += $d_col_g;
      
$col_b += $d_col_b;
      
// check the colours are sane
      
if ($col_r 255) { $col_r 255; }
      if (
$col_r 0) { $col_r 0; }
      if (
$col_g 255) { $col_g 255; }
      if (
$col_g 0) { $col_g 0; }
      if (
$col_b 255) { $col_b 255; }
      if (
$col_b 0) { $col_b 0; }
      
// Check the colours are within the limits from the original
      
if (abs($col_r-$mood_draw[1]) > $mood_draw[4]) 
    { 
$col_r = ($col_r $mood_draw[1] ? $mood_draw[1]-$mood_draw[4] : $mood_draw[1]+$mood_draw[4]); }
      if (
abs($col_g-$mood_draw[2]) > $mood_draw[5])  
        { 
$col_g = ($col_g $mood_draw[2] ? $mood_draw[2]-$mood_draw[5] : $mood_draw[2]+$mood_draw[5]); }
      if (
abs($col_b-$mood_draw[3]) > $mood_draw[6]) 
        { 
$col_b = ($col_b $mood_draw[3] ? $mood_draw[3]-$mood_draw[6] : $mood_draw[3]+$mood_draw[6]); }
      
// Check min/max distance
      
$min $max $col_r;
      if (
$min $col_b) { $min $col_b; } elseif ($max $col_b) { $max $col_b; }
      if (
$min $col_g) { $min $col_g; } elseif ($max $col_g) { $max $col_g; }
      if (
$max $min $mood_draw[7]) {
                     if (
$max == $col_r) { $col_r $min $mood_draw[7]; }
                      if (
$max == $col_g) { $col_g $min $mood_draw[7]; }
                    if (
$max == $col_b) { $col_b $min $mood_draw[7]; }
      }
      if (
$__DEBUG == 1) { echo $col_r.".".$col_g.".".$col_b."<br>"; }
      
// Random init of xy deltas
      
$dx rand(-2,2);
      
$dy rand(-2,2);
      
// Loop for dots
      
for ($loop_i=0;$loop_i<=$mood_draw[11];$loop_i++) {
        if (
$__DEBUG == 1) { echo ":"; }
        
// Alter xy deltas
        
$j rand(1,3);
        switch (
$j) {
          case 
1$dx -= rand(0,2); break;
          case 
2: break;
          case 
3$dx += rand(0,2); break;
        }
        
$j rand(1,3);
        switch (
$j) {
          case 
1$dy -= rand(0,2); break;
          case 
2: break;
          case 
3$dy += rand(0,2); break;
        }
        
// limit xy deltas
        
if ($dx $mood_draw[8]) { $dx $mood_draw[8]; }
        if (
$dx < -$mood_draw[8]) { $dx = -$mood_draw[8]; }
        if (
$dy $mood_draw[9]) { $dy $mood_draw[9]; }
        if (
$dy < -$mood_draw[9]) { $dy = -$mood_draw[9]; }
        
// Apply Deltas
        
$x += $dx;
        
$y += $dy;
        
// Apply Image size limits
        
if ($x $p_x) { $x=$p_x; } elseif ($x 0) { $x=0; }
        if (
$y $p_y) { $y=$p_y; } elseif ($y 0) { $y=0; }
        
imagesetpixel($im,$x,$y,(((($col_r*256)+$col_g)*256)+$col_b));
      }
      if (
$__DEBUG == 1) { echo "<br>"; }
    }
  }
  if (
$mood_draw[13]) { imagesetpixel($im,$p_x/2,$p_y/2,0xFFFFFF); }
  return 
$im;
}

function 
create_new ($im$image_x,$image_y,$__DEBUG) {
  
$im = @imagecreatetruecolor($image_x$image_y)
      or die(
"Cannot Initialize new GD image stream");
  return 
$im;
}

function 
imageflood ($im$col$image_x$image_y,$__DEBUG) {
  switch (
$col) {
    case 
0$cola ImageColorAllocate$im0,0,0);
            
imagefill($im,0,0,$cola);
            return 
$im;
  }
}

function 
execute_prog ($im$prog$image_x$image_y,$__DEBUG) {
  
//program entry is a 2 value array ... first is command, second is sub program
  // Command 0  : background : black (0)
  // Command 1  : dots       : See the begining of dots
  // Command 2  : create new : Creates new image of size param*50
  // Command 3  : Recurse    : Call this function with param as $prog
  
$programs = array(
    
=> array( array(2,8), array(0,0), array(1,0) ), // Legecy Dot() on black
    
=> array( array(2,8), array(0,0), array(1,1) ), // Legecy Dot() on black
    
=> array( array(2,8), array(0,0), array(1,2) ), // Legecy Dot() on black
    
=> array( array(2,8), array(0,0), array(1,3) ), // Legecy Dot() on black
    
=> array( array(2,8), array(0,0), array(1,4) ), // Legecy Dot() on black
    
=> array( array(2,8), array(0,0), array(1,5) ), // Legecy Dot() on black
    
=> array( array(2,8), array(0,0), array(1,6) ), // Legecy Dot() on black
    
=> array( array(2,8), array(0,0), array(1,7) ), // Legecy Dot() on black
    
=> array( array(2,4), array(0,0), array(1,6) ), // Smaller version of 6
    
=> array( array(2,8), array(0,0), array(1,8) ), // Fractics cyan
   
10 => array( array(2,8), array(0,0), array(1,9) ), // Cyan/red/gray type thing
   
11 => array( array(2,4), array(0,0), array(1,9) ),
   
12 => array( array(2,3), array(0,0), array(1,12) ),
   
13 => array( array(2,4), array(0,0), array(1,12) )
  );

  foreach (
$programs[$prog] as $prog_line) {
    
$__PA $prog_line[1];
    switch (
$prog_line[0]) {
      case 
0:  $im imageflood   ($im$__PA,$image_x,$image_y,$__DEBUG); break;
      case 
1:  $im dots         ($im$__PA,$image_x,$image_y,$__DEBUG); break;
      case 
2:  $im create_new   ($im$image_x=$__PA*50$image_y=$__PA*50$__DEBUG); break;
      case 
3:  $im execute_prog ($im$__PA,$image_x,$image_y,$__DEBUG); break;
    }
    
#$text_color = imagecolorallocate($im, 233, 14, 91);
    #imagestring($im, 10, 5, 5,  "Revenge of the line :P", $text_color);
  
}
  return 
$im;
}

$__DEBUG $_GET["debug"]?$_GET["debug"]:0;
$image_x 400;
$image_y 400;
$mymood 13;
$im execute_prog('',$_GET['mood']?$_GET['mood']:$mymood$image_x$image_y,$__DEBUG);
if (
$__DEBUG == 0) { header ("Content-type: image/jpeg"); }
if (
$__DEBUG == 0) { imagejpeg($im,'',80);}
imagedestroy($im);
?>