source-class-Com.Tecnick.Color.Model.Rgb

It appears that you are using AdBlocking software. The cost of running this website is covered by advertisements. If you like it please feel free to a small amount of money to secure the future of this website.
  1: <?php
  2: /**
  3:  * Rgb.php
  4:  *
  5:  * @since       2015-02-21
  6:  * @category    Library
  7:  * @package     Color
  8:  * @author      Nicola Asuni <info@tecnick.com>
  9:  * @copyright   2015-2015 Nicola Asuni - Tecnick.com LTD
 10:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 11:  * @link        https://github.com/tecnickcom/tc-lib-color
 12:  *
 13:  * This file is part of tc-lib-color software library.
 14:  */
 15: 
 16: namespace Com\Tecnick\Color\Model;
 17: 
 18: /**
 19:  * Com\Tecnick\Color\Model\Rgb
 20:  *
 21:  * RGB Color Model class
 22:  *
 23:  * @since       2015-02-21
 24:  * @category    Library
 25:  * @package     Color
 26:  * @author      Nicola Asuni <info@tecnick.com>
 27:  * @copyright   2015-2015 Nicola Asuni - Tecnick.com LTD
 28:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 29:  * @link        https://github.com/tecnickcom/tc-lib-color
 30:  */
 31: class Rgb extends \Com\Tecnick\Color\Model implements \Com\Tecnick\Color\Model\Template
 32: {
 33:     /**
 34:      * Color Model type
 35:      *
 36:      * @var string
 37:      */
 38:     protected $type = 'RGB';
 39: 
 40:     /**
 41:      * Value of the Red color component [0..1]
 42:      *
 43:      * @var float
 44:      */
 45:     protected $cmp_red = 0.0;
 46: 
 47:     /**
 48:      * Value of the Green color component [0..1]
 49:      *
 50:      * @var float
 51:      */
 52:     protected $cmp_green = 0.0;
 53: 
 54:     /**
 55:      * Value of the Blue color component [0..1]
 56:      *
 57:      * @var float
 58:      */
 59:     protected $cmp_blue = 0.0;
 60: 
 61:     /**
 62:      * Get an array with all color components
 63:      *
 64:      * @return array with keys ('R', 'G', 'B', 'A')
 65:      */
 66:     public function getArray()
 67:     {
 68:         return array(
 69:             'R' => $this->cmp_red,
 70:             'G' => $this->cmp_green,
 71:             'B' => $this->cmp_blue,
 72:             'A' => $this->cmp_alpha
 73:         );
 74:     }
 75: 
 76:     /**
 77:      * Get an array with color components values normalized between 0 and $max.
 78:      * NOTE: the alpha and other fraction component values are kept in the [0..1] range.
 79:      *
 80:      * @param int $max Maximum value to return (reference value)
 81:      *
 82:      * @return array with keys ('R', 'G', 'B', 'A')
 83:      */
 84:     public function getNormalizedArray($max)
 85:     {
 86:         return array(
 87:             'R' => $this->getNormalizedValue($this->cmp_red, $max),
 88:             'G' => $this->getNormalizedValue($this->cmp_green, $max),
 89:             'B' => $this->getNormalizedValue($this->cmp_blue, $max),
 90:             'A' => $this->cmp_alpha
 91:         );
 92:     }
 93: 
 94:     /**
 95:      * Get the CSS representation of the color: rgba(R, G, B, A)
 96:      * NOTE: Supported since CSS3 and above.
 97:      *       Use getHexadecimalColor() for CSS1 and CSS2
 98:      *
 99:      * @return string
100:      */
101:     public function getCssColor()
102:     {
103:         return 'rgba('
104:             .$this->getNormalizedValue($this->cmp_red, 100).'%,'
105:             .$this->getNormalizedValue($this->cmp_green, 100).'%,'
106:             .$this->getNormalizedValue($this->cmp_blue, 100).'%,'
107:             .$this->cmp_alpha
108:             .')';
109:     }
110: 
111:     /**
112:      * Get the color format used in Acrobat JavaScript
113:      * NOTE: the alpha channel is omitted from this representation unless is 0 = transparent
114:      *
115:      * @return string
116:      */
117:     public function getJsPdfColor()
118:     {
119:         if ($this->cmp_alpha == 0) {
120:             return '["T"]'; // transparent color
121:         }
122:         return sprintf('["RGB",%F,%F,%F]', $this->cmp_red, $this->cmp_green, $this->cmp_blue);
123:     }
124: 
125:     /**
126:      * Get a space separated string with color component values.
127:      *
128:      * @return string
129:      */
130:     public function getComponentsString()
131:     {
132:         return sprintf('%F %F %F', $this->cmp_red, $this->cmp_green, $this->cmp_blue);
133:     }
134: 
135:     /**
136:      * Get the color components format used in PDF documents (RGB)
137:      * NOTE: the alpha channel is omitted
138:      *
139:      * @param bool $stroke True for stroking (lines, drawing) and false for non-stroking (text and area filling).
140:      *
141:      * @return string
142:      */
143:     public function getPdfColor($stroke = false)
144:     {
145:         $mode = 'rg';
146:         if ($stroke) {
147:             $mode = strtoupper($mode);
148:         }
149:         return $this->getComponentsString().' '.$mode."\n";
150:     }
151: 
152:     /**
153:      * Get an array with Gray color components
154:      *
155:      * @return array with keys ('gray')
156:      */
157:     public function toGrayArray()
158:     {
159:         // convert using the SMPTE 295M-1997 standard conversion constants
160:         return array(
161:             'gray'  => (max(0, min(
162:                 1,
163:                 ((0.2126 * $this->cmp_red) + (0.7152 * $this->cmp_green) + (0.0722 * $this->cmp_blue))
164:             ))),
165:             'alpha' => $this->cmp_alpha
166:         );
167:     }
168: 
169:     /**
170:      * Get an array with RGB color components
171:      *
172:      * @return array with keys ('red', 'green', 'blue', 'alpha')
173:      */
174:     public function toRgbArray()
175:     {
176:         return array(
177:             'red'   => $this->cmp_red,
178:             'green' => $this->cmp_green,
179:             'blue'  => $this->cmp_blue,
180:             'alpha' => $this->cmp_alpha
181:         );
182:     }
183: 
184:     /**
185:      * Get an array with HSL color components
186:      *
187:      * @return array with keys ('hue', 'saturation', 'lightness', 'alpha')
188:      */
189:     public function toHslArray()
190:     {
191:         $min = min($this->cmp_red, $this->cmp_green, $this->cmp_blue);
192:         $max = max($this->cmp_red, $this->cmp_green, $this->cmp_blue);
193:         $lightness = (($min + $max) / 2);
194:         if ($min == $max) {
195:             $saturation = 0;
196:             $hue = 0;
197:         } else {
198:             $diff = ($max - $min);
199:             if ($lightness < 0.5) {
200:                 $saturation = ($diff / ($max + $min));
201:             } else {
202:                 $saturation = ($diff / (2.0 - $max - $min));
203:             }
204:             switch ($max) {
205:                 case $this->cmp_red:
206:                     $dgb = ($this->cmp_green - $this->cmp_blue);
207:                     $hue = ($dgb / $diff) + (($dgb < 0) ? 6 : 0);
208:                     break;
209:                 case $this->cmp_green:
210:                     $hue = (2.0 + (($this->cmp_blue - $this->cmp_red) / $diff));
211:                     break;
212:                 case $this->cmp_blue:
213:                     $hue = (4.0 + (($this->cmp_red - $this->cmp_green) / $diff));
214:                     break;
215:             }
216:             $hue /= 6; // 6 = 360 / 60
217:         }
218:         return array(
219:             'hue'        => max(0, min(1, $hue)),
220:             'saturation' => max(0, min(1, $saturation)),
221:             'lightness'  => max(0, min(1, $lightness)),
222:             'alpha'      => $this->cmp_alpha
223:         );
224:     }
225: 
226:     /**
227:      * Get an array with CMYK color components
228:      *
229:      * @return array with keys ('cyan', 'magenta', 'yellow', 'key', 'alpha')
230:      */
231:     public function toCmykArray()
232:     {
233:         $cyan = (1 - $this->cmp_red);
234:         $magenta = (1 - $this->cmp_green);
235:         $yellow = (1 - $this->cmp_blue);
236:         $key = 1;
237:         if ($cyan < $key) {
238:             $key = $cyan;
239:         }
240:         if ($magenta < $key) {
241:             $key = $magenta;
242:         }
243:         if ($yellow < $key) {
244:             $key = $yellow;
245:         }
246:         if ($key == 1) {
247:             // black
248:             $cyan = 0;
249:             $magenta = 0;
250:             $yellow = 0;
251:         } else {
252:             $cyan = (($cyan - $key) / (1 - $key));
253:             $magenta = (($magenta - $key) / (1 - $key));
254:             $yellow = (($yellow - $key) / (1 - $key));
255:         }
256:         return array(
257:             'cyan'    => max(0, min(1, $cyan)),
258:             'magenta' => max(0, min(1, $magenta)),
259:             'yellow'  => max(0, min(1, $yellow)),
260:             'key'     => max(0, min(1, $key)),
261:             'alpha'   => $this->cmp_alpha
262:         );
263:     }
264: 
265:     /**
266:      * Invert the color
267:      */
268:     public function invertColor()
269:     {
270:         $this->cmp_red   = (1 - $this->cmp_red);
271:         $this->cmp_green = (1 - $this->cmp_green);
272:         $this->cmp_blue  = (1 - $this->cmp_blue);
273:         return $this;
274:     }
275: }
276: 
 

© 2004-2017 – Nicola Asuni - Tecnick.com - All rights reserved.
about - disclaimer - privacy