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

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:  * Cmyk.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\Cmyk
 20:  *
 21:  * CMYK 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 Cmyk 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 = 'CMYK';
 39: 
 40:     /**
 41:      * Value of the Cyan color component [0..1]
 42:      *
 43:      * @var float
 44:      */
 45:     protected $cmp_cyan = 0.0;
 46: 
 47:     /**
 48:      * Value of the Magenta color component [0..1]
 49:      *
 50:      * @var float
 51:      */
 52:     protected $cmp_magenta = 0.0;
 53: 
 54:     /**
 55:      * Value of the Yellow color component [0..1]
 56:      *
 57:      * @var float
 58:      */
 59:     protected $cmp_yellow = 0.0;
 60: 
 61:     /**
 62:      * Value of the Key (Black) color component [0..1]
 63:      *
 64:      * @var float
 65:      */
 66:     protected $cmp_key = 0.0;
 67: 
 68:     /**
 69:      * Get an array with all color components
 70:      *
 71:      * @return array with keys ('C', 'M', 'Y', 'K', 'A')
 72:      */
 73:     public function getArray()
 74:     {
 75:         return array(
 76:             'C' => $this->cmp_cyan,
 77:             'M' => $this->cmp_magenta,
 78:             'Y' => $this->cmp_yellow,
 79:             'K' => $this->cmp_key,
 80:             'A' => $this->cmp_alpha
 81:         );
 82:     }
 83: 
 84:     /**
 85:      * Get an array with color components values normalized between 0 and $max.
 86:      * NOTE: the alpha and other fraction component values are kept in the [0..1] range.
 87:      *
 88:      * @param int $max Maximum value to return (reference value - it should be 100)
 89:      *
 90:      * @return array with keys ('C', 'M', 'Y', 'K', 'A')
 91:      */
 92:     public function getNormalizedArray($max)
 93:     {
 94:         return array(
 95:             'C' => $this->getNormalizedValue($this->cmp_cyan, $max),
 96:             'M' => $this->getNormalizedValue($this->cmp_magenta, $max),
 97:             'Y' => $this->getNormalizedValue($this->cmp_yellow, $max),
 98:             'K' => $this->getNormalizedValue($this->cmp_key, $max),
 99:             'A' => $this->cmp_alpha,
100:         );
101:     }
102: 
103:     /**
104:      * Get the CSS representation of the color: rgba(R, G, B, A)
105:      * NOTE: Supported since CSS3 and above.
106:      *       Use getHexadecimalColor() for CSS1 and CSS2
107:      *
108:      * @return string
109:      */
110:     public function getCssColor()
111:     {
112:         $rgb = $this->toRgbArray();
113:         return 'rgba('
114:             .$this->getNormalizedValue($rgb['red'], 100).'%,'
115:             .$this->getNormalizedValue($rgb['green'], 100).'%,'
116:             .$this->getNormalizedValue($rgb['blue'], 100).'%,'
117:             .$rgb['alpha']
118:             .')';
119:     }
120: 
121:     /**
122:      * Get the color format used in Acrobat JavaScript
123:      * NOTE: the alpha channel is omitted from this representation unless is 0 = transparent
124:      *
125:      * @return string
126:      */
127:     public function getJsPdfColor()
128:     {
129:         if ($this->cmp_alpha == 0) {
130:             return '["T"]'; // transparent color
131:         }
132:         return sprintf('["CMYK",%F,%F,%F,%F]', $this->cmp_cyan, $this->cmp_magenta, $this->cmp_yellow, $this->cmp_key);
133:     }
134: 
135:     /**
136:      * Get a space separated string with color component values.
137:      *
138:      * @return string
139:      */
140:     public function getComponentsString()
141:     {
142:         return sprintf('%F %F %F %F', $this->cmp_cyan, $this->cmp_magenta, $this->cmp_yellow, $this->cmp_key);
143:     }
144: 
145:     /**
146:      * Get the color components format used in PDF documents (CMYK)
147:      * NOTE: the alpha channel is omitted
148:      *
149:      * @param bool $stroke True for stroking (lines, drawing) and false for non-stroking (text and area filling).
150:      *
151:      * @return string
152:      */
153:     public function getPdfColor($stroke = false)
154:     {
155:         $mode = 'k';
156:         if ($stroke) {
157:             $mode = strtoupper($mode);
158:         }
159:         return $this->getComponentsString().' '.$mode."\n";
160:     }
161: 
162:     /**
163:      * Get an array with Gray color components
164:      *
165:      * @return array with keys ('gray')
166:      */
167:     public function toGrayArray()
168:     {
169:         return array(
170:             'gray'  => $this->cmp_key,
171:             'alpha' => $this->cmp_alpha
172:         );
173:     }
174: 
175:     /**
176:      * Get an array with RGB color components
177:      *
178:      * @return array with keys ('red', 'green', 'blue', 'alpha')
179:      */
180:     public function toRgbArray()
181:     {
182:         return array(
183:             'red'   => max(0, min(1, (1 - (($this->cmp_cyan    * (1 - $this->cmp_key)) + $this->cmp_key)))),
184:             'green' => max(0, min(1, (1 - (($this->cmp_magenta * (1 - $this->cmp_key)) + $this->cmp_key)))),
185:             'blue'  => max(0, min(1, (1 - (($this->cmp_yellow  * (1 - $this->cmp_key)) + $this->cmp_key)))),
186:             'alpha' => $this->cmp_alpha
187:         );
188:     }
189: 
190:     /**
191:      * Get an array with HSL color components
192:      *
193:      * @return array with keys ('hue', 'saturation', 'lightness', 'alpha')
194:      */
195:     public function toHslArray()
196:     {
197:         $rgb = new \Com\Tecnick\Color\Model\Rgb($this->toRgbArray());
198:         return $rgb->toHslArray();
199:     }
200: 
201:     /**
202:      * Get an array with CMYK color components
203:      *
204:      * @return array with keys ('cyan', 'magenta', 'yellow', 'key', 'alpha')
205:      */
206:     public function toCmykArray()
207:     {
208:         return array(
209:             'cyan'    => $this->cmp_cyan,
210:             'magenta' => $this->cmp_magenta,
211:             'yellow'  => $this->cmp_yellow,
212:             'key'     => $this->cmp_key,
213:             'alpha'   => $this->cmp_alpha
214:         );
215:     }
216: 
217:     /**
218:      * Invert the color
219:      */
220:     public function invertColor()
221:     {
222:         $this->cmp_cyan    = (1 - $this->cmp_cyan);
223:         $this->cmp_magenta = (1 - $this->cmp_magenta);
224:         $this->cmp_yellow  = (1 - $this->cmp_yellow);
225:         $this->cmp_key     = (1 - $this->cmp_key);
226:         return $this;
227:     }
228: }
229: 
 

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