[TASK] Improved performance by creating a lookup-table for the colorindex to rgb function.

This commit is contained in:
Jan Philipp Timme 2011-01-04 19:25:30 +00:00
parent b74c814e3f
commit 417f7a54f5
2 changed files with 27 additions and 1 deletions

View File

@ -5,6 +5,19 @@
*/ */
class CharColorConverter { class CharColorConverter {
/**
* @var array
*/
protected $colortable;
/**
* Default constructor.
* @return void
*/
public function __construct() {
$this->buildColorTable();
}
/** /**
* Returns an array with RGB for the corresponding index. * Returns an array with RGB for the corresponding index.
* @param int $index Index from 0 to 1530. (Everything above will result the same) * @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); 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. * Converts a single char to a string.
* @param string $string * @param string $string
@ -42,7 +66,7 @@ class CharColorConverter {
public function charToColor($string) { public function charToColor($string) {
if(strlen($string) === 0) throw new Exception("Empty string given!"); if(strlen($string) === 0) throw new Exception("Empty string given!");
$index = ord($string) * 5; $index = ord($string) * 5;
$color = $this->index2rgb($index); $color = $this->colortable[$index];
return $color; return $color;
} }
} }

View File

@ -20,7 +20,9 @@ class FileImageConverter {
* @return void * @return void
*/ */
public function __construct() { public function __construct() {
echo "\nInitializing color translation table...";
$this->colorConverter = new CharColorConverter(); $this->colorConverter = new CharColorConverter();
echo "done.\n";
} }
/** /**