PageRenderTime 46ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/Image/Color.php

https://bitbucket.org/yousef_fadila/vtiger
PHP | 719 lines | 382 code | 52 blank | 285 comment | 47 complexity | f8c85bc6695327aa46494e9537fbb180 MD5 | raw file
Possible License(s): LGPL-2.1, GPL-2.0
  1. <?php
  2. /* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
  3. /**
  4. * Color.php is the implementation of Image_Color.
  5. *
  6. * PHP versions 4 and 5
  7. *
  8. * LICENSE: This source file is subject to version 3.0 of the PHP license
  9. * that is available through the world-wide-web at the following URI:
  10. * http://www.php.net/license/3_0.txt. If you did not receive a copy of
  11. * the PHP License and are unable to obtain it through the web, please
  12. * send a note to license@php.net so we can mail you a copy immediately.
  13. *
  14. * @category Image
  15. * @package Image_Color
  16. * @author Jason Lotito <jason@lehighweb.com>
  17. * @author Andrew Morton <drewish@katherinehouse.com>
  18. * @copyright 2003-2005 The PHP Group
  19. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  20. * @version CVS: $Id: Color.php,v 1.15 2005/09/12 19:12:02 drewish Exp $
  21. * @link http://pear.php.net/package/Image_Color
  22. */
  23. /**
  24. * Image_Color handles color conversion and mixing.
  25. *
  26. * The class is quick, simple to use, and does its job fairly well but it's got
  27. * some code smells:
  28. * - Call setColors() for some functions but not others.
  29. * - Different functions expect different color formats. setColors() only
  30. * accepts hex while allocateColor() will accept named or hex (provided the
  31. * hex ones start with the # character).
  32. * - Some conversions go in only one direction, ie HSV->RGB but no RGB->HSV.
  33. * I'm going to try to straighten out some of this but I'll be hard to do so
  34. * without breaking backwards compatibility.
  35. *
  36. * @category Image
  37. * @package Image_Color
  38. * @author Jason Lotito <jason@lehighweb.com>
  39. * @author Andrew Morton <drewish@katherinehouse.com>
  40. * @copyright 2003-2005 The PHP Group
  41. * @license http://www.php.net/license/3_0.txt PHP License 3.0
  42. * @version Release: 0.1.2
  43. * @link http://pear.php.net/package/Image_Color
  44. */
  45. class Image_Color
  46. {
  47. /**
  48. * First color that the class handles for ranges and mixes.
  49. * @var array
  50. * @access public
  51. * @see setColors()
  52. */
  53. var $color1 = array();
  54. /**
  55. * Second color that the class handles for ranges and mixes.
  56. * @var array
  57. * @access public
  58. * @see setColors()
  59. */
  60. var $color2 = array();
  61. /**
  62. * Boolean value for determining whether colors outputted should be limited
  63. * to the web safe pallet or not.
  64. *
  65. * @var boolean
  66. * @access private
  67. * @see setWebSafe()
  68. */
  69. var $_websafeb = false;
  70. /**
  71. * Mix two colors together by finding their average. If the colors are not
  72. * passed as parameters, the class's colors will be mixed instead.
  73. *
  74. * @param string $col1 The first color you want to mix
  75. * @param string $col2 The second color you want to mix
  76. * @return string The mixed color.
  77. * @access public
  78. * @author Jason Lotito <jason@lehighweb.com>
  79. * @uses _setColors() to assign the colors if any are passed to the
  80. * class.
  81. */
  82. function mixColors($col1 = false, $col2 = false)
  83. {
  84. if ($col1) {
  85. $this->_setColors($col1, $col2);
  86. }
  87. // after finding the average, it will be a float. add 0.5 and then
  88. // cast to an integer to properly round it to an integer.
  89. $color3[0] = (int) ((($this->color1[0] + $this->color2[0]) / 2) + 0.5);
  90. $color3[1] = (int) ((($this->color1[1] + $this->color2[1]) / 2) + 0.5);
  91. $color3[2] = (int) ((($this->color1[2] + $this->color2[2]) / 2) + 0.5);
  92. if ($this->_websafeb) {
  93. array_walk($color3, '_makeWebSafe');
  94. }
  95. return Image_Color::rgb2hex($color3);
  96. }
  97. /**
  98. * Determines whether colors the returned by this class will be rounded to
  99. * the nearest web safe value.
  100. *
  101. * @param boolean $bool Indicates if colors should be limited to the
  102. * websafe pallet.
  103. * @return void
  104. * @access public
  105. * @author Jason Lotito <jason@lehighweb.com>
  106. */
  107. function setWebSafe($bool = true)
  108. {
  109. $this->_websafeb = (boolean) $bool;
  110. }
  111. /**
  112. * Set the two colors this class uses for mixing and ranges.
  113. *
  114. * @param string $col1 The first color in hex format
  115. * @param string $col2 The second color in hex format
  116. * @return void
  117. * @access public
  118. * @author Jason Lotito <jason@lehighweb.com>
  119. */
  120. function setColors($col1, $col2)
  121. {
  122. $this->_setColors($col1, $col2);
  123. }
  124. /**
  125. * Get the range of colors between the class's two colors, given a degree.
  126. *
  127. * @param integer $degrees How large a 'step' we should take between the
  128. * colors.
  129. * @return array Returns an array of hex strings, one element for each
  130. * color.
  131. * @access public
  132. * @author Jason Lotito <jason@lehighweb.com>
  133. * @todo Allow for degrees for individual parts of the colors.
  134. */
  135. function getRange($degrees = 2)
  136. {
  137. if ($degrees == 0) {
  138. $degrees = 1;
  139. }
  140. // The degrees give us how much we should advance each color at each
  141. // phase of the loop. This way, the advance is equal throughout all
  142. // the colors.
  143. $red_steps = ($this->color2[0] - $this->color1[0]) / $degrees;
  144. $green_steps = ($this->color2[1] - $this->color1[1]) / $degrees;
  145. $blue_steps = ($this->color2[2] - $this->color1[2]) / $degrees;
  146. $allcolors = array();
  147. /**
  148. * The loop stops once any color has gone beyond the end color.
  149. */
  150. // Loop through all the degrees between the colors
  151. for ($x = 0; $x < $degrees; $x++) {
  152. $col[0] = $red_steps * $x;
  153. $col[1] = $green_steps * $x;
  154. $col[2] = $blue_steps * $x;
  155. // Loop through each R, G, and B
  156. for ($i = 0; $i < 3; $i++) {
  157. $partcolor = $this->color1[$i] + $col[$i];
  158. // If the color is less than 256
  159. if ($partcolor < 256) {
  160. // Makes sure the colors is not less than 0
  161. if ($partcolor > -1) {
  162. $newcolor[$i] = $partcolor;
  163. } else {
  164. $newcolor[$i] = 0;
  165. }
  166. // Color was greater than 255
  167. } else {
  168. $newcolor[$i] = 255;
  169. }
  170. }
  171. if ($this->_websafeb) {
  172. array_walk($newcolor, '_makeWebSafe');
  173. }
  174. $allcolors[] = Image_Color::rgb2hex($newcolor);
  175. }
  176. return $allcolors;
  177. }
  178. /**
  179. * Change the lightness of the class's two colors.
  180. *
  181. * @param integer $degree The degree of the change. Positive values
  182. * lighten the color while negative values will darken it.
  183. * @return void
  184. * @access public
  185. * @author Jason Lotito <jason@lehighweb.com>
  186. * @uses Image_Color::$color1 as an input and return value.
  187. * @uses Image_Color::$color2 as an input and return value.
  188. */
  189. function changeLightness($degree = 10)
  190. {
  191. $color1 =& $this->color1;
  192. $color2 =& $this->color2;
  193. for ($x = 0; $x < 3; $x++) {
  194. if (($color1[$x] + $degree) < 256) {
  195. if (($color1[$x] + $degree) > -1) {
  196. $color1[$x] += $degree;
  197. } else {
  198. $color1[$x] = 0;
  199. }
  200. } else {
  201. $color1[$x] = 255;
  202. }
  203. if (($color2[$x] + $degree) < 256) {
  204. if (($color2[$x] + $degree) > -1) {
  205. $color2[$x] += $degree;
  206. } else {
  207. $color2[$x] = 0;
  208. }
  209. } else {
  210. $color2[$x] = 255;
  211. }
  212. }
  213. }
  214. /**
  215. * Determine if a light or dark text color would be more readable on a
  216. * background of a given color. This is determined by the G(reen) value of
  217. * RGB. You can change the dark and the light colors from their default
  218. * black and white.
  219. *
  220. * @param string $color The hex color to analyze
  221. * @param string $light The light color value to return if we should
  222. * have light text.
  223. * @param string $dark The dark color value to return if we should have
  224. * dark text.
  225. * @return string The light or dark value which would make the text most
  226. * readable.
  227. * @access public
  228. * @static
  229. * @author Jason Lotito <jason@lehighweb.com>
  230. */
  231. function getTextColor($color, $light = '#FFFFFF', $dark = '#000000')
  232. {
  233. $color = Image_Color::_splitColor($color);
  234. if ($color[1] > hexdec('66')) {
  235. return $dark;
  236. } else {
  237. return $light;
  238. }
  239. }
  240. /**
  241. * Internal method to set the colors.
  242. *
  243. * @param string $col1 First color, either a name or hex value
  244. * @param string $col2 Second color, either a name or hex value
  245. * @return void
  246. * @access private
  247. * @author Jason Lotito <jason@lehighweb.com>
  248. */
  249. function _setColors($col1, $col2)
  250. {
  251. if ($col1) {
  252. $this->color1 = Image_Color::_splitColor($col1);
  253. }
  254. if ($col2) {
  255. $this->color2 = Image_Color::_splitColor($col2);
  256. }
  257. }
  258. /**
  259. * Given a color, properly split it up into a 3 element RGB array.
  260. *
  261. * @param string $color The color.
  262. * @return array A three element RGB array.
  263. * @access private
  264. * @static
  265. * @author Jason Lotito <jason@lehighweb.com>
  266. */
  267. function _splitColor($color)
  268. {
  269. $color = str_replace('#', '', $color);
  270. $c[] = hexdec(substr($color, 0, 2));
  271. $c[] = hexdec(substr($color, 2, 2));
  272. $c[] = hexdec(substr($color, 4, 2));
  273. return $c;
  274. }
  275. /**
  276. * This is deprecated. Use rgb2hex() instead.
  277. * @access private
  278. * @deprecated Function deprecated after 1.0.1
  279. * @see rgb2hex().
  280. */
  281. function _returnColor ( $color )
  282. {
  283. return Image_Color::rgb2hex($color);
  284. }
  285. /**
  286. * Convert an RGB array to a hex string.
  287. *
  288. * @param array $color 3 element RGB array.
  289. * @return string Hex color string.
  290. * @access public
  291. * @static
  292. * @author Jason Lotito <jason@lehighweb.com>
  293. * @see hex2rgb()
  294. */
  295. function rgb2hex($color)
  296. {
  297. return sprintf('%02X%02X%02X',$color[0],$color[1],$color[2]);
  298. }
  299. /**
  300. * Convert a hex color string into an RGB array. An extra fourth element
  301. * will be returned with the original hex value.
  302. *
  303. * @param string $hex Hex color string.
  304. * @return array RGB color array with an extra 'hex' element containing
  305. * the original hex string.
  306. * @access public
  307. * @static
  308. * @author Jason Lotito <jason@lehighweb.com>
  309. * @see rgb2hex()
  310. */
  311. function hex2rgb($hex)
  312. {
  313. $return = Image_Color::_splitColor($hex);
  314. $return['hex'] = $hex;
  315. return $return;
  316. }
  317. /**
  318. * Convert an HSV (Hue, Saturation, Brightness) value to RGB.
  319. *
  320. * @param integer $h Hue
  321. * @param integer $s Saturation
  322. * @param integer $v Brightness
  323. * @return array RGB array.
  324. * @access public
  325. * @static
  326. * @author Jason Lotito <jason@lehighweb.com>
  327. * @uses hsv2hex() to convert the HSV value to Hex.
  328. * @uses hex2rgb() to convert the Hex value to RGB.
  329. */
  330. function hsv2rgb($h, $s, $v)
  331. {
  332. return Image_Color::hex2rgb(Image_Color::hsv2hex($h, $s, $v));
  333. }
  334. /**
  335. * Convert HSV (Hue, Saturation, Brightness) to a hex color string.
  336. *
  337. * Originally written by Jurgen Schwietering. Integrated into the class by
  338. * Jason Lotito.
  339. *
  340. * @param integer $h Hue
  341. * @param integer $s Saturation
  342. * @param integer $v Brightness
  343. * @return string The hex string.
  344. * @access public
  345. * @static
  346. * @author Jurgen Schwietering <jurgen@schwietering.com>
  347. * @uses rgb2hex() to convert the return value to a hex string.
  348. */
  349. function hsv2hex($h, $s, $v)
  350. {
  351. $s /= 256.0;
  352. $v /= 256.0;
  353. if ($s == 0.0) {
  354. $r = $g = $b = $v;
  355. return '';
  356. } else {
  357. $h = $h / 256.0 * 6.0;
  358. $i = floor($h);
  359. $f = $h - $i;
  360. $v *= 256.0;
  361. $p = (integer)($v * (1.0 - $s));
  362. $q = (integer)($v * (1.0 - $s * $f));
  363. $t = (integer)($v * (1.0 - $s * (1.0 - $f)));
  364. switch($i) {
  365. case 0:
  366. $r = $v;
  367. $g = $t;
  368. $b = $p;
  369. break;
  370. case 1:
  371. $r = $q;
  372. $g = $v;
  373. $b = $p;
  374. break;
  375. case 2:
  376. $r = $p;
  377. $g = $v;
  378. $b = $t;
  379. break;
  380. case 3:
  381. $r = $p;
  382. $g = $q;
  383. $b = $v;
  384. break;
  385. case 4:
  386. $r = $t;
  387. $g = $p;
  388. $b = $v;
  389. break;
  390. default:
  391. $r = $v;
  392. $g = $p;
  393. $b = $q;
  394. break;
  395. }
  396. }
  397. return $this->rgb2hex(array($r, $g, $b));
  398. }
  399. /**
  400. * Allocates a color in the given image.
  401. *
  402. * User defined color specifications get translated into an array of RGB
  403. * values.
  404. *
  405. * @param resource $img Image handle
  406. * @param string|array $color Name or hex string or an RGB array.
  407. * @return resource Image color handle.
  408. * @access public
  409. * @static
  410. * @uses ImageColorAllocate() to allocate the color.
  411. * @uses color2RGB() to parse the color into RGB values.
  412. */
  413. function allocateColor(&$img, $color) {
  414. $color = Image_Color::color2RGB($color);
  415. return ImageColorAllocate($img, $color[0], $color[1], $color[2]);
  416. }
  417. /**
  418. * Convert a named or hex color string to an RGB array. If the color begins
  419. * with the # character it will be treated as a hex value. Everything else
  420. * will be treated as a named color. If the named color is not known, black
  421. * will be returned.
  422. *
  423. * @param string $color
  424. * @return array RGB color
  425. * @access public
  426. * @static
  427. * @author Laurent Laville <pear@laurent-laville.org>
  428. * @uses hex2rgb() to convert colors begining with the # character.
  429. * @uses namedColor2RGB() to convert everything not starting with a #.
  430. */
  431. function color2RGB($color)
  432. {
  433. $c = array();
  434. if ($color{0} == '#') {
  435. $c = Image_Color::hex2rgb($color);
  436. } else {
  437. $c = Image_Color::namedColor2RGB($color);
  438. }
  439. return $c;
  440. }
  441. /**
  442. * Convert a named color to an RGB array. If the color is unknown black
  443. * is returned.
  444. *
  445. * @param string $color Case insensitive color name.
  446. * @return array RGB color array. If the color was unknown, the result
  447. * will be black.
  448. * @access public
  449. * @static
  450. * @author Sebastian Bergmann <sb@sebastian-bergmann.de>
  451. */
  452. function namedColor2RGB($color)
  453. {
  454. static $colornames;
  455. if (!isset($colornames)) {
  456. $colornames = array(
  457. 'aliceblue' => array(240, 248, 255),
  458. 'antiquewhite' => array(250, 235, 215),
  459. 'aqua' => array( 0, 255, 255),
  460. 'aquamarine' => array(127, 255, 212),
  461. 'azure' => array(240, 255, 255),
  462. 'beige' => array(245, 245, 220),
  463. 'bisque' => array(255, 228, 196),
  464. 'black' => array( 0, 0, 0),
  465. 'blanchedalmond' => array(255, 235, 205),
  466. 'blue' => array( 0, 0, 255),
  467. 'blueviolet' => array(138, 43, 226),
  468. 'brown' => array(165, 42, 42),
  469. 'burlywood' => array(222, 184, 135),
  470. 'cadetblue' => array( 95, 158, 160),
  471. 'chartreuse' => array(127, 255, 0),
  472. 'chocolate' => array(210, 105, 30),
  473. 'coral' => array(255, 127, 80),
  474. 'cornflowerblue' => array(100, 149, 237),
  475. 'cornsilk' => array(255, 248, 220),
  476. 'crimson' => array(220, 20, 60),
  477. 'cyan' => array( 0, 255, 255),
  478. 'darkblue' => array( 0, 0, 13),
  479. 'darkcyan' => array( 0, 139, 139),
  480. 'darkgoldenrod' => array(184, 134, 11),
  481. 'darkgray' => array(169, 169, 169),
  482. 'darkgreen' => array( 0, 100, 0),
  483. 'darkkhaki' => array(189, 183, 107),
  484. 'darkmagenta' => array(139, 0, 139),
  485. 'darkolivegreen' => array( 85, 107, 47),
  486. 'darkorange' => array(255, 140, 0),
  487. 'darkorchid' => array(153, 50, 204),
  488. 'darkred' => array(139, 0, 0),
  489. 'darksalmon' => array(233, 150, 122),
  490. 'darkseagreen' => array(143, 188, 143),
  491. 'darkslateblue' => array( 72, 61, 139),
  492. 'darkslategray' => array( 47, 79, 79),
  493. 'darkturquoise' => array( 0, 206, 209),
  494. 'darkviolet' => array(148, 0, 211),
  495. 'deeppink' => array(255, 20, 147),
  496. 'deepskyblue' => array( 0, 191, 255),
  497. 'dimgray' => array(105, 105, 105),
  498. 'dodgerblue' => array( 30, 144, 255),
  499. 'firebrick' => array(178, 34, 34),
  500. 'floralwhite' => array(255, 250, 240),
  501. 'forestgreen' => array( 34, 139, 34),
  502. 'fuchsia' => array(255, 0, 255),
  503. 'gainsboro' => array(220, 220, 220),
  504. 'ghostwhite' => array(248, 248, 255),
  505. 'gold' => array(255, 215, 0),
  506. 'goldenrod' => array(218, 165, 32),
  507. 'gray' => array(128, 128, 128),
  508. 'green' => array( 0, 128, 0),
  509. 'greenyellow' => array(173, 255, 47),
  510. 'honeydew' => array(240, 255, 240),
  511. 'hotpink' => array(255, 105, 180),
  512. 'indianred' => array(205, 92, 92),
  513. 'indigo' => array(75, 0, 130),
  514. 'ivory' => array(255, 255, 240),
  515. 'khaki' => array(240, 230, 140),
  516. 'lavender' => array(230, 230, 250),
  517. 'lavenderblush' => array(255, 240, 245),
  518. 'lawngreen' => array(124, 252, 0),
  519. 'lemonchiffon' => array(255, 250, 205),
  520. 'lightblue' => array(173, 216, 230),
  521. 'lightcoral' => array(240, 128, 128),
  522. 'lightcyan' => array(224, 255, 255),
  523. 'lightgoldenrodyellow' => array(250, 250, 210),
  524. 'lightgreen' => array(144, 238, 144),
  525. 'lightgrey' => array(211, 211, 211),
  526. 'lightpink' => array(255, 182, 193),
  527. 'lightsalmon' => array(255, 160, 122),
  528. 'lightseagreen' => array( 32, 178, 170),
  529. 'lightskyblue' => array(135, 206, 250),
  530. 'lightslategray' => array(119, 136, 153),
  531. 'lightsteelblue' => array(176, 196, 222),
  532. 'lightyellow' => array(255, 255, 224),
  533. 'lime' => array( 0, 255, 0),
  534. 'limegreen' => array( 50, 205, 50),
  535. 'linen' => array(250, 240, 230),
  536. 'magenta' => array(255, 0, 255),
  537. 'maroon' => array(128, 0, 0),
  538. 'mediumaquamarine' => array(102, 205, 170),
  539. 'mediumblue' => array( 0, 0, 205),
  540. 'mediumorchid' => array(186, 85, 211),
  541. 'mediumpurple' => array(147, 112, 219),
  542. 'mediumseagreen' => array( 60, 179, 113),
  543. 'mediumslateblue' => array(123, 104, 238),
  544. 'mediumspringgreen' => array( 0, 250, 154),
  545. 'mediumturquoise' => array(72, 209, 204),
  546. 'mediumvioletred' => array(199, 21, 133),
  547. 'midnightblue' => array( 25, 25, 112),
  548. 'mintcream' => array(245, 255, 250),
  549. 'mistyrose' => array(255, 228, 225),
  550. 'moccasin' => array(255, 228, 181),
  551. 'navajowhite' => array(255, 222, 173),
  552. 'navy' => array( 0, 0, 128),
  553. 'oldlace' => array(253, 245, 230),
  554. 'olive' => array(128, 128, 0),
  555. 'olivedrab' => array(107, 142, 35),
  556. 'orange' => array(255, 165, 0),
  557. 'orangered' => array(255, 69, 0),
  558. 'orchid' => array(218, 112, 214),
  559. 'palegoldenrod' => array(238, 232, 170),
  560. 'palegreen' => array(152, 251, 152),
  561. 'paleturquoise' => array(175, 238, 238),
  562. 'palevioletred' => array(219, 112, 147),
  563. 'papayawhip' => array(255, 239, 213),
  564. 'peachpuff' => array(255, 218, 185),
  565. 'peru' => array(205, 133, 63),
  566. 'pink' => array(255, 192, 203),
  567. 'plum' => array(221, 160, 221),
  568. 'powderblue' => array(176, 224, 230),
  569. 'purple' => array(128, 0, 128),
  570. 'red' => array(255, 0, 0),
  571. 'rosybrown' => array(188, 143, 143),
  572. 'royalblue' => array( 65, 105, 225),
  573. 'saddlebrown' => array(139, 69, 19),
  574. 'salmon' => array(250, 128, 114),
  575. 'sandybrown' => array(244, 164, 96),
  576. 'seagreen' => array( 46, 139, 87),
  577. 'seashell' => array(255, 245, 238),
  578. 'sienna' => array(160, 82, 45),
  579. 'silver' => array(192, 192, 192),
  580. 'skyblue' => array(135, 206, 235),
  581. 'slateblue' => array(106, 90, 205),
  582. 'slategray' => array(112, 128, 144),
  583. 'snow' => array(255, 250, 250),
  584. 'springgreen' => array( 0, 255, 127),
  585. 'steelblue' => array( 70, 130, 180),
  586. 'tan' => array(210, 180, 140),
  587. 'teal' => array( 0, 128, 128),
  588. 'thistle' => array(216, 191, 216),
  589. 'tomato' => array(255, 99, 71),
  590. 'turquoise' => array( 64, 224, 208),
  591. 'violet' => array(238, 130, 238),
  592. 'wheat' => array(245, 222, 179),
  593. 'white' => array(255, 255, 255),
  594. 'whitesmoke' => array(245, 245, 245),
  595. 'yellow' => array(255, 255, 0),
  596. 'yellowgreen' => array(154, 205, 50)
  597. );
  598. }
  599. $color = strtolower($color);
  600. if (isset($colornames[$color])) {
  601. return $colornames[$color];
  602. } else {
  603. return array(0, 0, 0);
  604. }
  605. }
  606. /**
  607. * Convert an RGB percentage string into an RGB array.
  608. *
  609. * @param string $color Percentage color string like "50%,20%,100%".
  610. * @return array RGB color array.
  611. * @access public
  612. * @static
  613. */
  614. function percentageColor2RGB($color)
  615. {
  616. // remove spaces
  617. $color = str_replace(' ', '', $color);
  618. // remove the percent signs
  619. $color = str_replace('%', '', $color);
  620. // split the string by commas
  621. $color = explode(',', $color);
  622. $ret = array();
  623. foreach ($color as $k => $v) {
  624. // range checks
  625. if ($v <= 0) {
  626. $ret[$k] = 0;
  627. } else if ($v <= 100) {
  628. // add 0.5 then cast to an integer to round the value.
  629. $ret[$k] = (integer) ((2.55 * $v) + 0.5);
  630. } else {
  631. $ret[$k] = 255;
  632. }
  633. }
  634. return $ret;
  635. }
  636. }
  637. // For Array Walk
  638. // {{{
  639. /**
  640. * Function for array_walk() to round colors to the closest web safe value.
  641. *
  642. * @param integer $color One channel of an RGB color.
  643. * @return integer The websafe equivalent of the color channel.
  644. * @author Jason Lotito <jason@lehighweb.com>
  645. * @author Andrew Morton <drewish@katherinehouse.com>
  646. * @access private
  647. * @static
  648. */
  649. function _makeWebSafe(&$color)
  650. {
  651. if ($color < 0x1a) {
  652. $color = 0x00;
  653. } else if ($color < 0x4d) {
  654. $color = 0x33;
  655. } else if ($color < 0x80) {
  656. $color = 0x66;
  657. } else if ($color < 0xB3) {
  658. $color = 0x99;
  659. } else if ($color < 0xE6) {
  660. $color = 0xCC;
  661. } else {
  662. $color = 0xFF;
  663. }
  664. return $color;
  665. }
  666. // }}}
  667. ?>