<?php
class pieChart { 
/* {{{ attributes */ 

  var $im; 
  var $data; 
  var $colors; 
  var $center_x; 
  var $center_y; 
  var $diameter; 

   /* sum of values */ 
  var $sum; 
/* }}} */ 
/* {{{ constants */ 

  var $PI = 3.1415926535897931;

/* }}} */ 
/* {{{ roundoff */ 

   /*  
  ** PHP has no function for rounding off doubles to the nearest 
  ** integer so we have to roll our own. 
  */ 
  function roundoff ($v) { 
    if ( $v - floor($v) >= 0.5) { 
      return(ceil($v)); 
    } else { 
      return(floor($v)); 
    } 
  } 

/* }}} */ 
/* {{{ deg2rad */ 

   /* 
  ** The built-in trig functions use radians and there's no 
  ** function in PHP to convert between degrees and radians 
  */ 

  function deg2rad ($degrees) { 
    return (($this->PI * $degrees) / doubleval(180)); 
  } 

/* }}} */ 
/* {{{ get_xy_factors */ 

   /* 
  ** Calculate the directional vector for the sides of the 
  ** piece of pie. 
  */ 

  function get_xy_factors ($degrees) { 
    $x = cos($this->deg2rad($degrees)); 
    $y = sin($this->deg2rad($degrees)); 
    return (array($x, $y)); 
  } 

/* }}} */ 
/* {{{ init */ 

   /* 
  ** Initialize the object and draw the pie.  This would be the 
  ** constructor in an ordinary OO scenario -- just that we haven't 
  ** got constructors in PHP, now do we? ;-) 
  */ 

  function init ($w, $h, $d,$colors) { 
    $this->im = ImageCreateFromGif('./pie.gif'); 

    foreach ($d as $v) {
        if (is_numeric($v)) {
            $this->data[] = floatval($v);
        }
    }
    if (empty($this->data)) {
        $this->data[] = 0; // backwards compatibility for 100% nonsense
    }
    $this->center_x = intval($w/2+1);
    $this->center_y = intval($h/2+1); 

    $this->diameter = 270; 
    $this->white = ImageColorAllocate($this->im, 255, 255, 255); 
        imagefill($this->im, 2, 2, $this->white); 
        imagefill($this->im, 270, 2, $this->white); 
        imagefill($this->im, 268, 268, $this->white); 
        imagefill($this->im, 2, 268, $this->white); 
    $this->black = ImageColorAllocate($this->im, 0, 0, 0); 
        imagecolortransparent($this->im,$this->white);
    $n = sizeof ($this->data); 
        $this->sum = 0;
    for ($i = 0; $i < $n; $i++) 
	 { 
      $this->colors[$i] = ImageColorAllocate($this->im, $colors[$i][0], $colors[$i][1], $colors[$i][2]); 
      $this->sum += $this->data[$i]; 
          
    } 

    $from = 0;
        $to = 0; 
                                 
        for($i=0;$i<$n && $this->sum != 0; $i++) 
		  { 
                if($this->data[$i] != 0)
					 {
                        $to = $from + $this->roundoff( ($this->data[$i]*360)/doubleval($this->sum) ); 
                        $col = $this->colors[$i]; 
                        if($i == $n-1)
									$to = 360;
/*                        if($to == 360 && $from == 0)
								{
                                $this->draw_slice($this->center_x, $this->center_y, 0, 160, $this->colors[$i]); 
                                $this->draw_slice($this->center_x, $this->center_y, 150, 360, $this->colors[$i]); 
                        }
                        else 
*/
									$this->draw_slice($this->center_x, $this->center_y, $from, $to, $this->colors[$i]); 
                        $from = $to;
                }
        } 
//      ImageArc($this->im, $this->center_x, $this->center_y, $this->diameter, $this->diameter, 1, 360, $this->black);
//      ImageArc($this->im, $this->center_x, $this->center_y, $this->diameter+2, $this->diameter+1, 1, 360, $this->black);
//      ImageArc($this->im, $this->center_x, $this->center_y, $this->diameter+3, $this->diameter+3, 1, 360, $this->white);
} 


/* }}} */ 
/* {{{ draw_slice */ 

   /* 
  ** This function draws a piece of pie centered at x,y starting at 
  ** "from" degrees and ending at "to" degrees using the specified color. 
  */ 
  function draw_slice ($x, $y, $from, $to, $color) { 

     # Awful Kludge!!! 

    if ($to > 360) { 
      $to=360; 
    } 

//    ImageArc($this->im, $this->center_x, $this->center_y, $this->diameter, $this->diameter, $from, $to, $this->black); 
                         
     /* First line */ 
        if($from==0 && $to != 360)
		  {
            $axy2 = $this->get_xy_factors($from); 
            $ax2 = $this->center_x + ($axy2[0] * ($this->diameter/2)); 
            $ay2 = $this->center_y + ($axy2[1] * ($this->diameter/2)); 
                ImageLine($this->im, $this->center_x, $this->center_y, $ax2, $ay2, $this->black); 
        }

     /* Second line */ 
	 if($to != 360)
	 {
	    $bxy2 = $this->get_xy_factors($to); 
	    $bx2 = $this->center_x + ($bxy2[0] * ($this->diameter/2)); 
	    $by2 = $this->center_y + ($bxy2[1] * ($this->diameter/2)); 
	    ImageLine($this->im, $this->center_x, $this->center_y, $bx2, $by2, $this->black); 
	}

     /* decide where to start filling, then fill */ 
    $xy2 = $this->get_xy_factors((($to-$from)/2)+$from); 
    $x2 = floor($this->center_x + ($xy2[0]*($this->diameter/3))); 
    $y2 = floor($this->center_y + ($xy2[1]*($this->diameter/3))); 
   imagefill($this->im, $x2, $y2, $color); 
  } 

  function display() { 
    Header( "Content-type: image/gif"); 
    ImageGif($this->im); 
  } 
 
}; 



function convertColor(&$color)
{
	$color = hexdec(str_replace('#','', $color));
	
	$b = $color & 0x0000FF;
	$g = ($color >> 8 ) & 0xFF;
	$r = ($color >> 16 ) & 0xFF;
	
	$color = array($r, $g, $b);
}


list($vals,$colors) = getInputs('vals','colors');

for($i = 0; $i< sizeOf($colors) ; $i++)
	convertColor( $colors[$i] );


$pie = new pieChart();
$pie->init(270,270,$vals,$colors); 
$pie->display();
