Witaj Go¶ciu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Operacje na kolorach - Color 0.3, Klasa, php 5
dr_bonzo
post
Post #1





Grupa: Przyjaciele php.pl
Postów: 5 724
Pomóg³: 259
Do³±czy³: 13.04.2004
Sk±d: N/A

Ostrze¿enie: (0%)
-----


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/
  1. <?php
  2. /**
  3.  * (c) Copyright by dr_bonzo
  4.  * Free to use and modify, just leave the note that i'm the autor of the initial version.
  5.  * 
  6.  * @author dr_bonzo
  7.  * @version 0.3
  8.  * @package Color
  9.  */
  10. class Color
  11. {
  12. /**
  13.  * 
  14.  *
  15.  * @param mixed $mixColorSpecification
  16.  * @throws ColorException 
  17.  */
  18. public function __construct( $mixColorSpecification )
  19. {
  20. try
  21. {
  22. // chain of responsibility?
  23. if ( is_array( $mixColorSpecification ) )
  24. {
  25. $this->setColorFromRGBArray( $mixColorSpecification );
  26. }
  27. elseif ( is_string( $mixColorSpecification ) )
  28. {
  29. if ( preg_match( &#092;"/^#?[a-f0-9]{6,6}$/\", strtolower( $mixColorSpecification ) ) === 1 )
  30. {
  31. $this->setColorFromHexString( $mixColorSpecification );
  32. }
  33. else 
  34. {
  35. $this->setColorFromColorName( $mixColorSpecification );
  36. }
  37. }
  38. else 
  39. {
  40. throw new ColorException( 'Unsupported color specification' );
  41. }
  42. }
  43. catch ( ColorException $e )
  44. {
  45. throw $e;
  46. }
  47. }
  48.  
  49. /**
  50.  * 
  51.  *
  52.  * @param unknown_type $strColorName
  53.  * @throws ColorException
  54.  */
  55. private function setColorFromColorName( $strColorName )
  56. {
  57. if ( array_key_exists( $strColorName, self::$arrPredefinedColorNames ) )
  58. {
  59. $this->setColorFromHexString( self::$arrPredefinedColorNames[ $strColorName ] );
  60. }
  61. else 
  62. {
  63. throw new ColorException( 'Invalid color name: '' . $strColorName . ''' );
  64. }
  65.  
  66. }
  67. /**
  68.  * Creates color from array( $r, $g, $b ) format, where $r is red color value as integer
  69.  *
  70.  * @param array $arrColorSpecification
  71.  * @throws ColorException
  72.  */
  73. private function setColorFromRGBArray( $arrColorSpecification )
  74. {
  75. if ( count( $arrColorSpecification ) !== 3 )
  76. {
  77. throw new ColorException( 'Invalid color specification as array of rgb values' );
  78. }
  79.  
  80. $arrNormalizedColorSpecification = array_values( $arrColorSpecification );
  81. $this->intRed = intval( $arrNormalizedColorSpecification[ 0 ] );
  82. $this->intGreen = intval( $arrNormalizedColorSpecification[ 1 ] );
  83. $this->intBlue = intval( $arrNormalizedColorSpecification[ 2 ] );
  84.  
  85. if ( ! $this->allIngredientRangesAreCorrect() )
  86. {
  87. throw new ColorException( 'Wrong color ingredient specification: invalid range');
  88. }
  89. }
  90.  
  91. /**
  92.  * Creates color from '#rrggbb' or 'rrggbb' format, where 'rr' is red color value in hex.
  93.  *
  94.  * @param string $strHexString
  95.  * @throws ColorException
  96.  */
  97. private function setColorFromHexString( $strHexString )
  98. {
  99. $strColorSpecification = strtolower( strval( $strHexString ) ); // to lower case
  100. $strColorSpecification = substr( $strColorSpecification, -6, 6 ); // remove '#' if exists
  101.  
  102. $this->intRed = hexdec( substr( $strColorSpecification, 0, 2 ) );
  103. $this->intGreen = hexdec( substr( $strColorSpecification, 2, 2 ) );
  104. $this->intBlue = hexdec( substr( $strColorSpecification, 4, 2 ) );
  105.  
  106. if ( ! $this->allIngredientRangesAreCorrect() )
  107. {
  108. throw new ColorException( 'Wrong color ingredient specification: invalid range');
  109. }
  110. }
  111.  
  112. /**
  113.  * Checks whether reg, green and blue values are in correct value ranges
  114.  *
  115.  * @return boolean 
  116.  */
  117. private function allIngredientRangesAreCorrect()
  118. {
  119. $boolRetVal = TRUE;
  120. $boolRetVal &= ( ( $this->intRed >= 0 ) && ( $this->intRed <= 255 ) );
  121. $boolRetVal &= ( ( $this->intGreen >= 0 ) && ( $this->intGreen <= 255 ) );
  122. $boolRetVal &= ( ( $this->intBlue >= 0 ) && ( $this->intBlue <= 255 ) );
  123.  
  124. return $boolRetVal;
  125. }
  126.  
  127. /**
  128.  * Returns red ingredient as integer
  129.  *
  130.  * @return integer
  131.  */
  132. public function getRed()
  133. {
  134. return $this->intRed;
  135. }
  136.  
  137. /**
  138.  * Returns green ingredient as integer
  139.  *
  140.  * @return integer
  141.  */
  142. public function getGreen()
  143. {
  144. return $this->intGreen;
  145. }
  146.  
  147. /**
  148.  * Returns blue ingredient as integer
  149.  *
  150.  * @return integer
  151.  */
  152. public function getBlue()
  153. {
  154. return $this->intBlue;
  155. }
  156.  
  157. /**
  158.  * @return string
  159.  */
  160. public function getRedAsHex()
  161. {
  162. return $this->getHexValueWithLeadingZero( $this->intRed );
  163. }
  164.  
  165. /**
  166.  * @return string
  167.  */
  168. public function getGreenAsHex()
  169. {
  170. return $this->getHexValueWithLeadingZero( $this->intGreen );
  171. }
  172.  
  173. /**
  174.  * @return string
  175.  */
  176. public function getBlueAsHex()
  177. {
  178. return $this->getHexValueWithLeadingZero( $this->intBlue );
  179. }
  180.  
  181.  
  182. private function getHexValueWithLeadingZero( $intNumber )
  183. {
  184. $strRetVal = dechex( $intNumber );
  185. return strlen( $strRetVal ) < 2 ? '0' . $strRetVal : $strRetVal;
  186. }
  187.  
  188. /**
  189.  * Returns color representation as '#rrggbb' or just 'rrggbb'
  190.  *
  191.  * @param boolean $boolWithSharp indicates whether add '#' prefix to color
  192.  * @return string
  193.  */
  194. public function getHTMLValue( $boolWithSharp = TRUE )
  195. {
  196. return ( $boolWithSharp ? '#' : '' ) . implode( '', array( $this->getRedAsHex(), $this->getGreenAsHex(), $this->getBlueAsHex() ) );
  197. }
  198.  
  199. public function getAsIntArray()
  200. {
  201. return array( $this->getRed(), $this->getGreen(), $this->getBlue() );
  202. }
  203.  
  204. /**
  205.  * Returns color being an invertion of current color.
  206.  * New object is returned
  207.  *
  208.  * @return Color
  209.  */
  210. public function getInverted()
  211. {
  212. return new Color( array( 255 - $this->getRed(), 255 - $this->getGreen(), 255 - $this->getBlue() ) );
  213. }
  214.  
  215. private $intRed = -1;
  216. private $intGreen = -1;
  217. private $intBlue = -1;
  218.  
  219. static private $arrPredefinedColorNames = array(
  220. 'black' => '000000', 
  221. 'silver' => 'C0C0C0',
  222. 'gray' => '808080',
  223. 'white' => 'FFFFFF',
  224. 'maroon' => '800000',
  225. 'red' => 'FF0000',
  226. 'purple' => '800080',
  227. 'fuchsia' => 'FF00FF',
  228. 'green' => '00FF00',
  229. 'lime' => '00FF00',
  230. 'olive' => '808000',
  231. 'yellow' => 'FFFF00',
  232. 'navy' => '000080',
  233. 'blue' => '0000FF',
  234. 'teal' => '008080',
  235. 'aqua' => '00FFFF'
  236. );
  237. }
  238.  
  239. class ColorException extends Exception 
  240. {
  241. }
  242. ?>


  1. <?php
  2.  
  3. /**
  4.  * (c) Copyright by dr_bonzo
  5.  * Free to use and modify, just leave the note that i'm the autor of the initial version.
  6.  * 
  7.  * @author dr_bonzo
  8.  * @version 0.3
  9.  * @package Color
  10.  */
  11. class ColorGradientGenerator
  12. {
  13. /**
  14.  * Creates gradient between colors of the ColorGradient object
  15.  *
  16.  * @param Color[] $arrColors
  17.  * @param integer[] $arrIntermediateColorNumbers
  18.  * @return Color[]
  19.  * @throws ColorException
  20.  */
  21. public function createGradient( $arrColors, $arrIntermediateColorNumbers )
  22. {
  23. if ( count( $arrColors ) < 2 )
  24. {
  25. throw new ColorException( 'Illegal number of Colors: ' . count( $arrColors ) );
  26. }
  27.  
  28. if ( count( $arrIntermediateColorNumbers ) !== count( $arrColors ) - 1 )
  29. {
  30. throw new ColorException( 'Illegal number of intermediate color numbers: ' . count( $arrIntermediateColorNumbers ) );
  31. }
  32.  
  33. $arrGradient = array();
  34. $iMax = count( $arrColors ) - 1;
  35.  
  36. for ( $i = 0; $i < $iMax; $i++ )
  37. {
  38. $arrGradient[] = $arrColors[ $i ]; // start color
  39. $arrGradient = array_merge( $arrGradient, $this->createGradientBeetweenColors( $arrColors[ $i ], $arrColors[ $i + 1 ], $arrIntermediateColorNumbers[ $i ] ) );
  40. }
  41.  
  42. $arrGradient[] = $arrColors[ count( $arrColors ) - 1 ]; // last color
  43.  
  44. return $arrGradient;
  45. }
  46.  
  47. /**
  48.  * Creates intermediate colors between two specified colors: startColor and endC
    olor.
  49.  *
  50.  * @param Color $startColor
  51.  * @param Color $endColor
  52.  * @param integer $intIntermediateColors numer of colors between start and 
  53. nd color
  54.  * @return Color[] 
  55.  * @throws ColorException
  56.  */
  57. private function createGradientBeetweenColors( Color $startColor, Color $endColor, $intIntermediateColors )
  58. {
  59. if ( $intIntermediateColors < 0 )
  60. {
  61. throw new ColorException( 'Illegal intIntermediateColors: ' . $intIntermediateColors );
  62. }
  63. $flRedInterval = ( $endColor->getRed() - $startColor->getRed() ) / ( $intIntermediateColors + 1 );
  64. $flGreenInterval = ( $endColor->getGreen() - $startColor->getGreen() ) / ( $intIntermediateColors + 1 );
  65. $flBlueInterval = ( $endColor->getBlue() - $startColor->getBlue() ) / ( $intIntermediateColors + 1 );
  66.  
  67. $flRed = floatval( $startColor->getRed() );
  68. $flGreen = floatval( $startColor->getGreen() );
  69. $flBlue = floatval( $startColor->getBlue() );
  70.  
  71. $arrGradient = array();
  72.  
  73. for ( $i = 0; $i < $intIntermediateColors; $i++ )
  74. {
  75. $flRed += $flRedInterval;
  76. $flGreen += $flGreenInterval;
  77. $flBlue += $flBlueInterval;
  78.  
  79. $c = new Color( array( round( $flRed ), round( $flGreen ), round( $flBlue ) ) );
  80. $arrGradient[] = $c;
  81. }
  82.  
  83. return $arrGradient;
  84. }
  85. }
  86. ?>


Ten post edytowa³ dr_bonzo 7.02.2006, 13:37:46
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 U¿ytkowników czyta ten temat (2 Go¶ci i 0 Anonimowych u¿ytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 18.09.2025 - 07:01