Update 2
---------------
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?
if ( is_array( $mixColorSpecification ) ) {
$this->setColorFromRGBArray( $mixColorSpecification );
}
elseif ( is_string( $mixColorSpecification ) ) {
if ( preg_match( \"/^#?[a-f0-9]{6,6}$/\", strtolower( $mixColorSpecification ) ) === 1 ) {
$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 )
{
if ( count( $arrColorSpecification ) !== 3
) {
throw new ColorException( 'Invalid color specification as array of rgb values' );
}
$arrNormalizedColorSpecification = array_values( $arrColorSpecification ); $this->intRed = intval( $arrNormalizedColorSpecification[ 0
] ); $this->intGreen = intval( $arrNormalizedColorSpecification[ 1
] ); $this->intBlue = intval( $arrNormalizedColorSpecification[ 2
] );
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 )
{
$strColorSpecification = strtolower( strval( $strHexString ) ); // to lower case $strColorSpecification = substr( $strColorSpecification, -6
, 6
); // remove '#' if exists
$this->intRed = hexdec( substr( $strColorSpecification, 0
, 2
) ); $this->intGreen = hexdec( substr( $strColorSpecification, 2
, 2
) ); $this->intBlue = hexdec( substr( $strColorSpecification, 4
, 2
) );
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 )
{
$strRetVal = dechex( $intNumber ); return strlen( $strRetVal ) < 2 ?
'0' . $strRetVal : $strRetVal; }
/**
* Returns color representation as '#rrggbb' or just 'rrggbb'
*
* @param boolean $boolWithSharp indicates whether add '#' prefix to color
* @return string
*/
public function getHTMLValue( $boolWithSharp = TRUE )
{
return ( $boolWithSharp ?
'#' : '' ) . implode( '', array( $this->getRedAsHex(), $this->getGreenAsHex(), $this->getBlueAsHex() ) ); }
public function getAsIntArray()
{
return array( $this->getRed(), $this->getGreen(), $this->getBlue() ); }
/**
* 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 )
{
if ( count( $arrColors ) < 2
) {
throw
new ColorException
( 'Illegal number of Colors: ' . count( $arrColors ) ); }
if ( count( $arrIntermediateColorNumbers ) !== count( $arrColors ) - 1
) {
throw
new ColorException
( 'Illegal number of intermediate color numbers: ' . count( $arrIntermediateColorNumbers ) ); }
$iMax = count( $arrColors ) - 1;
for ( $i = 0; $i < $iMax; $i++ )
{
$arrGradient[] = $arrColors[ $i ]; // start color
$arrGradient = array_merge( $arrGradient, $this->createGradientBeetweenColors( $arrColors[ $i ], $arrColors[ $i + 1
], $arrIntermediateColorNumbers[ $i ] ) ); }
$arrGradient[] = $arrColors[ count( $arrColors ) - 1
]; // last color
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 );
$flRed = floatval( $startColor->getRed() ); $flGreen = floatval( $startColor->getGreen() ); $flBlue = floatval( $startColor->getBlue() );
for ( $i = 0; $i < $intIntermediateColors; $i++ )
{
$flRed += $flRedInterval;
$flGreen += $flGreenInterval;
$flBlue += $flBlueInterval;
$arrGradient[] = $c;
}
return $arrGradient;
}
}
?>
Ten post edytowa³ dr_bonzo 7.02.2006, 13:37:46