red = $red; $this->green = $green; $this->blue = $blue; } public function asHex() { return sprintf("%06X", $this->red * 256 * 256 + $this->green * 256 + $this->blue); } public static function mix($rgb1, $w1, $rgb2, $w2) { return new RGB(iround($w1 * $rgb1->red + $w2 * $rgb2->red), iround($w1 * $rgb1->green + $w2 * $rgb2->green), iround($w1 * $rgb1->blue + $w2 * $rgb2->blue)); } } class ColorPalette { private $rgbvalues; private function fill($ix1, $rgb1, $ix2, $rgb2) { for($i = $ix1 + 1; $i < $ix2; $i++) { $dist = $ix2 - $ix1; $w2 = ($i - $ix1) / $dist; $w1 = 1 - $w2; $this->rgbvalues[] = RGB::mix($rgb1, $w1, $rgb2, $w2); } } public function __construct($nocolors, $basecolors) { $baselen = count($basecolors); $this->rgbvalues = array(); $this->rgbvalues[] = $basecolors[0]; $lastix = 0; for($i = 1; $i < $baselen; $i++) { $ix = iround($i * $nocolors / ($baselen - 1)) - 1; $this->fill($lastix, $basecolors[$i - 1], $ix, $basecolors[$i]); $this->rgbvalues[] = $basecolors[$i]; $lastix = $ix; } } public function getNoColors() { return count($this->rgbvalues); } public function getColor($ix) { return $this->rgbvalues[$ix]; } } function dump_palette($palette) { for($i = 0; $i < $palette->getNoColors(); $i++) { echo sprintf("%d
\r\n", $palette->getColor($i)->asHex(), $i); } } $palette = new ColorPalette(100, array(new RGB(255, 0, 0), new RGB(0, 255, 0), new RGB(0, 0, 255))); dump_palette($palette); ?>