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

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:  * Hsl.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\Hsl
 20:  *
 21:  * HSL 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 Hsl 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 = 'HSL';
 39: 
 40:     /**
 41:      * Value of the Hue color component [0..1]
 42:      *
 43:      * @var float
 44:      */
 45:     protected $cmp_hue = 0.0;
 46: 
 47:     /**
 48:      * Value of the Saturation color component [0..1]
 49:      *
 50:      * @var float
 51:      */
 52:     protected $cmp_saturation = 0.0;
 53: 
 54:     /**
 55:      * Value of the Lightness color component [0..1]
 56:      *
 57:      * @var float
 58:      */
 59:     protected $cmp_lightness = 0.0;
 60: 
 61:     /**
 62:      * Get an array with all color components
 63:      *
 64:      * @return array with keys ('H', 'S', 'L', 'A')
 65:      */
 66:     public function getArray()
 67:     {
 68:         return array(
 69:             'H' => $this->cmp_hue,
 70:             'S' => $this->cmp_saturation,
 71:             'L' => $this->cmp_lightness,
 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 (it is always set to 360)
 81:      *
 82:      * @return array with keys ('H', 'S', 'L', 'A')
 83:      */
 84:     public function getNormalizedArray($max)
 85:     {
 86:         $max = 360;
 87:         return array(
 88:             'H' => $this->getNormalizedValue($this->cmp_hue, $max),
 89:             'S' => $this->cmp_saturation,
 90:             'L' => $this->cmp_lightness,
 91:             'A' => $this->cmp_alpha
 92:         );
 93:     }
 94: 
 95:     /**
 96:      * Get the CSS representation of the color: hsla(H, S, L, A)
 97:      * NOTE: Supported since CSS3 and above.
 98:      *       Use getHexadecimalColor() for CSS1 and CSS2
 99:      *
100:      * @return string
101:      */
102:     public function getCssColor()
103:     {
104:         return 'hsla('
105:             .$this->getNormalizedValue($this->cmp_hue, 360).','
106:             .$this->getNormalizedValue($this->cmp_saturation, 100).'%,'
107:             .$this->getNormalizedValue($this->cmp_lightness, 100).'%,'
108:             .$this->cmp_alpha
109:             .')';
110:     }
111: 
112:     /**
113:      * Get the color format used in Acrobat JavaScript
114:      * NOTE: the alpha channel is omitted from this representation unless is 0 = transparent
115:      *
116:      * @return string
117:      */
118:     public function getJsPdfColor()
119:     {
120:         $rgb = $this->toRgbArray();
121:         if ($this->cmp_alpha == 0) {
122:             return '["T"]'; // transparent color
123:         }
124:         return sprintf('["RGB",%F,%F,%F]', $rgb['red'], $rgb['green'], $rgb['blue']);
125:     }
126: 
127:     /**
128:      * Get a space separated string with color component values.
129:      *
130:      * @return string
131:      */
132:     public function getComponentsString()
133:     {
134:         $rgb = $this->toRgbArray();
135:         return sprintf('%F %F %F', $rgb['red'], $rgb['green'], $rgb['blue']);
136:     }
137: 
138:     /**
139:      * Get the color components format used in PDF documents (RGB)
140:      * NOTE: the alpha channel is omitted
141:      *
142:      * @param bool $stroke True for stroking (lines, drawing) and false for non-stroking (text and area filling).
143:      *
144:      * @return string
145:      */
146:     public function getPdfColor($stroke = false)
147:     {
148:         $mode = 'rg';
149:         if ($stroke) {
150:             $mode = strtoupper($mode);
151:         }
152:         return $this->getComponentsString().' '.$mode."\n";
153:     }
154: 
155:     /**
156:      * Get an array with Gray color components
157:      *
158:      * @return array with keys ('gray')
159:      */
160:     public function toGrayArray()
161:     {
162:         return array(
163:             'gray'  => $this->cmp_lightness,
164:             'alpha' => $this->cmp_alpha
165:         );
166:     }
167: 
168:     /**
169:      * Get an array with RGB color components
170:      *
171:      * @return array with keys ('red', 'green', 'blue', 'alpha')
172:      */
173:     public function toRgbArray()
174:     {
175:         if ($this->cmp_saturation == 0) {
176:             return array(
177:                 'red'   => $this->cmp_lightness,
178:                 'green' => $this->cmp_lightness,
179:                 'blue'  => $this->cmp_lightness,
180:                 'alpha' => $this->cmp_alpha
181:             );
182:         }
183:         if ($this->cmp_lightness < 0.5) {
184:             $valb = ($this->cmp_lightness * (1 + $this->cmp_saturation));
185:         } else {
186:             $valb = (($this->cmp_lightness + $this->cmp_saturation) - ($this->cmp_lightness * $this->cmp_saturation));
187:         }
188:         $vala = ((2 * $this->cmp_lightness) - $valb);
189:         return array(
190:             'red'   => $this->convertHuetoRgb($vala, $valb, ($this->cmp_hue + (1 / 3))),
191:             'green' => $this->convertHuetoRgb($vala, $valb, $this->cmp_hue),
192:             'blue'  => $this->convertHuetoRgb($vala, $valb, ($this->cmp_hue - (1 / 3))),
193:             'alpha' => $this->cmp_alpha
194:         );
195:     }
196: 
197:     /**
198:      * Convet Hue to RGB
199:      *
200:      * @param float $vala Temporary value A
201:      * @param float $valb Temporary value B
202:      * @param float $hue  Hue value
203:      *
204:      * @return float
205:      */
206:     private function convertHuetoRgb($vala, $valb, $hue)
207:     {
208:         if ($hue < 0) {
209:             $hue += 1;
210:         }
211:         if ($hue > 1) {
212:             $hue -= 1;
213:         }
214:         if ((6 * $hue) < 1) {
215:             return max(0, min(1, ($vala + (($valb - $vala) * 6 * $hue))));
216:         }
217:         if ((2 * $hue) < 1) {
218:             return max(0, min(1, $valb));
219:         }
220:         if ((3 * $hue) < 2) {
221:             return max(0, min(1, ($vala + (($valb - $vala) * ((2 / 3) - $hue) * 6))));
222:         }
223:         return max(0, min(1, $vala));
224:     }
225: 
226:     /**
227:      * Get an array with HSL color components
228:      *
229:      * @return array with keys ('hue', 'saturation', 'lightness', 'alpha')
230:      */
231:     public function toHslArray()
232:     {
233:         return array(
234:             'hue'        => $this->cmp_hue,
235:             'saturation' => $this->cmp_saturation,
236:             'lightness'  => $this->cmp_lightness,
237:             'alpha'      => $this->cmp_alpha
238:         );
239:     }
240: 
241:     /**
242:      * Get an array with CMYK color components
243:      *
244:      * @return array with keys ('cyan', 'magenta', 'yellow', 'key', 'alpha')
245:      */
246:     public function toCmykArray()
247:     {
248:         $rgb = new \Com\Tecnick\Color\Model\Rgb($this->toRgbArray());
249:         return $rgb->toCmykArray();
250:     }
251: 
252:     /**
253:      * Invert the color
254:      */
255:     public function invertColor()
256:     {
257:         $this->cmp_hue = ($this->cmp_hue >= 0.5) ? ($this->cmp_hue - 0.5) : ($this->cmp_hue + 0.5);
258:         return $this;
259:     }
260: }
261: 
 

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