diff --git a/CharColorConverter.class.php b/CharColorConverter.class.php index ee1665c..62395d6 100644 --- a/CharColorConverter.class.php +++ b/CharColorConverter.class.php @@ -5,6 +5,19 @@ */ class CharColorConverter { + /** + * @var array + */ + protected $colortable; + + /** + * Default constructor. + * @return void + */ + public function __construct() { + $this->buildColorTable(); + } + /** * Returns an array with RGB for the corresponding index. * @param int $index Index from 0 to 1530. (Everything above will result the same) @@ -34,6 +47,17 @@ class CharColorConverter { return array($r, $g, $b); } + /** + * Creates the colortable. + * @return void + */ + protected function buildColorTable() { + $this->colortable = array(); + for($i = 0; $i <= 1275; $i++) { + $this->colortable[$i] = $this->index2rgb($i); + } + } + /** * Converts a single char to a string. * @param string $string @@ -42,7 +66,7 @@ class CharColorConverter { public function charToColor($string) { if(strlen($string) === 0) throw new Exception("Empty string given!"); $index = ord($string) * 5; - $color = $this->index2rgb($index); + $color = $this->colortable[$index]; return $color; } } diff --git a/FileImageConverter.class.php b/FileImageConverter.class.php index 468fdbb..a5a8e17 100644 --- a/FileImageConverter.class.php +++ b/FileImageConverter.class.php @@ -20,7 +20,9 @@ class FileImageConverter { * @return void */ public function __construct() { + echo "\nInitializing color translation table..."; $this->colorConverter = new CharColorConverter(); + echo "done.\n"; } /**