<?php
// output PNG
header("Content-type: image/png");

$size = 256; //must be divisible by 4
$dimension = $size + 1; //image dimensions
$addition = 256; //addition dimensions for sl square
$filler = 40; // space between wheel and square
$dim1 = $dimension - 1;
$halfsize = ($size)/2;
$quartersize = ($size)/4;

$image=imagecreatetruecolor($dimension+$addition+$filler,$dimension);
imageAlphaBlending($image,FALSE);
imagesavealpha($image,TRUE);

// fill background for no reason
$white=imageColorAllocate($image,255,255,255);
imageFilledRectangle($image,0,0,$dim1+$addition+$filler,$dim1,$white);
$black=imageColorAllocate($image,0,0,0);

// function adapted from the DHTML Color Calculator
function hsb2rgb ($h, $s, $b) {
    
$max = round($b*51/20);
    
$min = round($max*(1 - $s/100));
    if (
$min == $max) return array($max, $max, $max);
    
$d = $max - $min;
    
$h6 = $h/60;
    if (
$h6 <= 1) return array($max, round($min + $h6*$d), $min);
    if (
$h6 <= 2) return array(round($min - ($h6 - 2)*$d), $max, $min);
    if (
$h6 <= 3) return array($min, $max, round($min + ($h6 - 2)*$d));
    if (
$h6 <= 4) return array($min, round($min - ($h6 - 4)*$d), $max);
    if (
$h6 <= 5) return array(round($min + ($h6 - 4)*$d), $min, $max);
    return array(
$max, $min, round($min - ($h6 - 6)*$d));
}

//make the wheel

for ($k=0; $k<=$size; $k++) {
  for (
$j=0; $j<=$size; $j++) {
    
$x = $j - $halfsize;
    
$y = $halfsize - $k;
    
$x2 = $x * $x;
    
$y2 = $y * $y;
    
$xs = ($x < 0)?-1:1;
    
$ys = ($y < 0)?-1:1;
    
$xn = $x/$halfsize; //normalize x
    
$rr = sqrt($x2 + $y2); //raw radius
    
$rn = $rr/$halfsize; //normalized radius
    
$r = $rn;
    
$ar = acos($x/$rr); //angle in radians
    
$arc = ($y>=0)?$ar:pi()-$ar+pi();  //correct below axis
    
$ad = rad2deg($arc);  //convert to degrees
    
$a = $ad;
    if (
$x == 0 & $y == 0) {$rgb = array(0,0,0);} // same as $r == 0
    
else if ($rn > 1) {$rgb = array(255,255,255);} // outside circle
        
else if ($rn >= .5) {
          
$rs = ($rn - .5) *2;
          
$rgb = hsb2rgb($a,100-$rs*100,100);
          }
              else {
                 
$rb = $rn * 2;
                 
$rgb = hsb2rgb($a,100,$rb*100);}
    
$color=imageColorAllocate($image,$rgb[0],$rgb[1],$rgb[2]);
    
imageSetPixel($image,$j,$k,$color);
  }
}

// make the transparent SV square:  0=opaque 127=transparent

for ($k=0; $k<$addition; $k++) {
  for (
$j=0; $j<=$addition; $j++) {
  
$x = $j + $dimension + $filler;
  
$y = $k;
  
$grey = 255 - $k; //ranges from white at the top to black at the bottom
  
$transy = 127 - floor($k/2);
    
//ranges from trans at top to opaque at bottom
  
$transx = floor($j/2);
    
//ranges from opaque at the left to transparent at the right
  
$trans = floor($j/2 * $transy/127);
  
$color=imageColorAllocateAlpha($image,$grey,$grey,$grey,$trans);
  
imageSetPixel($image,$x,$y,$color);
  }
}

imagePNG($image);
imageDestroy($image);
?>