Cheetah
Color.php
Go to the documentation of this file.
1 <?php
2 
4 
6 {
12  public $pixel;
13 
20  public function initFromInteger($value)
21  {
22  $a = ($value >> 24) & 0xFF;
23  $r = ($value >> 16) & 0xFF;
24  $g = ($value >> 8) & 0xFF;
25  $b = $value & 0xFF;
26  $a = $this->rgb2alpha($a);
27 
28  $this->setPixel($r, $g, $b, $a);
29  }
30 
37  public function initFromArray($array)
38  {
39  $array = array_values($array);
40 
41  if (count($array) == 4) {
42 
43  // color array with alpha value
44  list($r, $g, $b, $a) = $array;
45 
46  } elseif (count($array) == 3) {
47 
48  // color array without alpha value
49  list($r, $g, $b) = $array;
50  $a = 1;
51  }
52 
53  $this->setPixel($r, $g, $b, $a);
54  }
55 
63  public function initFromString($value)
64  {
65  if ($color = $this->rgbaFromString($value)) {
66  $this->setPixel($color[0], $color[1], $color[2], $color[3]);
67  }
68  }
69 
77  public function initFromObject($value)
78  {
79  if (is_a($value, '\ImagickPixel')) {
80  $this->pixel = $value;
81  }
82  }
83 
93  public function initFromRgb($r, $g, $b)
94  {
95  $this->setPixel($r, $g, $b);
96  }
97 
108  public function initFromRgba($r, $g, $b, $a)
109  {
110  $this->setPixel($r, $g, $b, $a);
111  }
112 
118  public function getInt()
119  {
120  $r = $this->getRedValue();
121  $g = $this->getGreenValue();
122  $b = $this->getBlueValue();
123  $a = intval(round($this->getAlphaValue() * 255));
124 
125  return intval(($a << 24) + ($r << 16) + ($g << 8) + $b);
126  }
127 
135  public function getHex($prefix = '')
136  {
137  return sprintf('%s%02x%02x%02x', $prefix,
138  $this->getRedValue(),
139  $this->getGreenValue(),
140  $this->getBlueValue()
141  );
142  }
143 
149  public function getArray()
150  {
151  return array(
152  $this->getRedValue(),
153  $this->getGreenValue(),
154  $this->getBlueValue(),
155  $this->getAlphaValue()
156  );
157  }
158 
164  public function getRgba()
165  {
166  return sprintf('rgba(%d, %d, %d, %.2f)',
167  $this->getRedValue(),
168  $this->getGreenValue(),
169  $this->getBlueValue(),
170  $this->getAlphaValue()
171  );
172  }
173 
181  public function differs(\Intervention\Image\AbstractColor $color, $tolerance = 0)
182  {
183  $color_tolerance = round($tolerance * 2.55);
184  $alpha_tolerance = round($tolerance);
185 
186  $delta = array(
187  'r' => abs($color->getRedValue() - $this->getRedValue()),
188  'g' => abs($color->getGreenValue() - $this->getGreenValue()),
189  'b' => abs($color->getBlueValue() - $this->getBlueValue()),
190  'a' => abs($color->getAlphaValue() - $this->getAlphaValue())
191  );
192 
193  return (
194  $delta['r'] > $color_tolerance or
195  $delta['g'] > $color_tolerance or
196  $delta['b'] > $color_tolerance or
197  $delta['a'] > $alpha_tolerance
198  );
199  }
200 
206  public function getRedValue()
207  {
208  return intval(round($this->pixel->getColorValue(\Imagick::COLOR_RED) * 255));
209  }
210 
216  public function getGreenValue()
217  {
218  return intval(round($this->pixel->getColorValue(\Imagick::COLOR_GREEN) * 255));
219  }
220 
226  public function getBlueValue()
227  {
228  return intval(round($this->pixel->getColorValue(\Imagick::COLOR_BLUE) * 255));
229  }
230 
236  public function getAlphaValue()
237  {
238  return round($this->pixel->getColorValue(\Imagick::COLOR_ALPHA), 2);
239  }
240 
246  private function setPixel($r, $g, $b, $a = null)
247  {
248  $a = is_null($a) ? 1 : $a;
249 
250  return $this->pixel = new \ImagickPixel(
251  sprintf('rgba(%d, %d, %d, %.2f)', $r, $g, $b, $a)
252  );
253  }
254 
260  public function getPixel()
261  {
262  return $this->pixel;
263  }
264 
271  private function rgb2alpha($value)
272  {
273  // (255 -> 1.0) / (0 -> 0.0)
274  return (float) round($value/255, 2);
275  }
276 
277 }
Intervention\Image\Imagick\Color\getRedValue
getRedValue()
Definition: Color.php:206
Intervention\Image\Imagick\Color\initFromRgb
initFromRgb($r, $g, $b)
Definition: Color.php:93
Intervention\Image\Imagick\Color\getRgba
getRgba()
Definition: Color.php:164
Intervention\Image\Imagick\Color\initFromObject
initFromObject($value)
Definition: Color.php:77
Intervention\Image\Imagick
Definition: Color.php:3
Intervention\Image\Imagick\Color\getBlueValue
getBlueValue()
Definition: Color.php:226
Intervention\Image\Imagick\Color
Definition: Color.php:6
php
Intervention\Image\Imagick\Color\getHex
getHex($prefix='')
Definition: Color.php:135
Intervention\Image\Imagick\Color\initFromInteger
initFromInteger($value)
Definition: Color.php:20
Intervention\Image\Imagick\Color\getInt
getInt()
Definition: Color.php:118
Intervention\Image\Image
Definition: Image.php:50
Intervention\Image\Imagick\Color\getPixel
getPixel()
Definition: Color.php:260
Intervention\Image\Imagick\Color\getGreenValue
getGreenValue()
Definition: Color.php:216
Intervention\Image\Imagick\Color\getAlphaValue
getAlphaValue()
Definition: Color.php:236
Intervention\Image\AbstractColor
Definition: AbstractColor.php:6
Intervention\Image\Imagick\Color\getArray
getArray()
Definition: Color.php:149
Intervention\Image\Imagick\Color\differs
differs(\Intervention\Image\AbstractColor $color, $tolerance=0)
Definition: Color.php:181
Intervention\Image\Imagick\Color\initFromRgba
initFromRgba($r, $g, $b, $a)
Definition: Color.php:108
Intervention\Image\Imagick\Color\$pixel
$pixel
Definition: Color.php:12
Intervention\Image\AbstractColor\rgbaFromString
rgbaFromString($value)
Definition: AbstractColor.php:187
Intervention\Image\Imagick\Color\initFromArray
initFromArray($array)
Definition: Color.php:37
Intervention
or
Voluntary License Schemes The Licensor waives the right to collect whether individually or
Definition: license.txt:37
Intervention\Image\Imagick\Color\initFromString
initFromString($value)
Definition: Color.php:63