FW: [PEAR-DEV] Color class ?

From: Date: Tue, 26 Feb 2002 11:05:06 +0000
Subject: FW: [PEAR-DEV] Color class ?
Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-4882@lists.php.net to get a copy of this message
Cc:ed to wrong address... :( -- Richard Heyes "If you have any trouble sounding condescending, find a Unix user to show you how it's done." - Scott Adams > -----Original Message----- > From: Richard Heyes [mailto:richard@phpguru.org] > Sent: 26 February 2002 10:49 > To: Bertrand Mansion > Cc: pear-dev@php.net > Subject: RE: [PEAR-DEV] Color class ? > > > > Is anyone working on a color class or should I make one myself ? > > I need one urgently that's why I ask, but I think I heard someone > > saying he > > made one recently. If he needs help, he can still send me the > > code so I can > > have a look and see if I can help. > > Someone posted this one or a link to it a while back. > > -- > Richard Heyes > "If you have any trouble sounding condescending, > find a Unix user to show you how it's done." - Scott Adams

<?php /* // +------------------------------------------------------------------------+ // | Maintained by Jason Lotito <jason@lehighweb.com> | // +------------------------------------------------------------------------+ // | This library is free software; you can redistribute it and/or | // | modify it under the terms of the GNU Lesser General Public | // | License as published by the Free Software Foundation; either | // | version 2.1 of the License, or (at your option) any later version. | // | | // | This library 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 | // | Lesser General Public License for more details. | // | | // | You should have received a copy of the GNU Lesser General Public | // | License along with this library; if not, write to the Free Software | // | Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA| // +------------------------------------------------------------------------+ // Copyright (C) 2001 Jason Lotito <jason@lehighweb.com> // +------------------------------------------------------------------------+ // // Color Class // */ if ( empty($x) ) $x = 10; $number = $x; echo ' <html> <head><title>Color Class Test</title></head> <body bgcolor="white"> <table cellspacing="0" cellpadding="2" width="400" border="0" align="center"> <caption><b>Color Ranges: FF0000 to 0000FF With Hue Change</b></caption> '; $color = new Color; $color->setColors("FFFFFF", "F28580"); echo "\t\t</tr>\n"; $i = 1; for ( $x = 1; $x <= 25; $x++ ) { $colors = $color->getRange($number); echo "\t\t<tr>\n"; foreach ( $colors as $key => $col ) { //if ( $key % 5 == 0 ) echo "\t\t\t".'<td bgcolor="'.$col.'"><font color="#'.Color::getTextColor($col).'">'.$col.'</font></td>'."\n"; $i++; } echo "\t\t</tr>\n"; $color->changeHue(-10); } $color->setWebSafe(); $color->setColors("FF0000", "0000FF"); echo '<tr><td colspan="'.$number.'">Turning On Web Color Safety</td></tr>'; for ( $x = 1; $x <= 25; $x++ ) { $colors = $color->getRange($number); echo "\t\t<tr>\n"; foreach ( $colors as $key => $col ) { //if ( $key % 5 == 0 ) echo "\t\t\t".'<td bgcolor="'.$col.'"><font color="#'.Color::getTextColor($col).'">'.$col.'</font></td>'."\n"; $i++; } echo "\t\t</tr>\n"; $color->changeHue(-10); } $bgcolor = $color->mixColors('336699', 'ffffff'); echo ' </table> <table cellspacing="0" cellpadding="2" width="400" height="50" border="1" align="center"> <caption><b>Color Mix: 336699 AND FFFFFF w/ Web Safe Colors</b></caption> <tr> <td bgcolor="#336699">336699</td> </tr> <tr> <td bgcolor="#'.$bgcolor.'"><font color="#'.Color::getTextColor($bgcolor).'">'.$bgcolor.'</font></td> </tr> <tr> <td bgcolor="#FFFFFF">FFFFFF</td> </tr> </table> '; $color->setWebSafe(false); $bgcolor = $color->mixColors('336699', 'ffffff'); echo ' </table> <table cellspacing="0" cellpadding="2" width="400" height="50" border="1" align="center"> <caption><b>Color Mix: 336699 AND FFFFFF w/o Web Safe Colors</b></caption> <tr> <td bgcolor="#336699">336699</td> </tr> <tr> <td bgcolor="#'.$bgcolor.'"><font color="#'.Color::getTextColor($bgcolor).'">'.$bgcolor.'</font></td> </tr> <tr> <td bgcolor="#FFFFFF">FFFFFF</td> </tr> </table> '; $rgb = Color::hex2rgb("336699"); echo "<h1>hex2rgb()</h1>"; echo '<pre>'; print_r($rgb); echo '</pre>'; $rgb = Color::rgb2hex($rgb); echo "<h1>rgb2hex()</h1>"; echo '<pre>'; print_r($rgb); echo '</pre>'; echo ' </body> </html> '; class Color { var $color1 = array(); var $color2 = array(); var $_websafeb = false; var $websafe = array( '00' => '00', '33' => '51', '66' => '102', '99' => '153', 'cc' => '204', 'ff' => '255'); function mixColors ( $col1=false, $col2=false ) { if ( $col1 ) { $this->_setColors($col1, $col2); } // RED $color3[0] = ( $this->color1[0] + $this->color2[0] ) / 2; // GREEN $color3[1] = ( $this->color1[1] + $this->color2[1] ) / 2; // BLUE $color3[2] = ( $this->color1[2] + $this->color2[2] ) / 2; if ( $this->_websafeb ) { array_walk( $color3, '_makeWebSafe' ); } return $this->_returnColor( $color3 ); } function setWebSafe( $bool=true ) { $this->_websafeb = $bool; } function setColors( $col1, $col2 ) { $this->_setColors($col1, $col2); } function getRange ( $degrees=2 ) { //$degrees--; // Correctly accounts for total colors. $color2 =& $this->color2; $color1 =& $this->color1; if ( $degrees == 0 ) $degrees = 1; /** The steps 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 $red_steps = ( $color2[0] - $color1[0] ) / $degrees; // GREEN $green_steps = ( $color2[1] - $color1[1] ) / $degrees; // BLUE $blue_steps = ( $color2[2] - $color1[2] ) / $degrees; $allcolors = array(); $x = 0; /** 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 = $color1[$i] + $col[$i]; //echo "PARTCOLOR: $i -> $partcolor<br />"; // 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[] = $this->_returnColor($newcolor); //echo "NEW COLOR: ".$this->_returnColor($newcolor)."<br /><br />"; } //$allcolors[] = $this->_returnColor($color2); return $allcolors; } function changeHue ( $degree=10 ) { $color2 =& $this->color2; $color1 =& $this->color1; for ( $x = 0; $x < 3; $x++ ) { if ( ( $color1[$x] + $degree ) < 256 ) { if ( ( $color1[$x] + $degree ) > -1 ) { $color1[$x] += $degree; } else { $color1[$x] = 0; } } else { $color1[$x] = 255; } } for ( $x = 0; $x < 3; $x++ ) { if ( ( $color2[$x] + $degree ) < 256 ) { if ( ( $color2[$x] + $degree ) > -1 ) { $color2[$x] += $degree; } else { $color2[$x] = 0; } } else { $color2[$x] = 255; } } } function getTextColor ( $color, $light='FFFFFF', $dark='000000' ) { $color = Color::_splitColor($color); if ( $color[1] > hexdec('66') ) { return $dark; } else { return $light; } } function _color2dec ( $color ) { return Color::_splitColor($color); } function _setColors ( $col1, $col2 ) { $this->color1 = Color::_splitColor($col1); $this->color2 = Color::_splitColor($col2); } function _splitColor ( $color ) { $c[] = hexdec( substr( $color, 0, 2 ) ); $c[] = hexdec( substr( $color, 2, 2 ) ); $c[] = hexdec( substr( $color, 4, 2 ) ); return $c; } function _returnColor ( $color ) { return sprintf("%02X%02X%02X",$color[0],$color[1],$color[2]); } function rgb2hex ( $color ) { return Color::_returnColor( $color ); } function hex2rgb ( $hex ) { $return = Color::_splitColor( $hex ); $return['hex'] = $hex; return $return; } } // For Array Walk // {{{ function _makeWebSafe ( &$color ) { if ( $color == 0 ) { return $color; } else { if ( ($color % 51) == 0 ) { return $color; } else { if ( $color < 26 ) { $color = 00; return $color; } else if ( $color < 77 && $color > 25 ) { $color = 51; return $color; } else if ( $color > 76 && $color < 127 ) { $color = 102; return $color; } else if ( $color > 126 && $color < 178 ) { $color = 153; return $color; } else if ( $color > 177 && $color < 229 ) { $color = 204; return $color; } else { $color = 255; return $color; } } } } // }}} /* * @tabsize 4 SPACES */ ?>
« previous php.pear.dev (#4882) next »