fork download
  1. <?php
  2. //$colors = $_GET['color'];//Thought these variables made more sense up here.
  3. $gradient = "red,orange,yellow,green,blue,indigo,violet";//$_GET['gradient'];
  4. if (isset($colors)){ //added these to avoid sending a blank page with image headers.
  5. header('Content-Type: image/png');
  6. $type = 'color';
  7. } else if (isset($gradient)){
  8. header('Content-Type: image/png');
  9. $type = 'gradient';
  10. }
  11.  
  12. $colornames = array(
  13. "pink" => "FFC0CB",
  14. "lightpink" => "FFB6C1",
  15. "hotpink" => "FF69B4",
  16. "deeppink" => "FF1493",
  17. "palevioletred" => "DB7093",
  18. "mediumvioletred" => "C71585",
  19. "lightsalmon" => "FFA07A",
  20. "salmon" => "FA8072",
  21. "darksalmon" => "E9967A",
  22. "lightcoral" => "F08080",
  23. "indianred" => "CD5C5C",
  24. "crimson" => "DC143C",
  25. "firebrick" => "B22222",
  26. "darkred" => "8B0000",
  27. "red" => "FF0000",
  28. "orangered" => "FF4500",
  29. "tomato" => "FF6347",
  30. "coral" => "FF7F50",
  31. "darkorange" => "FF8C00",
  32. "orange" => "FFA500",
  33. "gold" => "FFD700",
  34. "yellow" => "FFFF00",
  35. "lightyellow" => "FFFFE0",
  36. "lemonchiffon" => "FFFACD",
  37. "lightgoldenrodyellow" => "FAFAD2",
  38. "papayawhip" => "FFEFD5",
  39. "moccasin" => "FFE4B5",
  40. "peachpuff" => "FFDAB9",
  41. "palegoldenrod" => "EEE8AA",
  42. "khaki" => "F0E68C",
  43. "darkkhaki" => "BDB76B",
  44. "cornsilk" => "FFF8DC",
  45. "blanchedalmond" => "FFEBCD",
  46. "bisque" => "FFE4C4",
  47. "navajowhite" => "FFDEAD",
  48. "wheat" => "F5DEB3",
  49. "burlywood" => "DEB887",
  50. "tan" => "D2B48C",
  51. "rosybrown" => "BC8F8F",
  52. "sandybrown" => "F4A460",
  53. "goldenrod" => "DAA520",
  54. "darkgoldenrod" => "B8860B",
  55. "peru" => "CD853F",
  56. "chocolate" => "D2691E",
  57. "saddlebrown" => "8B4513",
  58. "sienna" => "A0522D",
  59. "brown" => "A52A2A",
  60. "maroon" => "800000",
  61. "darkolivegreen" => "556B2F",
  62. "olive" => "808000",
  63. "olivedrab" => "6B8E23",
  64. "yellowgreen" => "9ACD32",
  65. "limegreen" => "32CD32",
  66. "lime" => "00FF00",
  67. "lawngreen" => "7CFC00",
  68. "chartreuse" => "7FFF00",
  69. "greenyellow" => "ADFF2F",
  70. "springgreen" => "00FF7F",
  71. "mediumspringgreen" => "00FA9A",
  72. "lightgreen" => "90EE90",
  73. "palegreen" => "98FB98",
  74. "darkseagreen" => "8FBC8F",
  75. "mediumseagreen" => "3CB371",
  76. "seagreen" => "2E8B57",
  77. "forestgreen" => "228B22",
  78. "green" => "008000",
  79. "darkgreen" => "006400",
  80. "mediumaquamarine" => "66CDAA",
  81. "aqua" => "00FFFF",
  82. "cyan" => "00FFFF",
  83. "lightcyan" => "E0FFFF",
  84. "paleturquoise" => "AFEEEE",
  85. "aquamarine" => "7FFFD4",
  86. "turquoise" => "40E0D0",
  87. "mediumturquoise" => "48D1CC",
  88. "darkturquoise" => "00CED1",
  89. "lightseagreen" => "20B2AA",
  90. "cadetblue" => "5F9EA0",
  91. "darkcyan" => "008B8B",
  92. "teal" => "008080",
  93. "lightsteelblue" => "B0C4DE",
  94. "powderblue" => "B0E0E6",
  95. "lightblue" => "ADD8E6",
  96. "skyblue" => "87CEEB",
  97. "lightskyblue" => "87CEFA",
  98. "deepskyblue" => "00BFFF",
  99. "dodgerblue" => "1E90FF",
  100. "cornflowerblue" => "6495ED",
  101. "steelblue" => "4682B4",
  102. "royalblue" => "4169E1",
  103. "blue" => "0000FF",
  104. "mediumblue" => "0000CD",
  105. "darkblue" => "00008B",
  106. "navy" => "000080",
  107. "midnightblue" => "191970",
  108. "lavender" => "E6E6FA",
  109. "thistle" => "D8BFD8",
  110. "plum" => "DDA0DD",
  111. "violet" => "EE82EE",
  112. "orchid" => "DA70D6",
  113. "fuchsia" => "FF00FF",
  114. "magenta" => "FF00FF",
  115. "mediumorchid" => "BA55D3",
  116. "mediumpurple" => "9370DB",
  117. "blueviolet" => "8A2BE2",
  118. "darkviolet" => "9400D3",
  119. "darkorchid" => "9932CC",
  120. "darkmagenta" => "8B008B",
  121. "purple" => "800080",
  122. "indigo" => "4B0082",
  123. "darkslateblue" => "483D8B",
  124. "slateblue" => "6A5ACD",
  125. "mediumslateblue" => "7B68EE",
  126. "white" => "FFFFFF",
  127. "snow" => "FFFAFA",
  128. "honeydew" => "F0FFF0",
  129. "mintcream" => "F5FFFA",
  130. "azure" => "F0FFFF",
  131. "aliceblue" => "F0F8FF",
  132. "ghostwhite" => "F8F8FF",
  133. "whitesmoke" => "F5F5F5",
  134. "seashell" => "FFF5EE",
  135. "beige" => "F5F5DC",
  136. "oldlace" => "FDF5E6",
  137. "floralwhite" => "FFFAF0",
  138. "ivory" => "FFFFF0",
  139. "antiquewhite" => "FAEBD7",
  140. "linen" => "FAF0E6",
  141. "lavenderblush" => "FFF0F5",
  142. "mistyrose" => "FFE4E1",
  143. "gainsboro" => "DCDCDC",
  144. "lightgray" => "D3D3D3",
  145. "lightgrey" => "D3D3D3",
  146. "silver" => "C0C0C0",
  147. "darkgray" => "A9A9A9",
  148. "darkgrey" => "A9A9A9",
  149. "gray" => "808080",
  150. "grey" => "808080",
  151. "dimgray" => "696969",
  152. "dimgrey" => "696969",
  153. "lightslategray" => "778899",
  154. "lightslategrey" => "778899",
  155. "slategray" => "708090",
  156. "slategrey" => "708090",
  157. "darkslategray" => "2F4F4F",
  158. "darkslategrey" => "2F4F4F",
  159. "black" => "000000"
  160. );
  161.  
  162.  
  163. function rgbFromHex($hexValue){
  164. if (strlen($hexValue) == 3) { //shorthand check
  165. $r = hexdec(str_repeat($hexValue[0], 2));
  166. $g = hexdec(str_repeat($hexValue[1], 2));
  167. $b = hexdec(str_repeat($hexValue[2], 2));
  168. }
  169. else if (strlen($hexValue) == 6){ //standard hex color
  170. $r = hexdec(substr($hexValue, 0, 2));
  171. $g = hexdec(substr($hexValue, 2, 2));
  172. $b = hexdec(substr($hexValue, 4, 2));
  173. }
  174. else{
  175. $r = 0;
  176. $g = 0;
  177. $b = 0;
  178. }
  179.  
  180. $rgb = array(
  181. $r,
  182. $g,
  183. $b
  184. );
  185. return $rgb;
  186. }
  187.  
  188. function getHexByName($name){
  189. global $colornames;
  190. if (isset($colornames[$name])) {
  191. return $colornames[$name];
  192. }
  193. else {
  194. return null;
  195. }
  196. }
  197.  
  198. /*
  199.  * $input - a string, possibly comma-separated list of HexValues and/or color names
  200.  *
  201.  * returns an array of [r, g, b] arrays
  202.  */
  203. function parseColors($input){
  204. $colorArray = array();
  205. $parts = explode(",", $input);
  206.  
  207. foreach($parts as $color){
  208. $byName = getHexByName($color);
  209. if(!isset($byName)){
  210. array_push($colorArray, rgbFromHex($color));
  211. } else{
  212. if (strpos($byName, ",") !== FALSE){
  213. // we're dealing with a multi-part name, eg: roygbiv
  214. foreach(parseColors($byName) as $col){
  215. array_push($colorArray, $col);
  216. }
  217. } else{
  218. array_push($colorArray, rgbFromHex($byName));
  219. }
  220. }
  221. }
  222.  
  223. return $colorArray;
  224. }
  225.  
  226. /*
  227.  * $colorArray - an array of [r, g, b] arrays.
  228.  *
  229.  * returns an array of [r, g, b] arrays.
  230.  */
  231. function interpolateGradient($colorArray){
  232. $steps = 200;
  233. $arrLen = count($colorArray) - 1;
  234. $stepsPerGrad = $steps/$arrLen;
  235. $output = array();
  236.  
  237. for($step = 0;$step < $steps; $step++){
  238. $index = $step / $steps * $arrLen;
  239. $curr = $colorArray[$index];
  240. $next = $colorArray[$index + 1];
  241.  
  242. if(isset($next)){
  243. array_push($output, array(
  244. $curr[0] + ($step % $stepsPerGrad) / $stepsPerGrad * ($next[0] - $curr[0]),
  245. $curr[1] + ($step % $stepsPerGrad) / $stepsPerGrad * ($next[1] - $curr[1]),
  246. $curr[2] + ($step % $stepsPerGrad) / $stepsPerGrad * ($next[2] - $curr[2])
  247. ));
  248. }
  249. else{
  250. array_push($output, $curr);
  251. }
  252. }
  253.  
  254. return $output;
  255. }
  256.  
  257. /*
  258.  * $colorArray - an array of [r, g, b] arrays.
  259.  *
  260.  * returns an image.
  261.  */
  262. function createSwatch($colorArray){
  263. $img = imagecreate(200, 40);
  264. $colWidth = 200 / count($colorArray);
  265. $x = 0;
  266. foreach($colorArray as $rgb) {
  267. $color = imagecolorallocate($img, $rgb[0], $rgb[1], $rgb[2]);
  268. imagefilledrectangle($img, $x, 0, $x + $colWidth, 40, $color);
  269. $x += $colWidth;
  270. }
  271. return imagepng($img);
  272. }
  273.  
  274.  
  275.  
  276.  
  277. if ($type == 'color') {
  278. $colorArray = parseColors(strtolower($colors));
  279. } else if ($type == 'gradient'){
  280. $colorArray = interpolateGradient(parseColors(strtolower($gradient)));
  281. } else {
  282. echo 'GO AWAY';
  283. }
  284.  
  285. if (isset($colorArray)){
  286. createSwatch($colorArray);
  287. }
  288.  
  289. ?>
Success #stdin #stdout 0.01s 20560KB
stdin
Standard input is empty
stdout