---------------
W paczce takze UnitTesty (SimpleTest) dla obu klas.
Co mozna nia zrobic?
- przejscie tonalne miedzy dwoma kolorami,
- inwersje koloru
- pobranie wartosci koloru jako tablice intow, lub wersji HTML dla kazdej skladowej osobno lub dla calego koloru,
- (brakuje fadeTo)
wymaga php5
http://northslope.lap.pl/dev/Color/
<?php /** * (c) Copyright by dr_bonzo * Free to use and modify, just leave the note that i'm the autor of the initial version. * * @author dr_bonzo * @version 0.3 * @package Color */ class Color { /** * * * @param mixed $mixColorSpecification * @throws ColorException */ public function __construct( $mixColorSpecification ) { try { // chain of responsibility? { $this->setColorFromRGBArray( $mixColorSpecification ); } { { $this->setColorFromHexString( $mixColorSpecification ); } else { $this->setColorFromColorName( $mixColorSpecification ); } } else { throw new ColorException( 'Unsupported color specification' ); } } catch ( ColorException $e ) { throw $e; } } /** * * * @param unknown_type $strColorName * @throws ColorException */ private function setColorFromColorName( $strColorName ) { { $this->setColorFromHexString( self::$arrPredefinedColorNames[ $strColorName ] ); } else { throw new ColorException( 'Invalid color name: '' . $strColorName . ''' ); } } /** * Creates color from array( $r, $g, $b ) format, where $r is red color value as integer * * @param array $arrColorSpecification * @throws ColorException */ private function setColorFromRGBArray( $arrColorSpecification ) { { throw new ColorException( 'Invalid color specification as array of rgb values' ); } if ( ! $this->allIngredientRangesAreCorrect() ) { throw new ColorException( 'Wrong color ingredient specification: invalid range'); } } /** * Creates color from '#rrggbb' or 'rrggbb' format, where 'rr' is red color value in hex. * * @param string $strHexString * @throws ColorException */ private function setColorFromHexString( $strHexString ) { if ( ! $this->allIngredientRangesAreCorrect() ) { throw new ColorException( 'Wrong color ingredient specification: invalid range'); } } /** * Checks whether reg, green and blue values are in correct value ranges * * @return boolean */ private function allIngredientRangesAreCorrect() { $boolRetVal = TRUE; $boolRetVal &= ( ( $this->intRed >= 0 ) && ( $this->intRed <= 255 ) ); $boolRetVal &= ( ( $this->intGreen >= 0 ) && ( $this->intGreen <= 255 ) ); $boolRetVal &= ( ( $this->intBlue >= 0 ) && ( $this->intBlue <= 255 ) ); return $boolRetVal; } /** * Returns red ingredient as integer * * @return integer */ public function getRed() { return $this->intRed; } /** * Returns green ingredient as integer * * @return integer */ public function getGreen() { return $this->intGreen; } /** * Returns blue ingredient as integer * * @return integer */ public function getBlue() { return $this->intBlue; } /** * @return string */ public function getRedAsHex() { return $this->getHexValueWithLeadingZero( $this->intRed ); } /** * @return string */ public function getGreenAsHex() { return $this->getHexValueWithLeadingZero( $this->intGreen ); } /** * @return string */ public function getBlueAsHex() { return $this->getHexValueWithLeadingZero( $this->intBlue ); } private function getHexValueWithLeadingZero( $intNumber ) { } /** * Returns color representation as '#rrggbb' or just 'rrggbb' * * @param boolean $boolWithSharp indicates whether add '#' prefix to color * @return string */ public function getHTMLValue( $boolWithSharp = TRUE ) { } public function getAsIntArray() { } /** * Returns color being an invertion of current color. * New object is returned * * @return Color */ public function getInverted() { return new Color( array( 255 - $this->getRed(), 255 - $this->getGreen(), 255 - $this->getBlue() ) ); } private $intRed = -1; private $intGreen = -1; private $intBlue = -1; 'black' => '000000', 'silver' => 'C0C0C0', 'gray' => '808080', 'white' => 'FFFFFF', 'maroon' => '800000', 'red' => 'FF0000', 'purple' => '800080', 'fuchsia' => 'FF00FF', 'green' => '00FF00', 'lime' => '00FF00', 'olive' => '808000', 'yellow' => 'FFFF00', 'navy' => '000080', 'blue' => '0000FF', 'teal' => '008080', 'aqua' => '00FFFF' ); } class ColorException extends Exception { } ?>
<?php /** * (c) Copyright by dr_bonzo * Free to use and modify, just leave the note that i'm the autor of the initial version. * * @author dr_bonzo * @version 0.3 * @package Color */ class ColorGradientGenerator { /** * Creates gradient between colors of the ColorGradient object * * @param Color[] $arrColors * @param integer[] $arrIntermediateColorNumbers * @return Color[] * @throws ColorException */ public function createGradient( $arrColors, $arrIntermediateColorNumbers ) { { } { throw new ColorException( 'Illegal number of intermediate color numbers: ' . count( $arrIntermediateColorNumbers ) ); } for ( $i = 0; $i < $iMax; $i++ ) { $arrGradient[] = $arrColors[ $i ]; // start color $arrGradient = array_merge( $arrGradient, $this->createGradientBeetweenColors( $arrColors[ $i ], $arrColors[ $i + 1 ], $arrIntermediateColorNumbers[ $i ] ) ); } return $arrGradient; } /** * Creates intermediate colors between two specified colors: startColor and endC
olor. * * @param Color $startColor * @param Color $endColor * @param integer $intIntermediateColors numer of colors between start and nd color * @return Color[] * @throws ColorException */ private function createGradientBeetweenColors( Color $startColor, Color $endColor, $intIntermediateColors ) { if ( $intIntermediateColors < 0 ) { throw new ColorException( 'Illegal intIntermediateColors: ' . $intIntermediateColors ); } $flRedInterval = ( $endColor->getRed() - $startColor->getRed() ) / ( $intIntermediateColors + 1 ); $flGreenInterval = ( $endColor->getGreen() - $startColor->getGreen() ) / ( $intIntermediateColors + 1 ); $flBlueInterval = ( $endColor->getBlue() - $startColor->getBlue() ) / ( $intIntermediateColors + 1 ); for ( $i = 0; $i < $intIntermediateColors; $i++ ) { $flRed += $flRedInterval; $flGreen += $flGreenInterval; $flBlue += $flBlueInterval; $arrGradient[] = $c; } return $arrGradient; } } ?>