Cheetah
All Classes Namespaces Files Functions Variables Pages
Color.php
Go to the documentation of this file.
1 <?php
2 
4 
6 
7 class Color extends AbstractColor
8 {
14  public $r;
15 
21  public $g;
22 
28  public $b;
29 
35  public $a;
36 
43  public function initFromInteger($value)
44  {
45  $this->a = ($value >> 24) & 0xFF;
46  $this->r = ($value >> 16) & 0xFF;
47  $this->g = ($value >> 8) & 0xFF;
48  $this->b = $value & 0xFF;
49  }
50 
57  public function initFromArray($array)
58  {
59  $array = array_values($array);
60 
61  if (count($array) == 4) {
62 
63  // color array with alpha value
64  list($r, $g, $b, $a) = $array;
65  $this->a = $this->alpha2gd($a);
66 
67  } elseif (count($array) == 3) {
68 
69  // color array without alpha value
70  list($r, $g, $b) = $array;
71  $this->a = 0;
72 
73  }
74 
75  $this->r = $r;
76  $this->g = $g;
77  $this->b = $b;
78  }
79 
86  public function initFromString($value)
87  {
88  if ($color = $this->rgbaFromString($value)) {
89  $this->r = $color[0];
90  $this->g = $color[1];
91  $this->b = $color[2];
92  $this->a = $this->alpha2gd($color[3]);
93  }
94  }
95 
104  public function initFromRgb($r, $g, $b)
105  {
106  $this->r = intval($r);
107  $this->g = intval($g);
108  $this->b = intval($b);
109  $this->a = 0;
110  }
111 
121  public function initFromRgba($r, $g, $b, $a = 1)
122  {
123  $this->r = intval($r);
124  $this->g = intval($g);
125  $this->b = intval($b);
126  $this->a = $this->alpha2gd($a);
127  }
128 
135  public function initFromObject($value)
136  {
137  throw new \Intervention\Image\Exception\NotSupportedException(
138  "GD colors cannot init from ImagickPixel objects."
139  );
140  }
141 
147  public function getInt()
148  {
149  return ($this->a << 24) + ($this->r << 16) + ($this->g << 8) + $this->b;
150  }
151 
158  public function getHex($prefix = '')
159  {
160  return sprintf('%s%02x%02x%02x', $prefix, $this->r, $this->g, $this->b);
161  }
162 
168  public function getArray()
169  {
170  return array($this->r, $this->g, $this->b, round(1 - $this->a / 127, 2));
171  }
172 
178  public function getRgba()
179  {
180  return sprintf('rgba(%d, %d, %d, %.2f)', $this->r, $this->g, $this->b, round(1 - $this->a / 127, 2));
181  }
182 
190  public function differs(AbstractColor $color, $tolerance = 0)
191  {
192  $color_tolerance = round($tolerance * 2.55);
193  $alpha_tolerance = round($tolerance * 1.27);
194 
195  $delta = array(
196  'r' => abs($color->r - $this->r),
197  'g' => abs($color->g - $this->g),
198  'b' => abs($color->b - $this->b),
199  'a' => abs($color->a - $this->a)
200  );
201 
202  return (
203  $delta['r'] > $color_tolerance or
204  $delta['g'] > $color_tolerance or
205  $delta['b'] > $color_tolerance or
206  $delta['a'] > $alpha_tolerance
207  );
208  }
209 
216  private function alpha2gd($input)
217  {
218  $oldMin = 0;
219  $oldMax = 1;
220 
221  $newMin = 127;
222  $newMax = 0;
223 
224  return ceil(((($input- $oldMin) * ($newMax - $newMin)) / ($oldMax - $oldMin)) + $newMin);
225  }
226 }
Intervention\Image\Gd\Color\initFromRgb
initFromRgb($r, $g, $b)
Definition: Color.php:104
Intervention\Image\Gd\Color\initFromRgba
initFromRgba($r, $g, $b, $a=1)
Definition: Color.php:121
Intervention\Image\Gd\Color\getArray
getArray()
Definition: Color.php:168
use
GNU LESSER GENERAL PUBLIC LICENSE February Free Software Inc Franklin Fifth MA USA Everyone is permitted to copy and distribute verbatim copies of this license but changing it is not allowed[This is the first released version of the Lesser GPL. It also counts as the successor of the GNU Library Public License, version 2, hence the version number 2.1.] Preamble The licenses for most software are designed to take away your freedom to share and change it By the GNU General Public Licenses are intended to guarantee your freedom to share and change free software to make sure the software is free for all its users This the Lesser General Public applies to some specially designated software packages typically libraries of the Free Software Foundation and other authors who decide to use it You can use it but we suggest you first think carefully about whether this license or the ordinary General Public License is the better strategy to use in any particular based on the explanations below When we speak of free we are referring to freedom of use
Definition: license.txt:27
php
Intervention\Image\Gd
Definition: Color.php:3
a
Filter ExtractStyleBlocks Scope FilterParam ExtractStyleBlocksScope DESCRIPTION< p > If you would like users to be able to define external but only allow them to specify CSS declarations for a specific node and prevent them from fiddling with other use this directive It accepts any valid CSS and will prepend this to any CSS declaration extracted from the document For if this directive is set to< code > selector< code > a
Definition: Filter.ExtractStyleBlocks.Scope.txt:15
Intervention\Image\Gd\Color\initFromString
initFromString($value)
Definition: Color.php:86
Intervention\Image\Gd\Color\$a
$a
Definition: Color.php:35
Intervention\Image\Gd\Color\$g
$g
Definition: Color.php:21
Intervention\Image\Gd\Color\$r
$r
Definition: Color.php:14
Intervention\Image\Gd\Color\getRgba
getRgba()
Definition: Color.php:178
Intervention\Image\Gd\Color\differs
differs(AbstractColor $color, $tolerance=0)
Definition: Color.php:190
Intervention\Image\Gd\Color\getInt
getInt()
Definition: Color.php:147
Intervention\Image\Gd\Color\$b
$b
Definition: Color.php:28
Intervention\Image\AbstractColor
Definition: AbstractColor.php:6
Intervention\Image\Gd\Color\initFromArray
initFromArray($array)
Definition: Color.php:57
Intervention\Image\Gd\Color
Definition: Color.php:8
Intervention\Image\Gd\Color\initFromObject
initFromObject($value)
Definition: Color.php:135
Intervention\Image\Gd\Color\getHex
getHex($prefix='')
Definition: Color.php:158
b
el b
Definition: Output.SortAttr.txt:8
Intervention\Image\Gd\Color\initFromInteger
initFromInteger($value)
Definition: Color.php:43
Intervention\Image\AbstractColor\rgbaFromString
rgbaFromString($value)
Definition: AbstractColor.php:187
or
Voluntary License Schemes The Licensor waives the right to collect whether individually or
Definition: license.txt:37