<?php header ("Content-type: image/svg+xml"); ?>
<svg height='1000' width='1000' viewBox='0 0 1000 1000' xmlns='http://www.w3.org/2000/svg' xmlns:xlink='http://www.w3.org/1999/xlink'>
<?php

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

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 ("333");
  else
    return ("ccc");
}

$contrastColor = getContrastingColor ($color);
// print_r ($color);
// print_r ($contrastColor);
// print_r (array ($dir, $shape, $color));

$degrees = array ( "n" => -90, "ne" => -45, "e" => 0,   "se" => 45, 
                   "s" => 90,  "sw" => 135, "w" => 180, "nw" => -135 );

if (array_key_exists ($dir, $degrees))
  $rotation = $degrees[$dir];
else if (is_numeric ($dir))
  $rotation = -$dir;
else
  $rotation = 90;

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

// draw the outside
echo "<rect x='0' y='0' width='1000' height='1000' fill='#$color' />\n";

// draw the inside figure
if ($shape == "disc") {
  echo "<circle cx='500' cy='500' r='250' fill='#$contrastColor' />\n";
}

else if ($shape == "circle") {
  echo "<circle cx='500' cy='500' r='275' fill='#$contrastColor' />\n";
  echo "<circle cx='500' cy='500' r='225' fill='#$color' />\n";
}

else if ($shape == "square") {
  echo "<rect x='250' y='250' width='500' height='500' fill='#$contrastColor' />\n";
}

else if ($shape == "diamond") {
  echo "<polygon points='500 150 850 500 500 850 150 500' fill='#$contrastColor' />\n";
}

else if ($shape == "triangle") {
  echo "<defs>\n";
  echo "  <polygon id='triangle' points='375 283 375 717 750 500' fill='#$contrastColor' />\n";
  echo "</defs>\n";

  echo "<use xlink:href='#triangle' transform='rotate($rotation 500 500)' />\n";
  // echo "<circle cx='500' cy='500' r='10' fill='#888' />\n";
}
else if ($shape == "arrow") {
  echo "<defs>\n";
  echo "  <polygon id='arrow' points='500 300 500 425 250 425 250 575 500 575 500 700 750 500' fill='#$contrastColor' />\n";
  echo "</defs>\n";

  echo "<use xlink:href='#arrow' transform='rotate($rotation 500 500)' />\n";
}
?>

</svg>