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