1. <?php
  2.  
  3. /**
  4.  * Intended to work with http://colorschemedesigner.com with Tetrad OR Accented Analogic schemes
  5.  * UPDATE: works with any type of scheme:
  6.  *     http://students.washington.edu/kuksenok/colors/ColorScheme.php?demo&scheme=three
  7.  *     http://students.washington.edu/kuksenok/colors/ColorScheme.php?demo&scheme=two
  8.  *
  9.  * Any scheme can be made "dark" rather than "light" by including the "dark" flag
  10.  * eg: http://students.washington.edu/kuksenok/colors/ColorScheme.php?demo
  11.  *     http://students.washington.edu/kuksenok/colors/ColorScheme.php?demo&scheme=billow
  12.  *     http://students.washington.edu/kuksenok/colors/ColorScheme.php?demo&scheme=billow&dark
  13.  */
  14. class ColorScheme {
  15.  
  16.     private $colors = array();
  17.     private $inverse = false;
  18.  
  19.     public function __construct($txtdef, $dark=false) {
  20.         $this->inverse = $dark;
  21.         $sections = explode("*** ", file_get_contents($txtdef));
  22.         $types = array("P", "A", "B", "C");
  23.         for ($i = 1; $i <= 4; $i++) {
  24.             $variations = explode("\n", $sections[$i]);
  25.             $arr = array();
  26.             for ($j = 2; $j <= 6; $j++) {
  27.                 $bits = explode(" = ", trim($variations[$j]));
  28.                 $arr[] = array($bits[1], explode(",", substr($bits[2], 4, -1)));
  29.             }
  30.             usort($arr, 'ColorScheme::ccompare');
  31.             $this->colors[$types[$i - 1]] = $arr;
  32.         }
  33.         $grays = array("0", 0, "3", 51, "6", 102, "9", 153, "C", 204, "F", 255);
  34.         $this->colors["_"] = array();
  35.         for ($i = 0; $i < count($grays); $i+=2) {
  36.             $hex = "#";
  37.             while (strlen($hex) < 7)
  38.                 $hex.=$grays[$i];
  39.             $this->colors["_"][] = array($hex, array($grays[$i + 1], $grays[$i + 1], $grays[$i + 1]));
  40.         }
  41.     }
  42.  
  43.     public function rand_rgb() {
  44.         return $this->rand("none", 1, null);
  45.     }
  46.  
  47.     public function rand_hex() {
  48.         return $this->rand("none", 0, null);
  49.     }
  50.  
  51.     public function rand_shade_hex($whichColor="_") {
  52.         return $this->rand("which", 0, $whichColor);
  53.     }
  54.  
  55.     public function rand_color_hex($whichShade=2) {
  56.         return $this->rand("shade", 0, $whichShade);
  57.     }
  58.  
  59.     public function rand_shade_rgb($whichColor="_") {
  60.         return $this->rand("which", 1, $whichColor);
  61.     }
  62.  
  63.     public function rand_color_rgb($whichShade=2) {
  64.         return $this->rand("shade", 1, $whichShade);
  65.     }
  66.  
  67.     private function rand($fix, $type, $fixpoint=null) {
  68.         $types = array("P", "A", "B", "C", "_");
  69.         $which = $fix == "which" ? $fixpoint : $types[rand(0, count($this->colors) - 1)];
  70.         $shade = $fix == "shade" ? $fixpoint : rand(0, count($this->colors[$which]) - 1);
  71.         return $this->get($type, $which, $shade);
  72.     }
  73.  
  74.     public function rgb($which="_", $shade=2) {
  75.         return $this->get(1, $which, $shade);
  76.     }
  77.  
  78.     public function hex($which="_", $shade=2) {
  79.         return $this->get(0, $which, $shade);
  80.     }
  81.  
  82.     private function get($type, $which="_", $shade=2) {
  83.         if (!isset($this->colors[$which]))
  84.             $which = "_";
  85.         if (!isset($this->colors[$which][$shade]))
  86.             $shade = 2;
  87.         if ($this->inverse) {
  88.             $shade = count($this->colors[$which]) - $shade - 1;
  89.         }
  90.         return $this->colors[$which][$shade][$type];
  91.     }
  92.  
  93.     public static function ccompare($x, $y) {
  94.         $diff = 0;
  95.         for ($i = 0; $i < 3; $i++) {
  96.             $diff += (int) $x[1][$i] - (int) $y[1][$i];
  97.         }
  98.         return $diff;
  99.     }
  100.  
  101.     public function __toString() {
  102.         $out = "";
  103.         $types = array("Primary", "Secondary A", "Secondary B", "Complementary", "Grayscale");
  104.         $stypes = array("P", "A", "B", "C", "_");
  105.         for ($i = 0; $i < count($stypes); $i++) {
  106.             $out.= "<h3>" . $types[$i] . ": ";
  107.             foreach ($this->colors[$stypes[$i]] as $color) {
  108.                 $out.= "<span style=\"color:" . $color[0] . "\"><strong>w</strong></span>";
  109.             }
  110.             $out.="</h3>\n";
  111.         }
  112.         return $out;
  113.     }
  114.  
  115. }
  116.  
  117. if (isset($_REQUEST['demo'])) {
  118.     $txtdef = "colors/" . (isset($_REQUEST['scheme']) ? $_REQUEST['scheme'] . ".scheme" : "billow.scheme");
  119.     $cs = new ColorScheme($txtdef, isset($_REQUEST['dark']));
  120.     $names = array("P" => "primary", "A" => "secondary-a", "B" => "secondary-b", "C" => "complement", "_" => "gscale");
  121.     ?>
  122.  
  123.     <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  124.         "http://www.w3.org/TR/html4/strict.dtd">
  125.     <html>
  126.         <head>
  127.             <meta http-equiv="content-type" content="text/html; charset=utf-8">
  128.             <title>Color Palette by Color Scheme Generator</title>
  129.             <meta name="generator" content="ColorSchemeGenerator.com">
  130.  
  131.             <style type="text/css">
  132.  
  133.                 /* Palette color codes */
  134.                 /* Feel free to copy&paste color codes to your application */
  135.  
  136.                 <?php
  137.                 foreach (array_keys($names) as $which) {
  138.                     for ($i = 0; $i < 6; $i++) {
  139.                         if ($i == 5 && $which != "_")
  140.                             continue;
  141.                         echo "." . $names[$which] . "-" . ($i + 1) . " { background-color: " . $cs->hex($which, $i) . " }\n";
  142.                     }
  143.                 }
  144.                 ?>
  145.  
  146.                 /* end */
  147.                 body {
  148.                     margin:0; padding: 2em;
  149.                     background:white;
  150.                     color: #666;
  151.                     font: 75%/1.33 Verdana, sans-serif;
  152.                     text-align:left;
  153.                 }
  154.                 h1 {
  155.                     margin: 0.5em 0;
  156.                     font-size: 200%;
  157.                 }
  158.                 p {
  159.                     margin: 0.5em 0;
  160.                 }
  161.  
  162.                 .color-table {
  163.                     margin: 2em 2em 5em;
  164.                     border-collapse:collapse;
  165.                     border:none;
  166.                     border-spacing:0;
  167.                     font-size:100%;
  168.                 }
  169.                 .color-table th {
  170.                     padding: 0 1em 0 0;
  171.                     text-align:right;
  172.                     vertical-align: middle;
  173.                     font-size:110%;
  174.                     border: none;
  175.                 }
  176.                 .color-table td.sample {
  177.                     width:10em; height:8em;
  178.                     padding: 10px;
  179.                     text-align:center;
  180.                     vertical-align:middle;
  181.                     font-size:90%;
  182.                     border: 1px solid white;
  183.                     white-space:nowrap;
  184.                 }
  185.                 .color-table.small td.sample {
  186.                     width:6em; height:6em;
  187.                     padding:0;
  188.                     border:none;
  189.                 }
  190.                 .color-table .white { margin-bottom:0.2em; color:white }
  191.                 .color-table .black { margin-top:0.2em; color:black }
  192.  
  193.                 hr {
  194.                     border:none;
  195.                     border-bottom:1px solid silver;
  196.                 }
  197.                 #footer {
  198.                     padding:1em;
  199.                     font-size:80%;
  200.                 }
  201.  
  202.             </style>
  203.  
  204.         </head>
  205.         <body>
  206.  
  207.             <h1><a href="<?php echo $txtdef; ?>"><?php echo $txtdef; ?></a></h1>
  208.  
  209.             <table class="color-table small">
  210.                 <?php
  211.                 foreach (array_keys($names) as $which) {
  212.                     echo "                <tr>\n";
  213.                     echo "                    <th>" . $names[$which] . "</th>";
  214.                     for ($i = 0; $i < 6; $i++) {
  215.                         if ($i == 5 && $which != "_")
  216.                             continue;
  217.                         echo "                    <td class=\"sample " . $names[$which] . "-" . ($i + 1) . "\"><span style=\"padding:5px;color:#dddddd;background-color:#333333;border:dotted 1px #ffffff;\">" . $which . $i . "</span></td>\n";
  218.                     }
  219.                 }
  220.                 ?>
  221.             </table>
  222.             <p id="footer">Color scheme [probably], and some of the code for this page, originally from <a href="http://colorschemedesigner.com">Color Scheme Designer</a> &copy; Petr Stanicek 2002-2010</p>
  223.  
  224.         </body>
  225.     </html>
  226.  
  227.     <?php
  228. }
  229. ?>