Cheetah
AbstractColor.php
Go to the documentation of this file.
1 <?php
2 
4 
5 abstract class AbstractColor
6 {
13  abstract public function initFromInteger($value);
14 
21  abstract public function initFromArray($value);
22 
29  abstract public function initFromString($value);
30 
37  abstract public function initFromObject($value);
38 
47  abstract public function initFromRgb($r, $g, $b);
48 
58  abstract public function initFromRgba($r, $g, $b, $a);
59 
65  abstract public function getInt();
66 
73  abstract public function getHex($prefix);
74 
80  abstract public function getArray();
81 
87  abstract public function getRgba();
88 
96  abstract public function differs(AbstractColor $color, $tolerance = 0);
97 
103  public function __construct($value = null)
104  {
105  $this->parse($value);
106  }
107 
114  public function parse($value)
115  {
116  switch (true) {
117 
118  case is_string($value):
119  $this->initFromString($value);
120  break;
121 
122  case is_int($value):
123  $this->initFromInteger($value);
124  break;
125 
126  case is_array($value):
127  $this->initFromArray($value);
128  break;
129 
130  case is_object($value):
131  $this->initFromObject($value);
132  break;
133 
134  case is_null($value):
135  $this->initFromArray(array(255, 255, 255, 0));
136  break;
137 
138  default:
139  throw new \Intervention\Image\Exception\NotReadableException(
140  "Color format ({$value}) cannot be read."
141  );
142  }
143 
144  return $this;
145  }
146 
153  public function format($type)
154  {
155  switch (strtolower($type)) {
156 
157  case 'rgba':
158  return $this->getRgba();
159 
160  case 'hex':
161  return $this->getHex('#');
162 
163  case 'int':
164  case 'integer':
165  return $this->getInt();
166 
167  case 'array':
168  return $this->getArray();
169 
170  case 'obj':
171  case 'object':
172  return $this;
173 
174  default:
175  throw new \Intervention\Image\Exception\NotSupportedException(
176  "Color format ({$type}) is not supported."
177  );
178  }
179  }
180 
187  protected function rgbaFromString($value)
188  {
189  $result = false;
190 
191  // parse color string in hexidecimal format like #cccccc or cccccc or ccc
192  $hexPattern = '/^#?([a-f0-9]{1,2})([a-f0-9]{1,2})([a-f0-9]{1,2})$/i';
193 
194  // parse color string in format rgb(140, 140, 140)
195  $rgbPattern = '/^rgb ?\‍(([0-9]{1,3}), ?([0-9]{1,3}), ?([0-9]{1,3})\‍)$/i';
196 
197  // parse color string in format rgba(255, 0, 0, 0.5)
198  $rgbaPattern = '/^rgba ?\‍(([0-9]{1,3}), ?([0-9]{1,3}), ?([0-9]{1,3}), ?([0-9.]{1,4})\‍)$/i';
199 
200  if (preg_match($hexPattern, $value, $matches)) {
201  $result = array();
202  $result[0] = strlen($matches[1]) == '1' ? hexdec($matches[1].$matches[1]) : hexdec($matches[1]);
203  $result[1] = strlen($matches[2]) == '1' ? hexdec($matches[2].$matches[2]) : hexdec($matches[2]);
204  $result[2] = strlen($matches[3]) == '1' ? hexdec($matches[3].$matches[3]) : hexdec($matches[3]);
205  $result[3] = 1;
206  } elseif (preg_match($rgbPattern, $value, $matches)) {
207  $result = array();
208  $result[0] = ($matches[1] >= 0 && $matches[1] <= 255) ? intval($matches[1]) : 0;
209  $result[1] = ($matches[2] >= 0 && $matches[2] <= 255) ? intval($matches[2]) : 0;
210  $result[2] = ($matches[3] >= 0 && $matches[3] <= 255) ? intval($matches[3]) : 0;
211  $result[3] = 1;
212  } elseif (preg_match($rgbaPattern, $value, $matches)) {
213  $result = array();
214  $result[0] = ($matches[1] >= 0 && $matches[1] <= 255) ? intval($matches[1]) : 0;
215  $result[1] = ($matches[2] >= 0 && $matches[2] <= 255) ? intval($matches[2]) : 0;
216  $result[2] = ($matches[3] >= 0 && $matches[3] <= 255) ? intval($matches[3]) : 0;
217  $result[3] = ($matches[4] >= 0 && $matches[4] <= 1) ? $matches[4] : 0;
218  } else {
219  throw new \Intervention\Image\Exception\NotReadableException(
220  "Unable to read color ({$value})."
221  );
222  }
223 
224  return $result;
225  }
226 }
Intervention\Image\AbstractColor\initFromObject
initFromObject($value)
Intervention\Image\AbstractColor\initFromInteger
initFromInteger($value)
Intervention\Image\AbstractColor\initFromRgb
initFromRgb($r, $g, $b)
php
Intervention\Image\AbstractColor\getRgba
getRgba()
Intervention\Image\AbstractColor\initFromString
initFromString($value)
Intervention\Image\AbstractColor\getInt
getInt()
Intervention\Image\AbstractColor\format
format($type)
Definition: AbstractColor.php:153
Intervention\Image\AbstractColor\initFromRgba
initFromRgba($r, $g, $b, $a)
Intervention\Image\AbstractColor\getHex
getHex($prefix)
Intervention\Image\AbstractColor\parse
parse($value)
Definition: AbstractColor.php:114
Intervention\Image\AbstractColor\initFromArray
initFromArray($value)
Intervention\Image\AbstractColor
Definition: AbstractColor.php:6
Intervention\Image\AbstractColor\differs
differs(AbstractColor $color, $tolerance=0)
Intervention\Image\AbstractColor\__construct
__construct($value=null)
Definition: AbstractColor.php:103
Intervention\Image\AbstractColor\rgbaFromString
rgbaFromString($value)
Definition: AbstractColor.php:187
Intervention\Image\AbstractColor\getArray
getArray()
Intervention\Image
Definition: AbstractColor.php:3