tcpdf_colors.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. <?php
  2. //============================================================+
  3. // File name : tcpdf_colors.php
  4. // Version : 1.0.004
  5. // Begin : 2002-04-09
  6. // Last Update : 2014-04-25
  7. // Author : Nicola Asuni - Tecnick.com LTD - www.tecnick.com - info@tecnick.com
  8. // License : GNU-LGPL v3 (http://www.gnu.org/copyleft/lesser.html)
  9. // -------------------------------------------------------------------
  10. // Copyright (C) 2002-2013 Nicola Asuni - Tecnick.com LTD
  11. //
  12. // This file is part of TCPDF software library.
  13. //
  14. // TCPDF is free software: you can redistribute it and/or modify it
  15. // under the terms of the GNU Lesser General Public License as
  16. // published by the Free Software Foundation, either version 3 of the
  17. // License, or (at your option) any later version.
  18. //
  19. // TCPDF is distributed in the hope that it will be useful, but
  20. // WITHOUT ANY WARRANTY; without even the implied warranty of
  21. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  22. // See the GNU Lesser General Public License for more details.
  23. //
  24. // You should have received a copy of the GNU Lesser General Public License
  25. // along with TCPDF. If not, see <http://www.gnu.org/licenses/>.
  26. //
  27. // See LICENSE.TXT file for more information.
  28. // -------------------------------------------------------------------
  29. //
  30. // Description : Array of WEB safe colors
  31. //
  32. //============================================================+
  33. /**
  34. * @file
  35. * PHP color class for TCPDF
  36. * @author Nicola Asuni
  37. * @package com.tecnick.tcpdf
  38. */
  39. /**
  40. * @class TCPDF_COLORS
  41. * PHP color class for TCPDF
  42. * @package com.tecnick.tcpdf
  43. * @version 1.0.004
  44. * @author Nicola Asuni - info@tecnick.com
  45. */
  46. class TCPDF_COLORS {
  47. /**
  48. * Array of WEB safe colors
  49. * @public static
  50. */
  51. public static $webcolor = array (
  52. 'aliceblue' => 'f0f8ff',
  53. 'antiquewhite' => 'faebd7',
  54. 'aqua' => '00ffff',
  55. 'aquamarine' => '7fffd4',
  56. 'azure' => 'f0ffff',
  57. 'beige' => 'f5f5dc',
  58. 'bisque' => 'ffe4c4',
  59. 'black' => '000000',
  60. 'blanchedalmond' => 'ffebcd',
  61. 'blue' => '0000ff',
  62. 'blueviolet' => '8a2be2',
  63. 'brown' => 'a52a2a',
  64. 'burlywood' => 'deb887',
  65. 'cadetblue' => '5f9ea0',
  66. 'chartreuse' => '7fff00',
  67. 'chocolate' => 'd2691e',
  68. 'coral' => 'ff7f50',
  69. 'cornflowerblue' => '6495ed',
  70. 'cornsilk' => 'fff8dc',
  71. 'crimson' => 'dc143c',
  72. 'cyan' => '00ffff',
  73. 'darkblue' => '00008b',
  74. 'darkcyan' => '008b8b',
  75. 'darkgoldenrod' => 'b8860b',
  76. 'dkgray' => 'a9a9a9',
  77. 'darkgray' => 'a9a9a9',
  78. 'darkgrey' => 'a9a9a9',
  79. 'darkgreen' => '006400',
  80. 'darkkhaki' => 'bdb76b',
  81. 'darkmagenta' => '8b008b',
  82. 'darkolivegreen' => '556b2f',
  83. 'darkorange' => 'ff8c00',
  84. 'darkorchid' => '9932cc',
  85. 'darkred' => '8b0000',
  86. 'darksalmon' => 'e9967a',
  87. 'darkseagreen' => '8fbc8f',
  88. 'darkslateblue' => '483d8b',
  89. 'darkslategray' => '2f4f4f',
  90. 'darkslategrey' => '2f4f4f',
  91. 'darkturquoise' => '00ced1',
  92. 'darkviolet' => '9400d3',
  93. 'deeppink' => 'ff1493',
  94. 'deepskyblue' => '00bfff',
  95. 'dimgray' => '696969',
  96. 'dimgrey' => '696969',
  97. 'dodgerblue' => '1e90ff',
  98. 'firebrick' => 'b22222',
  99. 'floralwhite' => 'fffaf0',
  100. 'forestgreen' => '228b22',
  101. 'fuchsia' => 'ff00ff',
  102. 'gainsboro' => 'dcdcdc',
  103. 'ghostwhite' => 'f8f8ff',
  104. 'gold' => 'ffd700',
  105. 'goldenrod' => 'daa520',
  106. 'gray' => '808080',
  107. 'grey' => '808080',
  108. 'green' => '008000',
  109. 'greenyellow' => 'adff2f',
  110. 'honeydew' => 'f0fff0',
  111. 'hotpink' => 'ff69b4',
  112. 'indianred' => 'cd5c5c',
  113. 'indigo' => '4b0082',
  114. 'ivory' => 'fffff0',
  115. 'khaki' => 'f0e68c',
  116. 'lavender' => 'e6e6fa',
  117. 'lavenderblush' => 'fff0f5',
  118. 'lawngreen' => '7cfc00',
  119. 'lemonchiffon' => 'fffacd',
  120. 'lightblue' => 'add8e6',
  121. 'lightcoral' => 'f08080',
  122. 'lightcyan' => 'e0ffff',
  123. 'lightgoldenrodyellow' => 'fafad2',
  124. 'ltgray' => 'd3d3d3',
  125. 'lightgray' => 'd3d3d3',
  126. 'lightgrey' => 'd3d3d3',
  127. 'lightgreen' => '90ee90',
  128. 'lightpink' => 'ffb6c1',
  129. 'lightsalmon' => 'ffa07a',
  130. 'lightseagreen' => '20b2aa',
  131. 'lightskyblue' => '87cefa',
  132. 'lightslategray' => '778899',
  133. 'lightslategrey' => '778899',
  134. 'lightsteelblue' => 'b0c4de',
  135. 'lightyellow' => 'ffffe0',
  136. 'lime' => '00ff00',
  137. 'limegreen' => '32cd32',
  138. 'linen' => 'faf0e6',
  139. 'magenta' => 'ff00ff',
  140. 'maroon' => '800000',
  141. 'mediumaquamarine' => '66cdaa',
  142. 'mediumblue' => '0000cd',
  143. 'mediumorchid' => 'ba55d3',
  144. 'mediumpurple' => '9370d8',
  145. 'mediumseagreen' => '3cb371',
  146. 'mediumslateblue' => '7b68ee',
  147. 'mediumspringgreen' => '00fa9a',
  148. 'mediumturquoise' => '48d1cc',
  149. 'mediumvioletred' => 'c71585',
  150. 'midnightblue' => '191970',
  151. 'mintcream' => 'f5fffa',
  152. 'mistyrose' => 'ffe4e1',
  153. 'moccasin' => 'ffe4b5',
  154. 'navajowhite' => 'ffdead',
  155. 'navy' => '000080',
  156. 'oldlace' => 'fdf5e6',
  157. 'olive' => '808000',
  158. 'olivedrab' => '6b8e23',
  159. 'orange' => 'ffa500',
  160. 'orangered' => 'ff4500',
  161. 'orchid' => 'da70d6',
  162. 'palegoldenrod' => 'eee8aa',
  163. 'palegreen' => '98fb98',
  164. 'paleturquoise' => 'afeeee',
  165. 'palevioletred' => 'd87093',
  166. 'papayawhip' => 'ffefd5',
  167. 'peachpuff' => 'ffdab9',
  168. 'peru' => 'cd853f',
  169. 'pink' => 'ffc0cb',
  170. 'plum' => 'dda0dd',
  171. 'powderblue' => 'b0e0e6',
  172. 'purple' => '800080',
  173. 'red' => 'ff0000',
  174. 'rosybrown' => 'bc8f8f',
  175. 'royalblue' => '4169e1',
  176. 'saddlebrown' => '8b4513',
  177. 'salmon' => 'fa8072',
  178. 'sandybrown' => 'f4a460',
  179. 'seagreen' => '2e8b57',
  180. 'seashell' => 'fff5ee',
  181. 'sienna' => 'a0522d',
  182. 'silver' => 'c0c0c0',
  183. 'skyblue' => '87ceeb',
  184. 'slateblue' => '6a5acd',
  185. 'slategray' => '708090',
  186. 'slategrey' => '708090',
  187. 'snow' => 'fffafa',
  188. 'springgreen' => '00ff7f',
  189. 'steelblue' => '4682b4',
  190. 'tan' => 'd2b48c',
  191. 'teal' => '008080',
  192. 'thistle' => 'd8bfd8',
  193. 'tomato' => 'ff6347',
  194. 'turquoise' => '40e0d0',
  195. 'violet' => 'ee82ee',
  196. 'wheat' => 'f5deb3',
  197. 'white' => 'ffffff',
  198. 'whitesmoke' => 'f5f5f5',
  199. 'yellow' => 'ffff00',
  200. 'yellowgreen' => '9acd32'
  201. ); // end of web colors
  202. /**
  203. * Array of valid JavaScript color names
  204. * @public static
  205. */
  206. public static $jscolor = array ('transparent', 'black', 'white', 'red', 'green', 'blue', 'cyan', 'magenta', 'yellow', 'dkGray', 'gray', 'ltGray');
  207. /**
  208. * Array of Spot colors (C,M,Y,K,name)
  209. * Color keys must be in lowercase and without spaces.
  210. * As long as no open standard for spot colours exists, you have to buy a colour book by one of the colour manufacturers and insert the values and names of spot colours directly.
  211. * Common industry standard spot colors are: ANPA-COLOR, DIC, FOCOLTONE, GCMI, HKS, PANTONE, TOYO, TRUMATCH.
  212. * @public static
  213. */
  214. public static $spotcolor = array (
  215. // special registration colors
  216. 'none' => array( 0, 0, 0, 0, 'None'),
  217. 'all' => array(100, 100, 100, 100, 'All'),
  218. // standard CMYK colors
  219. 'cyan' => array(100, 0, 0, 0, 'Cyan'),
  220. 'magenta' => array( 0, 100, 0, 0, 'Magenta'),
  221. 'yellow' => array( 0, 0, 100, 0, 'Yellow'),
  222. 'key' => array( 0, 0, 0, 100, 'Key'),
  223. // alias
  224. 'white' => array( 0, 0, 0, 0, 'White'),
  225. 'black' => array( 0, 0, 0, 100, 'Black'),
  226. // standard RGB colors
  227. 'red' => array( 0, 100, 100, 0, 'Red'),
  228. 'green' => array(100, 0, 100, 0, 'Green'),
  229. 'blue' => array(100, 100, 0, 0, 'Blue'),
  230. // Add here standard spot colors or dynamically define them with AddSpotColor()
  231. // ...
  232. ); // end of spot colors
  233. // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  234. /**
  235. * Return the Spot color array.
  236. * @param string $name Name of the spot color.
  237. * @param array $spotc Reference to an array of spot colors.
  238. * @return array|false Spot color array or false if not defined.
  239. * @since 5.9.125 (2011-10-03)
  240. * @public static
  241. */
  242. public static function getSpotColor($name, &$spotc) {
  243. if (isset($spotc[$name])) {
  244. return $spotc[$name];
  245. }
  246. $color = preg_replace('/[\s]*/', '', $name); // remove extra spaces
  247. $color = strtolower($color);
  248. if (isset(self::$spotcolor[$color])) {
  249. if (!isset($spotc[$name])) {
  250. $i = (1 + count($spotc));
  251. $spotc[$name] = array('C' => self::$spotcolor[$color][0], 'M' => self::$spotcolor[$color][1], 'Y' => self::$spotcolor[$color][2], 'K' => self::$spotcolor[$color][3], 'name' => self::$spotcolor[$color][4], 'i' => $i);
  252. }
  253. return $spotc[self::$spotcolor[$color][4]];
  254. }
  255. return false;
  256. }
  257. /**
  258. * Returns an array (RGB or CMYK) from an html color name, or a six-digit (i.e. #3FE5AA), or three-digit (i.e. #7FF) hexadecimal color, or a javascript color array, or javascript color name.
  259. * @param string $hcolor HTML color.
  260. * @param array $spotc Reference to an array of spot colors.
  261. * @param array $defcol Color to return in case of error.
  262. * @return array|false RGB or CMYK color, or false in case of error.
  263. * @public static
  264. */
  265. public static function convertHTMLColorToDec($hcolor, &$spotc, $defcol=array('R'=>128,'G'=>128,'B'=>128)) {
  266. $color = preg_replace('/[\s]*/', '', $hcolor); // remove extra spaces
  267. $color = strtolower($color);
  268. // check for javascript color array syntax
  269. if (strpos($color, '[') !== false) {
  270. if (preg_match('/[\[][\"\'](t|g|rgb|cmyk)[\"\'][\,]?([0-9\.]*)[\,]?([0-9\.]*)[\,]?([0-9\.]*)[\,]?([0-9\.]*)[\]]/', $color, $m) > 0) {
  271. $returncolor = array();
  272. switch ($m[1]) {
  273. case 'cmyk': {
  274. // RGB
  275. $returncolor['C'] = max(0, min(100, (floatval($m[2]) * 100)));
  276. $returncolor['M'] = max(0, min(100, (floatval($m[3]) * 100)));
  277. $returncolor['Y'] = max(0, min(100, (floatval($m[4]) * 100)));
  278. $returncolor['K'] = max(0, min(100, (floatval($m[5]) * 100)));
  279. break;
  280. }
  281. case 'rgb': {
  282. // RGB
  283. $returncolor['R'] = max(0, min(255, (floatval($m[2]) * 255)));
  284. $returncolor['G'] = max(0, min(255, (floatval($m[3]) * 255)));
  285. $returncolor['B'] = max(0, min(255, (floatval($m[4]) * 255)));
  286. break;
  287. }
  288. case 'g': {
  289. // grayscale
  290. $returncolor['G'] = max(0, min(255, (floatval($m[2]) * 255)));
  291. break;
  292. }
  293. case 't':
  294. default: {
  295. // transparent (empty array)
  296. break;
  297. }
  298. }
  299. return $returncolor;
  300. }
  301. } elseif ((substr($color, 0, 4) != 'cmyk') AND (substr($color, 0, 3) != 'rgb') AND (($dotpos = strpos($color, '.')) !== false)) {
  302. // remove class parent (i.e.: color.red)
  303. $color = substr($color, ($dotpos + 1));
  304. if ($color == 'transparent') {
  305. // transparent (empty array)
  306. return array();
  307. }
  308. }
  309. if (strlen($color) == 0) {
  310. return $defcol;
  311. }
  312. // RGB ARRAY
  313. if (substr($color, 0, 3) == 'rgb') {
  314. $codes = substr($color, 4);
  315. $codes = str_replace(')', '', $codes);
  316. $returncolor = explode(',', $codes);
  317. foreach ($returncolor as $key => $val) {
  318. if (strpos($val, '%') > 0) {
  319. // percentage
  320. $returncolor[$key] = (255 * intval($val) / 100);
  321. } else {
  322. $returncolor[$key] = intval($val);
  323. }
  324. // normalize value
  325. $returncolor[$key] = max(0, min(255, $returncolor[$key]));
  326. }
  327. return $returncolor;
  328. }
  329. // CMYK ARRAY
  330. if (substr($color, 0, 4) == 'cmyk') {
  331. $codes = substr($color, 5);
  332. $codes = str_replace(')', '', $codes);
  333. $returncolor = explode(',', $codes);
  334. foreach ($returncolor as $key => $val) {
  335. if (strpos($val, '%') !== false) {
  336. // percentage
  337. $returncolor[$key] = (100 * intval($val) / 100);
  338. } else {
  339. $returncolor[$key] = intval($val);
  340. }
  341. // normalize value
  342. $returncolor[$key] = max(0, min(100, $returncolor[$key]));
  343. }
  344. return $returncolor;
  345. }
  346. if ($color[0] != '#') {
  347. // COLOR NAME
  348. if (isset(self::$webcolor[$color])) {
  349. // web color
  350. $color_code = self::$webcolor[$color];
  351. } else {
  352. // spot color
  353. $returncolor = self::getSpotColor($hcolor, $spotc);
  354. if ($returncolor === false) {
  355. $returncolor = $defcol;
  356. }
  357. return $returncolor;
  358. }
  359. } else {
  360. $color_code = substr($color, 1);
  361. }
  362. // HEXADECIMAL REPRESENTATION
  363. switch (strlen($color_code)) {
  364. case 3: {
  365. // 3-digit RGB hexadecimal representation
  366. $r = substr($color_code, 0, 1);
  367. $g = substr($color_code, 1, 1);
  368. $b = substr($color_code, 2, 1);
  369. $returncolor = array();
  370. $returncolor['R'] = max(0, min(255, hexdec($r.$r)));
  371. $returncolor['G'] = max(0, min(255, hexdec($g.$g)));
  372. $returncolor['B'] = max(0, min(255, hexdec($b.$b)));
  373. break;
  374. }
  375. case 6: {
  376. // 6-digit RGB hexadecimal representation
  377. $returncolor = array();
  378. $returncolor['R'] = max(0, min(255, hexdec(substr($color_code, 0, 2))));
  379. $returncolor['G'] = max(0, min(255, hexdec(substr($color_code, 2, 2))));
  380. $returncolor['B'] = max(0, min(255, hexdec(substr($color_code, 4, 2))));
  381. break;
  382. }
  383. case 8: {
  384. // 8-digit CMYK hexadecimal representation
  385. $returncolor = array();
  386. $returncolor['C'] = max(0, min(100, round(hexdec(substr($color_code, 0, 2)) / 2.55)));
  387. $returncolor['M'] = max(0, min(100, round(hexdec(substr($color_code, 2, 2)) / 2.55)));
  388. $returncolor['Y'] = max(0, min(100, round(hexdec(substr($color_code, 4, 2)) / 2.55)));
  389. $returncolor['K'] = max(0, min(100, round(hexdec(substr($color_code, 6, 2)) / 2.55)));
  390. break;
  391. }
  392. default: {
  393. $returncolor = $defcol;
  394. break;
  395. }
  396. }
  397. return $returncolor;
  398. }
  399. /**
  400. * Convert a color array into a string representation.
  401. * @param array $c Array of colors.
  402. * @return string The color array representation.
  403. * @since 5.9.137 (2011-12-01)
  404. * @public static
  405. */
  406. public static function getColorStringFromArray($c) {
  407. $c = array_values($c);
  408. $color = '[';
  409. switch (count($c)) {
  410. case 4: {
  411. // CMYK
  412. $color .= sprintf('%F %F %F %F', (max(0, min(100, floatval($c[0]))) / 100), (max(0, min(100, floatval($c[1]))) / 100), (max(0, min(100, floatval($c[2]))) / 100), (max(0, min(100, floatval($c[3]))) / 100));
  413. break;
  414. }
  415. case 3: {
  416. // RGB
  417. $color .= sprintf('%F %F %F', (max(0, min(255, floatval($c[0]))) / 255), (max(0, min(255, floatval($c[1]))) / 255), (max(0, min(255, floatval($c[2]))) / 255));
  418. break;
  419. }
  420. case 1: {
  421. // grayscale
  422. $color .= sprintf('%F', (max(0, min(255, floatval($c[0]))) / 255));
  423. break;
  424. }
  425. }
  426. $color .= ']';
  427. return $color;
  428. }
  429. /**
  430. * Convert color to javascript color.
  431. * @param string $color color name or "#RRGGBB"
  432. * @protected
  433. * @since 2.1.002 (2008-02-12)
  434. * @public static
  435. */
  436. public static function _JScolor($color) {
  437. if (substr($color, 0, 1) == '#') {
  438. return sprintf("['RGB',%F,%F,%F]", (hexdec(substr($color, 1, 2)) / 255), (hexdec(substr($color, 3, 2)) / 255), (hexdec(substr($color, 5, 2)) / 255));
  439. }
  440. if (!in_array($color, self::$jscolor)) {
  441. // default transparent color
  442. $color = self::$jscolor[0];
  443. }
  444. return 'color.'.$color;
  445. }
  446. } // END OF TCPDF_COLORS CLASS
  447. //============================================================+
  448. // END OF FILE
  449. //============================================================+