. */ /** * Color is an immutable class, so all mutator methods return a new * Color instance rather than modifying this instance. * * The immutability is in practice undermined by the fact that the RGB * components are public. This is a transitional detail that should * eventually be done away with. */ class Color { public function __construct($red, $green = null, $blue = null) { if(is_null($green)) $green = $red; if(is_null($blue)) $blue = $red; if ($red < 0 || $red > 255) { throw new InvalidArgumentException("Invalid Red component"); } if ($green < 0 || $green > 255) { throw new InvalidArgumentException("Invalid Green component"); } if ($blue < 0 || $blue > 255) { throw new InvalidArgumentException("Invalid Blue component"); } $this->r = $red; $this->g = $green; $this->b = $blue; } /** * Return a new color formed by adding the specified increment to * the R, G and B values */ public function addRGBIncrement($increment) { $incremented = new Color($this->r, $this->g, $this->b); $incremented->r = $this->truncateColorComponentRange($incremented->r + $increment); $incremented->g = $this->truncateColorComponentRange($incremented->g + $increment); $incremented->b = $this->truncateColorComponentRange($incremented->b + $increment); return $incremented; } public function __toString() { return sprintf("Color<%d, %d, %d>", $this->r, $this->g, $this->b); } private function truncateColorComponentRange($input) { if ($input > 255) { return 255; } elseif ($input < 0) { return 0; } else { return $input; } } public function getR() { return $this->r; } public function getG() { return $this->g; } public function getB() { return $this->b; } /** * The members r, g and b are still public since they are used * within GDCanvas. Since we don't have any GDCanvas unit tests * yet, we can't safely make these private at the moment. */ public $r; public $g; public $b; }