Commit 4d99d771 authored by Francois Marier's avatar Francois Marier
Browse files

pear/Image/Color: new upstream version (1.0.4)



The only change is the license. This i now dual license under the
PHP license and the LGPL v2.1 or later.

We do not have any customisations to this file.
Signed-off-by: default avatarFrancois Marier <francois@catalyst.net.nz>
parent 7c508707
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Color.php is the implementation of Image_Color.
*
* PHP versions 4 and 5
*
* LICENSE: This source file is subject to version 3.0 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to license@php.net so we can mail you a copy immediately.
*
* @category Image
* @package Image_Color
* @author Jason Lotito <jason@lehighweb.com>
* @author Andrew Morton <drewish@katherinehouse.com>
* @copyright 2003-2005 The PHP Group
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version CVS: $Id: Color.php,v 1.17 2008/10/26 18:27:10 clockwerx Exp $
* @link http://pear.php.net/package/Image_Color
*/
/**
* Image_Color handles color conversion and mixing.
*
* The class is quick, simple to use, and does its job fairly well but it's got
* some code smells:
* - Call setColors() for some functions but not others.
* - Different functions expect different color formats. setColors() only
* accepts hex while allocateColor() will accept named or hex (provided the
* hex ones start with the # character).
* - Some conversions go in only one direction, ie HSV->RGB but no RGB->HSV.
* I'm going to try to straighten out some of this but I'll be hard to do so
* without breaking backwards compatibility.
*
* @category Image
* @package Image_Color
* @author Jason Lotito <jason@lehighweb.com>
* @author Andrew Morton <drewish@katherinehouse.com>
* @copyright 2003-2005 The PHP Group
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version Release: 0.1.2
* @link http://pear.php.net/package/Image_Color
*/
class Image_Color
{
/**
* First color that the class handles for ranges and mixes.
* @var array
* @access public
* @see setColors()
*/
var $color1 = array();
/**
* Second color that the class handles for ranges and mixes.
* @var array
* @access public
* @see setColors()
*/
var $color2 = array();
/**
* Boolean value for determining whether colors outputted should be limited
* to the web safe pallet or not.
*
* @var boolean
* @access private
* @see setWebSafe()
*/
var $_websafeb = false;
/**
* Mix two colors together by finding their average. If the colors are not
* passed as parameters, the class's colors will be mixed instead.
*
* @param string $col1 The first color you want to mix
* @param string $col2 The second color you want to mix
* @return string The mixed color.
* @access public
* @author Jason Lotito <jason@lehighweb.com>
* @uses _setColors() to assign the colors if any are passed to the
* class.
*/
function mixColors($col1 = false, $col2 = false)
{
if ($col1) {
$this->_setColors($col1, $col2);
}
// after finding the average, it will be a float. add 0.5 and then
// cast to an integer to properly round it to an integer.
$color3[0] = (int) ((($this->color1[0] + $this->color2[0]) / 2) + 0.5);
$color3[1] = (int) ((($this->color1[1] + $this->color2[1]) / 2) + 0.5);
$color3[2] = (int) ((($this->color1[2] + $this->color2[2]) / 2) + 0.5);
if ($this->_websafeb) {
array_walk($color3, '_makeWebSafe');
}
return Image_Color::rgb2hex($color3);
}
/**
* Determines whether colors the returned by this class will be rounded to
* the nearest web safe value.
*
* @param boolean $bool Indicates if colors should be limited to the
* websafe pallet.
* @return void
* @access public
* @author Jason Lotito <jason@lehighweb.com>
*/
function setWebSafe($bool = true)
{
$this->_websafeb = (boolean) $bool;
}
/**
* Set the two colors this class uses for mixing and ranges.
*
* @param string $col1 The first color in hex format
* @param string $col2 The second color in hex format
* @return void
* @access public
* @author Jason Lotito <jason@lehighweb.com>
*/
function setColors($col1, $col2)
{
$this->_setColors($col1, $col2);
}
/**
* Get the range of colors between the class's two colors, given a degree.
*
* @param integer $degrees How large a 'step' we should take between the
* colors.
* @return array Returns an array of hex strings, one element for each
* color.
* @access public
* @author Jason Lotito <jason@lehighweb.com>
* @todo Allow for degrees for individual parts of the colors.
*/
function getRange($degrees = 2)
{
if ($degrees == 0) {
$degrees = 1;
}
// The degrees give us how much we should advance each color at each
// phase of the loop. This way, the advance is equal throughout all
// the colors.
$red_steps = ($this->color2[0] - $this->color1[0]) / $degrees;
$green_steps = ($this->color2[1] - $this->color1[1]) / $degrees;
$blue_steps = ($this->color2[2] - $this->color1[2]) / $degrees;
$allcolors = array();
/**
* The loop stops once any color has gone beyond the end color.
*/
// Loop through all the degrees between the colors
for ($x = 0; $x < $degrees; $x++) {
$col[0] = $red_steps * $x;
$col[1] = $green_steps * $x;
$col[2] = $blue_steps * $x;
// Loop through each R, G, and B
for ($i = 0; $i < 3; $i++) {
$partcolor = $this->color1[$i] + $col[$i];
// If the color is less than 256
if ($partcolor < 256) {
// Makes sure the colors is not less than 0
if ($partcolor > -1) {
$newcolor[$i] = $partcolor;
} else {
$newcolor[$i] = 0;
}
// Color was greater than 255
} else {
$newcolor[$i] = 255;
}
}
if ($this->_websafeb) {
array_walk($newcolor, '_makeWebSafe');
}
$allcolors[] = Image_Color::rgb2hex($newcolor);
}
return $allcolors;
}
/**
* Change the lightness of the class's two colors.
*
* @param integer $degree The degree of the change. Positive values
* lighten the color while negative values will darken it.
* @return void
* @access public
* @author Jason Lotito <jason@lehighweb.com>
* @uses Image_Color::$color1 as an input and return value.
* @uses Image_Color::$color2 as an input and return value.
*/
function changeLightness($degree = 10)
{
$color1 =& $this->color1;
$color2 =& $this->color2;
<?php
/* vim: set expandtab tabstop=4 shiftwidth=4 softtabstop=4: */
/**
* Color.php is the implementation of Image_Color.
*
* PHP versions 4 and 5
*
* This package is released under both the LGPL and PHP licenses.
*
*
* This source file is subject to version 3.0 of the PHP license
* that is available through the world-wide-web at the following URI:
* http://www.php.net/license/3_0.txt. If you did not receive a copy of
* the PHP License and are unable to obtain it through the web, please
* send a note to license@php.net so we can mail you a copy immediately.
*
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 2.1 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*
* @category Image
* @package Image_Color
* @author Jason Lotito <jason@lehighweb.com>
* @author Andrew Morton <drewish@katherinehouse.com>
* @copyright 2003-2005 The PHP Group
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version CVS: $Id: Color.php 299528 2010-05-20 17:45:25Z drewish $
* @link http://pear.php.net/package/Image_Color
*/
/**
* Image_Color handles color conversion and mixing.
*
* The class is quick, simple to use, and does its job fairly well but it's got
* some code smells:
* - Call setColors() for some functions but not others.
* - Different functions expect different color formats. setColors() only
* accepts hex while allocateColor() will accept named or hex (provided the
* hex ones start with the # character).
* - Some conversions go in only one direction, ie HSV->RGB but no RGB->HSV.
* I'm going to try to straighten out some of this but I'll be hard to do so
* without breaking backwards compatibility.
*
* @category Image
* @package Image_Color
* @author Jason Lotito <jason@lehighweb.com>
* @author Andrew Morton <drewish@katherinehouse.com>
* @copyright 2003-2005 The PHP Group
* @license http://www.php.net/license/3_0.txt PHP License 3.0
* @version Release: 0.1.2
* @link http://pear.php.net/package/Image_Color
*/
class Image_Color
{
/**
* First color that the class handles for ranges and mixes.
* @var array
* @access public
* @see setColors()
*/
var $color1 = array();
/**
* Second color that the class handles for ranges and mixes.
* @var array
* @access public
* @see setColors()
*/
var $color2 = array();
/**
* Boolean value for determining whether colors outputted should be limited
* to the web safe pallet or not.
*
* @var boolean
* @access private
* @see setWebSafe()
*/
var $_websafeb = false;
/**
* Mix two colors together by finding their average. If the colors are not
* passed as parameters, the class's colors will be mixed instead.
*
* @param string $col1 The first color you want to mix
* @param string $col2 The second color you want to mix
* @return string The mixed color.
* @access public
* @author Jason Lotito <jason@lehighweb.com>
* @uses _setColors() to assign the colors if any are passed to the
* class.
*/
function mixColors($col1 = false, $col2 = false)
{
if ($col1) {
$this->_setColors($col1, $col2);
}
// after finding the average, it will be a float. add 0.5 and then
// cast to an integer to properly round it to an integer.
$color3[0] = (int) ((($this->color1[0] + $this->color2[0]) / 2) + 0.5);
$color3[1] = (int) ((($this->color1[1] + $this->color2[1]) / 2) + 0.5);
$color3[2] = (int) ((($this->color1[2] + $this->color2[2]) / 2) + 0.5);
if ($this->_websafeb) {
array_walk($color3, '_makeWebSafe');
}
return Image_Color::rgb2hex($color3);
}
/**
* Determines whether colors the returned by this class will be rounded to
* the nearest web safe value.
*
* @param boolean $bool Indicates if colors should be limited to the
* websafe pallet.
* @return void
* @access public
* @author Jason Lotito <jason@lehighweb.com>
*/
function setWebSafe($bool = true)
{
$this->_websafeb = (boolean) $bool;
}
/**
* Set the two colors this class uses for mixing and ranges.
*
* @param string $col1 The first color in hex format
* @param string $col2 The second color in hex format
* @return void
* @access public
* @author Jason Lotito <jason@lehighweb.com>
*/
function setColors($col1, $col2)
{
$this->_setColors($col1, $col2);
}
/**
* Get the range of colors between the class's two colors, given a degree.
*
* @param integer $degrees How large a 'step' we should take between the
* colors.
* @return array Returns an array of hex strings, one element for each
* color.
* @access public
* @author Jason Lotito <jason@lehighweb.com>
* @todo Allow for degrees for individual parts of the colors.
*/
function getRange($degrees = 2)
{
if ($degrees == 0) {
$degrees = 1;
}
// The degrees give us how much we should advance each color at each
// phase of the loop. This way, the advance is equal throughout all
// the colors.
$red_steps = ($this->color2[0] - $this->color1[0]) / $degrees;
$green_steps = ($this->color2[1] - $this->color1[1]) / $degrees;
$blue_steps = ($this->color2[2] - $this->color1[2]) / $degrees;
$allcolors = array();
/**
* The loop stops once any color has gone beyond the end color.
*/
// Loop through all the degrees between the colors
for ($x = 0; $x < $degrees; $x++) {
$col[0] = $red_steps * $x;
$col[1] = $green_steps * $x;
$col[2] = $blue_steps * $x;
// Loop through each R, G, and B
for ($i = 0; $i < 3; $i++) {
$partcolor = $this->color1[$i] + $col[$i];
// If the color is less than 256
if ($partcolor < 256) {
// Makes sure the colors is not less than 0
if ($partcolor > -1) {
$newcolor[$i] = $partcolor;
} else {
$newcolor[$i] = 0;
}
// Color was greater than 255
} else {
$newcolor[$i] = 255;
}
}
if ($this->_websafeb) {
array_walk($newcolor, '_makeWebSafe');
}
$allcolors[] = Image_Color::rgb2hex($newcolor);
}
return $allcolors;
}
/**
* Change the lightness of the class's two colors.
*
* @param integer $degree The degree of the change. Positive values
* lighten the color while negative values will darken it.
* @return void
* @access public
* @author Jason Lotito <jason@lehighweb.com>
* @uses Image_Color::$color1 as an input and return value.
* @uses Image_Color::$color2 as an input and return value.
*/
function changeLightness($degree = 10)
{
$color1 =& $this->color1;
$color2 =& $this->color2;
for ($x = 0; $x < 3; $x++) {
if (isset($color1[$x])) {
if (($color1[$x] + $degree) < 256) {
if (($color1[$x] + $degree) > -1) {
$color1[$x] += $degree;
} else {
$color1[$x] = 0;
}
} else {
$color1[$x] = 255;
if (isset($color1[$x])) {
if (($color1[$x] + $degree) < 256) {
if (($color1[$x] + $degree) > -1) {
$color1[$x] += $degree;
} else {
$color1[$x] = 0;
}
} else {
$color1[$x] = 255;
}
}
if (isset($color2[$x])) {
if (($color2[$x] + $degree) < 256) {
if (($color2[$x] + $degree) > -1) {
$color2[$x] += $degree;
} else {
$color2[$x] = 0;
}
} else {
$color2[$x] = 255;
}
if (isset($color2[$x])) {
if (($color2[$x] + $degree) < 256) {
if (($color2[$x] + $degree) > -1) {
$color2[$x] += $degree;
} else {
$color2[$x] = 0;
}
} else {
$color2[$x] = 255;
}
}
}
}
/**
* Determine if a light or dark text color would be more readable on a
* background of a given color. This is determined by the G(reen) value of
* RGB. You can change the dark and the light colors from their default
* black and white.
*
* @param string $color The hex color to analyze
* @param string $light The light color value to return if we should
* have light text.
* @param string $dark The dark color value to return if we should have
* dark text.
* @return string The light or dark value which would make the text most
* readable.
* @access public
* @static
* @author Jason Lotito <jason@lehighweb.com>
*/
function getTextColor($color, $light = '#FFFFFF', $dark = '#000000')
{
$color = Image_Color::_splitColor($color);
if ($color[1] > hexdec('66')) {
return $dark;
} else {
return $light;
}
}
/**
* Internal method to set the colors.
*
* @param string $col1 First color, either a name or hex value
* @param string $col2 Second color, either a name or hex value
* @return void
* @access private
* @author Jason Lotito <jason@lehighweb.com>
*/
function _setColors($col1, $col2)
{
if ($col1) {
$this->color1 = Image_Color::_splitColor($col1);
}
if ($col2) {
$this->color2 = Image_Color::_splitColor($col2);
}
}
/**
* Given a color, properly split it up into a 3 element RGB array.
*
* @param string $color The color.
* @return array A three element RGB array.
* @access private
* @static
* @author Jason Lotito <jason@lehighweb.com>
*/
function _splitColor($color)
{
$color = str_replace('#', '', $color);
$c[] = hexdec(substr($color, 0, 2));
$c[] = hexdec(substr($color, 2, 2));
$c[] = hexdec(substr($color, 4, 2));
return $c;
}
/**
* This is deprecated. Use rgb2hex() instead.
* @access private
* @deprecated Function deprecated after 1.0.1
* @see rgb2hex().
*/
function _returnColor ( $color )
{
return Image_Color::rgb2hex($color);
}
/**
* Convert an RGB array to a hex string.
*
* @param array $color 3 element RGB array.
* @return string Hex color string.
* @access public
* @static
* @author Jason Lotito <jason@lehighweb.com>
* @see hex2rgb()
*/
function rgb2hex($color)
{
return sprintf('%02X%02X%02X',$color[0],$color[1],$color[2]);
}
/**
* Convert a hex color string into an RGB array. An extra fourth element
* will be returned with the original hex value.
*
* @param string $hex Hex color string.
* @return array RGB color array with an extra 'hex' element containing
* the original hex string.
* @access public
* @static
* @author Jason Lotito <jason@lehighweb.com>
* @see rgb2hex()
*/
function hex2rgb($hex)
{
$return = Image_Color::_splitColor($hex);
$return['hex'] = $hex;
return $return;
}
/**
* Convert an HSV (Hue, Saturation, Brightness) value to RGB.
*
* @param integer $h Hue
* @param integer $s Saturation
* @param integer $v Brightness
* @return array RGB array.
* @access public
* @static
* @author Jason Lotito <jason@lehighweb.com>
* @uses hsv2hex() to convert the HSV value to Hex.
* @uses hex2rgb() to convert the Hex value to RGB.
*/
function hsv2rgb($h, $s, $v)
{
return Image_Color::hex2rgb(Image_Color::hsv2hex($h, $s, $v));