1<?php 2/* vim: set expandtab tabstop=4 shiftwidth=4: */ 3// +----------------------------------------------------------------------+ 4// | PHP Version 4 | 5// +----------------------------------------------------------------------+ 6// | Copyright (c) 1997-2003 The PHP Group | 7// +----------------------------------------------------------------------+ 8// | This source file is subject to version 2.02 of the PHP license, | 9// | that is bundled with this package in the file LICENSE, and is | 10// | available at through the world-wide-web at | 11// | http://www.php.net/license/2_02.txt. | 12// | If you did not receive a copy of the PHP license and are unable to | 13// | obtain it through the world-wide-web, please send a note to | 14// | license@php.net so we can mail you a copy immediately. | 15// +----------------------------------------------------------------------+ 16// | Author: Stefan Neufeind <pear.neufeind@speedpartner.de> | 17// +----------------------------------------------------------------------+ 18// 19// $Id: Color.php 287471 2009-08-18 23:12:01Z clockwerx $ 20 21/** 22* Class for color-handling 23* 24* @author Stefan Neufeind <pear.neufeind@speedpartner.de> 25* @package Image_Canvas 26* @category images 27* @license The PHP License, version 2.02 28*/ 29 30/** 31* Color class to be extended; from package PEAR::Image_Color 32*/ 33require_once 'Image/Color.php'; 34 35/** 36* Class for color-handling 37* 38* This is used to extend the functionality of the current PEAR::Image_Color v0.4. 39* I hope to be allowed to incorporate some of the improvements in a new Image_Color release. 40* 41* @author Stefan Neufeind <pear.neufeind@speedpartner.de> 42* @package Image_Canvas 43* @access public 44*/ 45class Image_Canvas_Color extends Image_Color 46{ 47 /** 48 * Allocates a color in the given image. 49 * 50 * Userdefined color specifications get translated into 51 * an array of rgb values. 52 * 53 * @param resource GD-resource 54 * @param mixed any color representation supported by color2RGB() 55 * @return resource Image color handle 56 * @see color2RGB() 57 * @access public 58 * @static 59 */ 60 function allocateColor(&$img, $color) 61 { 62 $color = Image_Canvas_Color::color2RGB($color); 63 64 if (($color[3] == 255) || (!function_exists("imagecolorallocatealpha"))) { 65 return imagecolorallocate($img, $color[0], $color[1], $color[2]); 66 } else { 67 return imagecolorallocatealpha($img, $color[0], $color[1], $color[2], 127-round(($color[3]*127)/255)); 68 } 69 } 70 71 /** 72 * Convert any color-representation into an array of 4 ints (RGBA). 73 * 74 * Userdefined color specifications get translated into 75 * an array of rgb values. 76 * 77 * @param mixed any color representation supported by Image_Canvas_Color::color2RGB() 78 * @return array Array of 4 ints (RGBA-representation) 79 * @access public 80 * @static 81 */ 82 function color2RGB($color) 83 { 84 if (is_array($color)) { 85 if (!is_numeric($color[0])) { 86 return null; // error 87 } 88 if (count($color) == 3) { // assume RGB-color 89 90 // 255 = alpha-value; full opaque 91 return array((int) $color[0], 92 (int) $color[1], 93 (int) $color[2], 94 255); 95 } 96 if (count($color) == 4) { // assume RGBA-color 97 98 // 255 = alpha-value; full opaque 99 return array((int) $color[0], 100 (int) $color[1], 101 (int) $color[2], 102 (int) $color[3]); 103 } 104 return null; // error 105 } elseif (is_string($color)) { 106 $alphaPos = strpos($color, '@'); 107 if ($alphaPos === false) { 108 $alpha = 255; 109 } else { 110 $alphaFloat = (float) substr($color, $alphaPos+1); 111 // restrict to range 0..1 112 $alphaFloat = max(min($alphaFloat, 1), 0); 113 $alpha = (int) round((float) 255 * $alphaFloat); 114 $color = substr($color, 0, $alphaPos); 115 } 116 if ($color[0] == '#') { // hex-color given, e.g. #FFB4B4 117 $tempColor = parent::hex2rgb($color); 118 return array((int) $tempColor[0], 119 (int) $tempColor[1], 120 (int) $tempColor[2], 121 $alpha); 122 } 123 if (strpos($color,'%') !== false) { 124 $tempColor = parent::percentageColor2RGB($color); 125 return array((int) $tempColor[0], 126 (int) $tempColor[1], 127 (int) $tempColor[2], 128 $alpha); 129 } else { 130 $tempColor = parent::namedColor2RGB($color); 131 return array((int) $tempColor[0], 132 (int) $tempColor[1], 133 (int) $tempColor[2], 134 $alpha); 135 } 136 } else { 137 return null; // error 138 } 139 } 140 141 /** 142 * getRange 143 * Given a degree, you can get the range of colors between one color and 144 * another color. 145 * 146 * @access public 147 * @param string How much each 'step' between the colors we should take. 148 * @return array Returns an array of all the colors, one element for each color. 149 */ 150 function getRange ($degrees) 151 { 152 $tempColors = parent::getRange($degrees); 153 154 // now add alpha-channel information 155 $steps = count($tempColors); 156 for($counter=0;$counter<$steps;$counter++) { 157 $tempColors[$counter] = parent::hex2rgb($tempColors[$counter]); 158 unset($tempColors[$counter]['hex']); 159 $tempColors[$counter][3] = (int) round( 160 (((float) $this->color1[3]*($steps-$counter))+ 161 ((float) $this->color2[3]*($counter)) 162 ) / $steps 163 ); 164 } 165 166 return $tempColors; 167 } 168 169 /** 170 * Internal method to correctly set the colors. 171 * 172 * @param mixed color 1 173 * @param mixed color 2 174 * @access private 175 */ 176 function _setColors ( $col1, $col2 ) 177 { 178 $this->color1 = Image_Canvas_Color::color2RGB($col1); 179 $this->color2 = Image_Canvas_Color::color2RGB($col2); 180 } 181} 182?>