Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Operacje na kolorach - Color 0.3, Klasa, php 5
dr_bonzo
post 3.05.2005, 19:35:54
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


--------------------
Nie lubię jednorożców.
Go to the top of the page
+Quote Post
bigZbig
post 25.01.2006, 15:04:35
Post #2





Grupa: Zarejestrowani
Postów: 740
Pomógł: 15
Dołączył: 23.08.2004
Skąd: Poznań

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


Tak przygladam sie Twojej klasie i nie bardzo moge zrozumiec koncepcje. Aby stworzyc gradient wywolujesz metode obiektu Color i jako parametr przekazujesz jej obiekt bedacy inna instancja tej samej klasy.

Nie lepiej byloby stworzyc osobna klase gradient do ktorej przekazywalbys dwa obiekty klasy Color? To samo tyczy sie metody fadeTo.

Pod wzgledem koncepcyjnym o wiele bardziej podoba mi sie klasa stworzona przez FiDO http://forum.php.pl/index.php?showtopic=21562. Dodajesz kolory do obiektu i pobierasz gradient.


--------------------
bigZbig (Zbigniew Heintze) | blog.heintze.pl
Go to the top of the page
+Quote Post
dr_bonzo
post 25.01.2006, 15:25:56
Post #3





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

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


Cytat
Aby stworzyc gradient wywolujesz metode obiektu Color i jako parametr przekazujesz jej obiekt bedacy inna instancja tej samej klasy.Nie lepiej byloby stworzyc osobna klase gradient do ktorej przekazywalbys dwa obiekty klasy Color?

OK, za malo przemyslalem to rozwiazanie. Dodam nowa klase z Iteratorem po kolorach gradientu.
Dodam podawanie kolowow przez nazwy np. Color::BLACK, Color::RED i opcjonalnie przez stringi "black" "red" -- jesli ktos tak woli.
Gradient z posrednimi stanami -- nawet nie pomyslalem smile.gif >> Color 0.3

Dzieki za uwagi.


--------------------
Nie lubię jednorożców.
Go to the top of the page
+Quote Post
bigZbig
post 3.02.2006, 18:13:05
Post #4





Grupa: Zarejestrowani
Postów: 740
Pomógł: 15
Dołączył: 23.08.2004
Skąd: Poznań

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


@dr_bonzo postanowilem powalczyc troszke z Twoja klasa i wspominanym juz przeze mnie rozwiazaniem FiDO i stworzylem swoja klase o podobnej funkcjonalnosci, ktora niesmialo nazwalem bigColor ;-)


--------------------
bigZbig (Zbigniew Heintze) | blog.heintze.pl
Go to the top of the page
+Quote Post
dr_bonzo
post 3.02.2006, 18:28:29
Post #5





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

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


Fajne, fajne.
Ma wszystko to czego mojej brakowalo.
----edit:
Skoro masz bigColorColection, to nie bylo by lepiej zwracac z gradientu kolekcje kolorow?


--------------------
Nie lubię jednorożców.
Go to the top of the page
+Quote Post
bigZbig
post 7.02.2006, 10:28:56
Post #6





Grupa: Zarejestrowani
Postów: 740
Pomógł: 15
Dołączył: 23.08.2004
Skąd: Poznań

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


Tworzac instancje klasy gradient masz automatycznie do czynienia z obiektem klasy bigColorColection bo bigColorGradient jest rozszerzeniem klasy bigColorColection. Nalezy oczywiscie uzyc metody generate klasy bigColorGradient bo inaczej kolekcja nie bedzie zawierac kolorow posrednich. Nie widze potrzeby wprowadzania dodatkowego posrednika jakim bylby pierwotny obiekt klasy bigColorColection zwracany przez klase generujaca gradient.


--------------------
bigZbig (Zbigniew Heintze) | blog.heintze.pl
Go to the top of the page
+Quote Post
dr_bonzo
post 7.02.2006, 11:07:52
Post #7





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

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


Aha, bo nie zagladalem do kodu, tylko do przykladow uzycia. Ale teraz nie pasuje mi logiczne zlozenie obiektow: to ze gradient dziedziczy po kolekcji, a przeznaczenie ma inne -- on tworzy kolekcje i sam nia jest :|

BTW. Dzisiaj wyjdzie moja wersja klas Kolorowych smile.gif


---------------------
edit: updatowalem pierwszy post.
bigZbig dzieki za uwagi: dodalem klase do robienia Gradientow


--------------------
Nie lubię jednorożców.
Go to the top of the page
+Quote Post
bigZbig
post 7.02.2006, 19:03:45
Post #8





Grupa: Zarejestrowani
Postów: 740
Pomógł: 15
Dołączył: 23.08.2004
Skąd: Poznań

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


Cytat(dr_bonzo @ 2006-02-07 12:07:52)
Ale teraz nie pasuje mi logiczne zlozenie obiektow: to ze gradient dziedziczy po kolekcji, a przeznaczenie ma inne -- on tworzy kolekcje i sam nia jest :|

Hmm faktycznie jest pewien zgrzyt logiczny ale czy aby napewno? Gradient jest kolekcja kolorow. Jesli nie wygenerujesz kolorow posrednich to w sumie tez masz gradient tyle, ze w postaci maksymalnie uproszczonej. Jak na to spojrzec w ten posob to kazda kolekscja jest gradientem. No zapedzilem sie juz w rozwazania filozoficzne, a nie o to przecierz chodzi aby na sile udowadniac swoja racje. Powiem szczerze zrobilem tak aby zaoszczedzic sobie pracy z metodami odpowiedzialnymi za dodawanie koloru do obiektu. Po glebszym zastanowieniu przyznaje Ci racje, ze klase gradient nalerzy raczej traktowac jak narzedzie niz jak pojemnik.

Tymczasem zrobilem maly formularz generujacy kolorowe naglowki.

@dr_bonzo - przyjrze sie Twojemu dzielu


--------------------
bigZbig (Zbigniew Heintze) | blog.heintze.pl
Go to the top of the page
+Quote Post
dr_bonzo
post 7.02.2006, 20:27:40
Post #9





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

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


Przyjzalem sie blizej kodowi:

- [ bigColor]Globale w OOP? Nie lepiej zawrzec to w klasie:
  1. <?php
  2. if (is_array($GLOBALS['aColorsNames']) && !empty($GLOBALS['aColorsNames'])) ...
  3.  
  4. //...
  5. $aColorsNames = array(
  6. "aqua" => "#00FFFF",
  7. "lime" => "#00FF00", ...
  8.  
  9. ?>


- [ bigColorCollection]
  1. <?php
  2. public function addColor($mColor){
  3. if(is_a($mColor, 'bigColor')) {
  4.  
  5. ?>

manual:
Cytat
The is_a() function is deprecated as of php 5 in favor of the instanceof type operator.


* [ bigColorCollection]
  1. <?php
  2. public function addColors($aColors)
  3. {
  4. if (!is_array($aColors))
  5. {
  6. throw new Exception('Wrong format color (array(255,255,255), "255,255,255", "#FF0000", "black")');
  7. }
  8. ?>

chyba troche nie trafiles z komentarzem: $aColors powinno byc tablica bigColor[], a komentarz masz od konstruktora bigColor'u :|

+ Znajdowanie nazwy dla koloru podanego jako liczby/-a
+ Masz wiecej nazwanych kolorow

? Przydalby sie iterator po bigColorCollection, bo na razie jest pojemnikiem na tablice bigColor[], musze wybrac ->getColors() zeby otrzymac tablice i dopiero po miej puscic foreach()

smile.gif
  1. <?php
  2. // upewnienie sie ze wartosci kolorow mieszcza sie w granicach
  3. $color[$j] = ($color[$j] > 255) ? 255 : $color[$j];
  4. $color[$j] = ($color[$j] < 0) ? 0 : $color[$j];
  5. ?>

To nie jest konieczne, gwarantuje ci to matematyka biggrin.gif i algorytm


PS. Zgodnie z regulaminem powinienes zalozyc nowy watek dla swojej klasy biggrin.gif


--------------------
Nie lubię jednorożców.
Go to the top of the page
+Quote Post
bigZbig
post 10.02.2006, 12:40:27
Post #10





Grupa: Zarejestrowani
Postów: 740
Pomógł: 15
Dołączył: 23.08.2004
Skąd: Poznań

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


Zastosowalem sie do wszystkich Twoich rad - za wyjatkiem zalozenia nowego watku ;-)


--------------------
bigZbig (Zbigniew Heintze) | blog.heintze.pl
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Wersja Lo-Fi Aktualny czas: 16.04.2024 - 05:16