<?php

header ("Content-type: image/svg+xml");

// ---------------------------------------------------------------------
//
// Mark Brautigam
// 18 Feb. 2015
//
// use like this:
// <img alt='A' src='letters.php?n=A&c=f80' />
//
// To do:
// 4. Add numbers
// 5. Do the scale conversions in PHP instead of in SVG
// 6. Convert some bezier curves to arcs?
// 7. Add real lower case letters?
// 8. Print a whole message of several letters in one block
// 9. Convert the whole thing to Javasript

// Notes:
// 2. You must specify the background color using 3-digit hex without the #
//    a. use c=xyz where xyz is the hex color
//    b. E.g. f00 = red, 0f0 = green, 00f = blue
//    c. The program will add # automatically
//    d. The program will calculate a suitable contrasting color for the
//       letters, either black or white
//
// DONE:
// 1. Convert lower case to upper case letters for now until we have a
//    real set of lower case letters
// 3. Let them specify color in 6-digit hex
//
// ---------------------------------------------------------------------

$letter = /*strtoupper*/ ($_GET['n']);
$color  = '#' . $_GET['c'];
if (strlen ($color) > 7) $color = substr ($color, 0, 7);
$textColor = contrastingColor ($color);

function contrastingColor ($c)
{
  if (strlen ($c) != 7) {
    $r = hexdec ($c[1]) / 16;
    $g = hexdec ($c[2]) / 16;
    $b = hexdec ($c[3]) / 16;
  }
  else {
    $r = (hexdec ($c[1]) * 16 + hexdec($c[2]) ) / 256;;
    $g = (hexdec ($c[3]) * 16 + hexdec($c[4]) ) / 256;;
    $b = (hexdec ($c[5]) * 16 + hexdec($c[6]) ) / 256;;
  }
  $luma = 0.30  * $r + 0.59 * $g + 0.11 * $b;
  if ($luma > 0.5)
    return ("#000");
  else
    return ("#fff");
  /*
  $max = max ($r, $g, $b);
  $min = min ($r, $g, $b);
  $r0 = $r1 = $r; $g0 = $g1 = $g; $b0 = $b1 = $b;
  if ($r0 == $max) $r0 = 15;
  if ($g0 == $max) $g0 = 15;
  if ($b0 == $max) $b0 = 15;
  if ($r1 == $min) $r1 = 0;
  if ($g1 == $min) $g1 = 0;
  if ($b1 == $min) $b1 = 0;
  if ($luma > 0.5)
    return sprintf ("#%x%x%x", $r0, $g0, $b0);
  else
    return sprintf ("#%x%x%x", $r1, $g1, $b1);
  */
}

$letters = array  (
  "A" => array ( "PL", "0 1000 500 0 1000 1000", "PL", "250 500 750 500" ),
  "B" => array ( "PL", "0 0 0 1000", "P", "M0 0 C500 0 1000 0 1000 250 C1000 500 500 500 0 500",
                                     "P", "M0 500 C500 500 1000 500 1000 750 C1000 1000 500 1000 0 1000" ),
  "C" => array ( "P", "M1000 100 A750 500 0 1 0 1000 900" ),
  "D" => array ( "PL", "0 0 0 1000", "P", "M0 0 C500 0 1000 0 1000 500 C1000 1000 500 1000 0 1000" ),
  "E" => array ( "PL", "1000 0 0 0 0 1000 1000 1000", "PL", "0 500 600 500" ),
  "F" => array ( "PL", "1000 0 0 0 0 1000", "PL", "0 500 600 500" ),
  "G" => array ( "P", "M1000 100 A750 500 0 1 0 1000 900 L1000 600 L600 600" ),
  "H" => array ( "PL", "0 0 0 1000", "PL", "1000 0 1000 1000", "PL", "0 500 1000 500" ),
  "I" => array ( "PL", "200 0 800 0", "PL", "200 1000 800 1000", "PL", "500 0 500 1000" ),
  "J" => array ("PL", "700 0 1000 0 1000 600", "P", "M0 600 C0 1100 1000 1100 1000 600" ),
  "K" => array ( "PL", "0 0 0 1000", "PL", "1000 0 0 500 1000 1000" ),
  "L" => array ( "PL", "0 0 0 1000 1000 1000" ),
  "M" => array ( "PL", "0 1000 0 0 500 1000 1000 0 1000 1000" ),
  "N" => array ( "PL", "0 1000 0 0 1000 1000 1000 0" ),
  "O" => array ( "C", 500, 500, 500 ),
  "P" => array ( "PL", "0 0 0 1000", "P", "M0 0 C500 0 1000 0 1000 250 C1000 500 500 500 0 500" ),
  "Q" => array ( "C", 500, 500, 500, "PL", "700 700 1000 1000" ),
  "R" => array ( "PL", "0 0 0 1000", "P", "M0 0 C500 0 1000 0 1000 250 C1000 500 500 500 0 500", "PL", "0 500 1000 1000" ),
  "S" => array ( "P", "M1000 250 C1000 125 750 0 500 0 C250 0 0 125 0 250 C0 375 250 500 500 500 C750 500 1000 625 1000 750 C1000 875 750 1000 500 1000 C250 1000 0 875 0 750" ),
  "T" => array ( "PL", "0 0 1000 0", "PL", "500 0 500 1000" ),
  "U" => array ("PL", "0 0 0 600", "PL", "1000 0 1000 600", "P", "M0 600 C0 1100 1000 1100 1000 600" ),
  "V" => array ( "PL", "0 0 500 1000 1000 0" ),
  "W" => array ( "PL", "0 0 250 1000 500 0 750 1000 1000 0" ),
  "X" => array ( "PL", "0 0 1000 1000", "PL", "0 1000 1000 0" ),
  "Y" => array ( "PL", "0 0 500 500 1000 0", "PL", "500 500 500 1000" ),
  "Z" => array ( "PL", "0 0 1000 0 0 1000 1000 1000" ),
  "_" => array ( ),

  // NUMBERS
  "1" => array ( "PL", "200 75 500 0 500 1000" ),
  "4" => array ( "PL", "750 1000 750 0 0 650 1000 650" ),
  "7" => array ( "PL", "0 0 1000 0 250 1000 " ),
  "0" => array ( "C", 500, 500, 500, "PL", "830 170 170 830" ),
  "3" => array ( "P", "M0 250 A500 225 0 1 1 500 450 A500 275 0 1 1 0 750" ),
  "2" => array ( "P", "M0 250 C0 -100 1000 -100 1000 250 C1000 400 550 475 500 500 C 400 525 0 700 0 1000 L1000 1000" ),
  "8" => array ( "E", 500, 225, 500, 225, "E", 500, 725, 500, 275 ),
  "5" => array ( "P", "M1000 0 L0 0 L 0 400" ,
                      "P", "M0 400 C500 400 1000 400 1000 700 C1000 1000 500 1000 0 1000" ),
  "6" => array ( "P", "M900 0 C300 0 0 100 0 500 L0 700", "E", 500, 700, 500, 300 ),
  "9" => array ( "P", "M100 1000 C700 1000 1000 900 1000 500 L1000 300", "E", 500, 300, 500, 300 ),

  // LOWER CASE
  "b" => array ( "E", 500, 700, 500, 300, "PL", "0 0 0 1000" ),
  "d" => array ( "E", 500, 700, 500, 300, "PL", "1000 0 1000 1000" ),
  "p" => array ( "E", 500, 550, 500, 300, "PL", "0 250 0 1250" ),
  "q" => array ( "E", 500, 550, 500, 300, "PL", "1000 250 1000 1250" ),
  "k" => array ( "PL", "0 0 0 1000", "PL", "1000 400 0 700 1000 1000" ),
  "l" => array ( "PL", "200 0 500 0 500 1000 200 1000 800 1000" ),
  "v" => array ( "PL", "100 400 500 1000 900 400" ),
  "w" => array ( "PL", "0 400 250 1000 500 400 750 1000 1000 400" ),
  "x" => array ( "PL", "100 400 900 1000", "PL", "100 1000 900 400" ),
  "y" => array ( "PL", "0 400 500 1000", "PL", "1000 400 0 1600" ),
  "z" => array ( "PL", "100 400 900 400 100 1000 900 1000" ),
  "i" => array ( "PL", "200 400 500 400 500 1000 200 1000 800 1000", "PL", "450 225 450 226" ),
  "o" => array ( "E", 500, 700, 500, 300 ),
  "c" => array ( "P", "M1000 500 A600 300 0 1 0 1000 900" ),
  "e" => array ( "P", "M1000 500 A600 300 0 1 0 1000 900", "PL", "1000 500 110 900" ),
  "j" => array ( "PL", "500 400 500 900", "PL", "500 225 500 226", "P", "M500 900 C500 1100 250 1200 0 1200" ),
  "n" => array ( "PL", "100 400 100 1000", "P", "M100 1000 L100 600 C100 320 900 320 900 600 L900 1000" ),
  "h" => array ( "PL", "100 0 100 1000", "P", "M100 1000 L100 600 C100 320 900 320 900 600 L900 1000" ),
  "m" => array ( "PL", "0 400 0 1000", "P", "M0 1000 L0 600 C0 320 500 320 500 600 L500 1000",
                                       "P", "M500 1000 L500 600 C500 320 1000 320 1000 600 L1000 1000" ),
  "g" => array ( "E", 500, 550, 500, 300, "PL", "1000 250 1000 900",
                                          "P", "M1000 900 C1000 1200 500 1200 0 1200" ),
  "r" => array ( "PL", "100 400 100 1000", "P", "M100 1000 L100 600 C100 320 900 320 900 600 " ),
  "u" => array ( "PL", "900 400 900 1000", "P", "M100 400 L100 800 C100 1080 900 1080 900 800 L900 400" ),
  "t" => array ( "P", "M400 0 L400 750 C400 750 400 1000 900 1000", "PL", "0 400 900 400" ),
  "f" => array ( "P", "M400 1000 L400 250 C400 250 400 0 900 0", "PL", "0 500 900 500" ),
  "s" => array ( "P", "M900 550 C900 475 750 400 500 400 C250 400 100 475 100 550 C100 625 250 700 500 700 C750 700 900 775 900 850 C900 925 750 1000 500 1000 C250 1000 100 925 100 850" ),
  "a" => array ( "E", 500, 700, 500, 300, "PL", "1000 400 1000 1000"),

  // PUNCTUATION
  "-" => array ( "PL", "0 500 1000 500" ),
  "|" => array ( "PL", "500 0 500 1000 " ),
  "+" => array ( "PL", "0 500 1000 500",  "PL", "500 100 500 900 " ),
  "/" => array ( "PL", "1000 0 0 1000" ),
  "\\" => array ( "PL", "0 0 1000 1000" ),
  "*" => array ( "PL", "500 100 500 900", "PL", "0 300 1000 700", "PL", "1000 300 0 700" ),
  "." => array ( "PL", "500 999 500 1001" ),
  "," => array ( "PL", "520 950 480 1050" ),
  ";" => array ( "PL", "520 699 520 701", "PL", "520 950 480 1050" ),
  ":" => array ( "PL", "500 699 500 701",  "PL", "500 999 500 1001" ),
  "(" => array ( "P", "M550 0 C100 300 100 700 550 1000" ),
  ")" => array ( "P", "M450 0 C900 300 900 700 450 1000" ),
  "'" => array ( "PL", "500 0 480 50" ),
  "\"" => array ( "PL", "600 0 580 50", "PL", "400 0 380 50" ),
  "?" => array ( "P", "M0 250 C0 -100 1000 -100 1000 250 C1000 400 550 475 500 700 ", "PL", "500 999 500 1001" ),
  "!" => array ( "PL", "500 0 500 700 ", "PL", "500 999 500 1001" ),
  "^" => array ( "PL", "100 400 500 0 900 400" ),
  "=" => array ( "PL", "0 300 1000 300",  "PL", "0 700 1000 700" ),
  "<" => array ( "PL", "900 0 100 500 900 1000" ),
  ">" => array ( "PL", "100 0 900 500 100 1000" ),
  "%" => array ( "C", 200, 200, 200, "PL", "1000 0 0 1000", "C", 800, 800, 200 ),
  "$" => array ( "P", "M1000 250 C1000 125 750 0 500 0 C250 0 0 125 0 250 C0 375 250 500 500 500 C750 500 1000 625 1000 750 C1000 875 750 1000 500 1000 C250 1000 0 875 0 750", "PL", "500 -100 500 1100" ),
  "`" => array ( "PL", "480 0 500 50" ),
  "~" => array ( "P", "M0 600 C500 000 500 1000 1000 400" ),

  /* OLD STUFF
  // "C" => array ("X", "<path d='M500 0 C250 0 0 250 0 500 C0 750 250 1000 500 1000 C 750 1000 1000 750 1000 500' transform='rotate(45 500 500)' />"),
  // "G" => array ("X", "<path d='M500 0 C250 0 0 250 0 500 C0 750 250 1000 500 1000 C 750 1000 1000 750 1000 500' transform='rotate(45 500 500)' />", "PL", "853 1000 853 600 700 600" ),
  // "6" => array ( "P", "M1000 100 C800 0 600 0 500 0 C100 0 0 250 0 500 L0 700", "E", 500, 700, 500, 300 ),
  // "9" => array ( "P", "M0 900 C200 1000 400 1000 500 1000 C900 1000 1000 750 1000 500 L1000 300", "E", 500, 300, 500, 300 ),
  // "G" => array ("X", "<path d='M500 0 C250 0 0 250 0 500 C0 750 250 1000 500 1000 C 750 1000 1000 750 1000 500 L750 250 L500 500' transform='rotate(45 500 500)' />" ),
  */
);

?>
<svg height="1000" width="1000" xmlns="http://www.w3.org/2000/svg" viewbox='0 0 1000 1000'>

<rect x='0' y='0' width='1000' height='1000' fill='<?php echo $color; ?>' />

<g stroke='<?php echo $textColor?>' stroke-width='100' stroke-linejoin='round' stroke-linecap='round' fill='none' 
  transform='scale(.4, .6) translate(750, 333)' >
<?php
  $myLetter = $letters[$letter];
  for ($i=0; $i<count($myLetter); $i++) {
    if ($myLetter[$i] == "PL")
      echo "  <polyline points='{$myLetter[$i+1]}' />\n";
    else if ($myLetter[$i] == "C")
      echo "  <circle cx='{$myLetter[$i+1]}' cy='{$myLetter[$i+2]}' r='{$myLetter[$i+3]}' />\n";
    else if ($myLetter[$i] == "E")
      echo "  <ellipse cx='{$myLetter[$i+1]}' cy='{$myLetter[$i+2]}' rx='{$myLetter[$i+3]}' ry='{$myLetter[$i+4]}' />\n";
    else if ($myLetter[$i] == "P")
      echo "  <path d='{$myLetter[$i+1]}' />\n";
    else if ($myLetter[$i] == "X")
      echo "  " . $myLetter[$i+1] . "\n";
  }
?>
</g>

</svg>