Go to the documentation of this file.00001 <?php
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00015 final class Color implements Stringable
00016 {
00017 private $red = 0;
00018 private $green = 0;
00019 private $blue = 0;
00020
00024 public static function create($rgb)
00025 {
00026 static $flyweightColors = array();
00027
00028 if (isset($flyweightColors[$rgb]))
00029 return $flyweightColors[$rgb];
00030
00031 $result = new self($rgb);
00032
00033 $flyweightColors[$rgb] = $result;
00034
00035 return $result;
00036 }
00037
00038
00039 public function __construct($rgb)
00040 {
00041 $length = strlen($rgb);
00042
00043 Assert::isTrue($length <= 7, 'color must be #XXXXXX');
00044
00045 if ($rgb[0] == '#')
00046 $rgb = substr($rgb, 1);
00047
00048 if ($length < 6)
00049 $rgb = str_pad($rgb, 6, '0', STR_PAD_LEFT);
00050
00051 $this->red = hexdec($rgb[0] . $rgb[1]);
00052 $this->green = hexdec($rgb[2] . $rgb[3]);
00053 $this->blue = hexdec($rgb[4] . $rgb[5]);
00054 }
00055
00059 public function setRed($red)
00060 {
00061 $this->red = $red;
00062
00063 return $this;
00064 }
00065
00066 public function getRed()
00067 {
00068 return $this->red;
00069 }
00070
00074 public function setGreen($green)
00075 {
00076 $this->green = $green;
00077
00078 return $this;
00079 }
00080
00081 public function getGreen()
00082 {
00083 return $this->green;
00084 }
00085
00089 public function setBlue($blue)
00090 {
00091 $this->blue = $blue;
00092
00093 return $this;
00094 }
00095
00096 public function getBlue()
00097 {
00098 return $this->blue;
00099 }
00100
00104 public function invertColor()
00105 {
00106 $this->setRed(255 - $this->getRed());
00107 $this->setBlue(255 - $this->getBlue());
00108 $this->setGreen(255 - $this->getGreen());
00109
00110 return $this;
00111 }
00112
00113 public function toString()
00114 {
00115 return sprintf('%02X%02X%02X', $this->red, $this->green, $this->blue);
00116 }
00117 }
00118 ?>