From 417f7a54f523f54d098fe4a77c6eac671593dfa1 Mon Sep 17 00:00:00 2001 From: Jan Philipp Timme Date: Tue, 4 Jan 2011 19:25:30 +0000 Subject: [PATCH] [TASK] Improved performance by creating a lookup-table for the colorindex to rgb function. --- CharColorConverter.class.php | 26 +++++++++++++++++++++++++- FileImageConverter.class.php | 2 ++ 2 files changed, 27 insertions(+), 1 deletion(-) 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"; } /**