<?php

// you could also output jpg and gif
Header ("Content-type: image/png");

$im = imagecreate (288, 288);
// $white = ImageColorAllocate ($im, 255, 255, 255);
// $black = ImageColorAllocate ($im, 0, 0, 0);
// $gray = ImageColorAllocate ($im, 128, 128, 128);

$dir = isset ($_GET['dir']) ? $_GET['dir'] : "north";
$shape = isset ($_GET['shape']) ? $_GET['shape'] : "circle";
$color = isset ($_GET['color']) ? $_GET['color'] : "c00";

$red   = hexdec ($color[0] . $color[0]);
$green = hexdec ($color[1] . $color[1]);
$blue  = hexdec ($color[2] . $color[2]);
/*
$myColor = ImageColorAllocate ($im, sprintf ("0x%s%s", $color[0], $color[0]), 
                                    sprintf ("0x%s%s", $color[1], $color[1]), 
                                    sprintf ("0x%s%s", $color[2], $color[2]));
*/
$myColor = imagecolorallocate ($im, $red, $green, $blue);

function getContrastingColor ($c)
{
  $r = hexdec ($c[0]) / 16;
  $g = hexdec ($c[1]) / 16;
  $b = hexdec ($c[2]) / 16;
  $luma = 0.30  * $r + 0.59 * $g + 0.11 * $b;
  if ($luma > 0.5)
    return ("066"); // return ("333");
  else
    return ("dff"); // return ("ccc");
}

$contrastColor = getContrastingColor ($color);

$cred   = hexdec ($contrastColor[0] . $contrastColor[0]);
$cgreen = hexdec ($contrastColor[1] . $contrastColor[1]);
$cblue  = hexdec ($contrastColor[2] . $contrastColor[2]);

/*
$fgColor = ImageColorAllocate ($im, sprintf ("0x%s%s", $contrastColor[0], $contrastColor[0]), 
                                    sprintf ("0x%s%s", $contrastColor[1], $contrastColor[1]), 
                                    sprintf ("0x%s%s", $contrastColor[2], $contrastColor[2]));
*/
$fgColor = ImageColorAllocate ($im, $cred, $cgreen, $cblue);

// directions: n=u=t, s=d=b, e=4, w=l, nw, ne, se, sw
// shapes: circle, disc, triangle, square, diamond, arrow
// color: must be #xyz without the #

// draw the outside
ImageFilledRectangle ($im, 0, 0, 288, 288, $myColor);

// draw the inside figure
if ($shape == "disc") {
  // no direction
  imagefilledellipse ($im, 144, 144, 168, 168, $fgColor);
}

else if ($shape == "circle") {
  // no direction
  imagefilledellipse ($im, 144, 144, 192, 192, $fgColor);
  imagefilledellipse ($im, 144, 144, 144, 144, $myColor);
}

else if ($shape == "square") {
  // no direction
  imagefilledrectangle ($im, 72, 72, 216, 216, $fgColor);
}

else if ($shape == "diamond") {
  // no direction
  imagefilledpolygon ($im, array (144, 48, 240, 144, 144, 240, 48, 144), 4, $fgColor);
}

else if ($shape == "triangle") {
  switch ($dir) {
    case "north" : 
      imagefilledpolygon ($im, array (72, 216, 216, 216, 144, 72), 3, $fgColor);
      break;
    case "south" : 
      imagefilledpolygon ($im, array (72, 72, 216, 72, 144, 216), 3, $fgColor);
      break;
    case "east" : 
      imagefilledpolygon ($im, array (72, 72, 72, 216, 216, 144), 3, $fgColor);
      break;
    case "west" : 
      imagefilledpolygon ($im, array (216, 72, 216, 216, 72, 144), 3, $fgColor);
      break;
    case "nw" : 
      imagefilledpolygon ($im, array (72, 72, 128, 216, 216, 128), 3, $fgColor);
      break;
    case "ne" : 
      imagefilledpolygon ($im, array (216, 72, 160, 216, 72, 128), 3, $fgColor);
      break;
    case "se" : 
      imagefilledpolygon ($im, array (216, 216, 160, 72, 72, 160), 3, $fgColor);
      break;
    case "sw" : 
      imagefilledpolygon ($im, array (72, 216, 128, 72, 216, 160), 3, $fgColor);
      break;
  }
}
else if ($shape == "arrow") {
  switch ($dir) {
    case "north" : 
      imagefilledpolygon ($im, array (72, 144, 216, 144, 144, 64), 3, $fgColor);
      imagefilledrectangle ($im, 108, 144, 180, 216, $fgColor);
      break;
    case "south" : 
      imagefilledpolygon ($im, array (72, 144, 216, 144, 144, 224), 3, $fgColor);
      imagefilledrectangle ($im, 108, 144, 180, 72, $fgColor);
      break;
    case "east" : 
      imagefilledpolygon ($im, array (144, 72, 144, 216, 224, 144), 3, $fgColor);
      imagefilledrectangle ($im, 144, 108, 72, 180, $fgColor);
      break;
    case "west" : 
      imagefilledpolygon ($im, array (144, 72, 144, 216, 64, 144), 3, $fgColor);
      imagefilledrectangle ($im, 144, 108, 216, 180, $fgColor);
      break;
    case "nw" : 
      imagefilledpolygon ($im, array (72, 72, 72, 216, 216, 72), 3, $fgColor);
      imagefilledpolygon ($im, array (120, 168, 168, 120, 216, 168, 168, 216), 4, $fgColor);
      break;
    case "ne" : 
      imagefilledpolygon ($im, array (216, 72, 216, 216, 72, 72), 3, $fgColor);
      imagefilledpolygon ($im, array (168, 168, 120, 120, 72, 168, 120, 216), 4, $fgColor);
      break;
    case "se" : 
      imagefilledpolygon ($im, array (216, 216, 216, 72, 72, 216), 3, $fgColor);
      imagefilledpolygon ($im, array (168, 120, 120, 168, 72, 120, 120, 72), 4, $fgColor);
      break;
    case "sw" : 
      imagefilledpolygon ($im, array (72, 216, 72, 72, 216, 216), 3, $fgColor);
      imagefilledpolygon ($im, array (120, 120, 168, 168, 216, 120, 168, 72), 4, $fgColor);
      break;
  }
}

// you could also output jpg with imagejpeg() and gif with imagegif()
imagepng ($im);
ImageDestroy ($im);

?>