00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00016 final class Color implements Stringable
00017 {
00018 private $red = 0;
00019 private $green = 0;
00020 private $blue = 0;
00021
00025 public static function create($rgb)
00026 {
00027 return new self($rgb);
00028 }
00029
00030
00031 public function __construct($rgb)
00032 {
00033 $length = strlen($rgb);
00034
00035 Assert::isTrue($length <= 7, 'color must be #XXXXXX');
00036
00037 if ($rgb[0] == '#')
00038 $rgb = substr($rgb, 1);
00039
00040 if ($length < 6)
00041 $rgb = str_pad($rgb, 6, '0', STR_PAD_LEFT);
00042
00043 $this->red = hexdec($rgb[0] . $rgb[1]);
00044 $this->green = hexdec($rgb[2] . $rgb[3]);
00045 $this->blue = hexdec($rgb[4] . $rgb[5]);
00046 }
00047
00051 public function setRed($red)
00052 {
00053 $this->red = $red;
00054
00055 return $this;
00056 }
00057
00058 public function getRed()
00059 {
00060 return $this->red;
00061 }
00062
00066 public function setGreen($green)
00067 {
00068 $this->green = $green;
00069
00070 return $this;
00071 }
00072
00073 public function getGreen()
00074 {
00075 return $this->green;
00076 }
00077
00081 public function setBlue($blue)
00082 {
00083 $this->blue = $blue;
00084
00085 return $this;
00086 }
00087
00088 public function getBlue()
00089 {
00090 return $this->blue;
00091 }
00092
00096 public function invertColor()
00097 {
00098 $this->setRed(255 - $this->getRed());
00099 $this->setBlue(255 - $this->getBlue());
00100 $this->setGreen(255 - $this->getGreen());
00101
00102 return $this;
00103 }
00104
00105 public function toString()
00106 {
00107 return sprintf('%02X%02X%02X', $this->red, $this->green, $this->blue);
00108 }
00109 }
00110 ?>