source-class-Com.Tecnick.Color.Spot

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:  * Spot.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;
 17: 
 18: use \Com\Tecnick\Color\Exception as ColorException;
 19: use \Com\Tecnick\Color\Model\Cmyk;
 20: 
 21: /**
 22:  * Com\Tecnick\Color\Spot
 23:  *
 24:  * Spot Color class
 25:  *
 26:  * @since       2015-02-21
 27:  * @category    Library
 28:  * @package     Color
 29:  * @author      Nicola Asuni <info@tecnick.com>
 30:  * @copyright   2015-2015 Nicola Asuni - Tecnick.com LTD
 31:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 32:  * @link        https://github.com/tecnickcom/tc-lib-color
 33:  */
 34: class Spot extends \Com\Tecnick\Color\Web
 35: {
 36:     /**
 37:      * Array of default Spot colors
 38:      * Color keys must be in lowercase and without spaces.
 39:      *
 40:      * @var array
 41:      */
 42:     protected static $default_spot_colors = array (
 43:         'none' => array('name' => 'None',
 44:             'color' => array('cyan' => 0, 'magenta' => 0, 'yellow' => 0, 'key' => 0, 'alpha' => 1)),
 45:         'all' => array('name' => 'All',
 46:             'color' => array('cyan' => 1, 'magenta' => 1, 'yellow' => 1, 'key' => 1, 'alpha' => 1)),
 47:         'cyan' => array('name' => 'Cyan',
 48:             'color' => array('cyan' => 1, 'magenta' => 0, 'yellow' => 0, 'key' => 0, 'alpha' => 1)),
 49:         'magenta' => array('name' => 'Magenta',
 50:             'color' => array('cyan' => 0, 'magenta' => 1, 'yellow' => 0, 'key' => 0, 'alpha' => 1)),
 51:         'yellow' => array('name' => 'Yellow',
 52:             'color' => array('cyan' => 0, 'magenta' => 0, 'yellow' => 1, 'key' => 0, 'alpha' => 1)),
 53:         'key' => array('name' => 'Key',
 54:             'color' => array('cyan' => 0, 'magenta' => 0, 'yellow' => 0, 'key' => 1, 'alpha' => 1)),
 55:         'white' => array('name' => 'White',
 56:             'color' => array('cyan' => 0, 'magenta' => 0, 'yellow' => 0, 'key' => 0, 'alpha' => 1)),
 57:         'black' => array('name' => 'Black',
 58:             'color' => array('cyan' => 0, 'magenta' => 0, 'yellow' => 0, 'key' => 1, 'alpha' => 1)),
 59:         'red' => array('name' => 'Red',
 60:             'color' => array('cyan' => 0, 'magenta' => 1, 'yellow' => 1, 'key' => 0, 'alpha' => 1)),
 61:         'green' => array('name' => 'Green',
 62:             'color' => array('cyan' => 1, 'magenta' => 0, 'yellow' => 1, 'key' => 0, 'alpha' => 1)),
 63:         'blue' => array('name' => 'Blue',
 64:             'color' => array('cyan' => 1, 'magenta' => 1, 'yellow' => 0, 'key' => 0, 'alpha' => 1)),
 65:     );
 66:     
 67:     /**
 68:      * Array of Spot colors
 69:      *
 70:      * @var array
 71:      */
 72:     protected $spot_colors = array();
 73: 
 74:     /**
 75:      * Returns the array of spot colors.
 76:      *
 77:      * @return array Spot colors array.
 78:      */
 79:     public function getSpotColors()
 80:     {
 81:         return $this->spot_colors;
 82:     }
 83: 
 84:     /**
 85:      * Return the normalized version of the spot color name
 86:      *
 87:      * @param string $name Full name of the spot color.
 88:      *
 89:      * @return string
 90:      */
 91:     public function normalizeSpotColorName($name)
 92:     {
 93:         return preg_replace('/[^a-z0-9]*/', '', strtolower($name));
 94:     }
 95: 
 96:     /**
 97:      * Return the requested spot color data array
 98:      *
 99:      * @param string $name Full name of the spot color.
100:      *
101:      * @return array
102:      *
103:      * @throws ColorException if the color is not found
104:      */
105:     public function getSpotColor($name)
106:     {
107:         $key = $this->normalizeSpotColorName($name);
108:         if (empty($this->spot_colors[$key])) {
109:             // search on default spot colors
110:             if (empty(self::$default_spot_colors[$key])) {
111:                 throw new ColorException('unable to find the spot color: '.$key);
112:             }
113:             $this->addSpotColor($key, new Cmyk(self::$default_spot_colors[$key]['color']));
114:         }
115:         return $this->spot_colors[$key];
116:     }
117: 
118:     /**
119:      * Return the requested spot color CMYK object
120:      *
121:      * @param string $name Full name of the spot color.
122:      *
123:      * @return \Com\Tecnick\Color\Web\Model\Cmyk
124:      *
125:      * @throws ColorException if the color is not found
126:      */
127:     public function getSpotColorObj($name)
128:     {
129:         $spot = $this->getSpotColor($name);
130:         return $spot['color'];
131:     }
132: 
133:     /**
134:      * Add a new spot color or overwrite an existing one with the same name.
135:      *
136:      * @param string $name  Full name of the spot color.
137:      * @param Cmyk   $color CMYK color object
138:      */
139:     public function addSpotColor($name, Cmyk $color)
140:     {
141:         $key = $this->normalizeSpotColorName($name);
142:         if (isset($this->spot_colors[$key])) {
143:             $num = $this->spot_colors[$key]['i'];
144:         } else {
145:             $num = (count($this->spot_colors) + 1);
146:         }
147:         $this->spot_colors[$key] = array(
148:             'i'     => $num,   // color index
149:             'n'     => 0,      // PDF object number
150:             'name'  => $name,  // color name (key)
151:             'color' => $color, // CMYK color object
152:         );
153:     }
154: 
155:     /**
156:      * Returns the PDF command to output Spot color objects.
157:      *
158:      * @param int $pon Current PDF object number
159:      *
160:      * @return string PDF command
161:      */
162:     public function getPdfSpotObjects(&$pon)
163:     {
164:         $out = '';
165:         foreach ($this->spot_colors as $name => $color) {
166:             $out .= (++$pon).' 0 obj'."\n";
167:             $this->spot_colors[$name]['n'] = $pon;
168:             $out .= '[/Separation /'.str_replace(' ', '#20', $name)
169:                 .' /DeviceCMYK <<'
170:                 .'/Range [0 1 0 1 0 1 0 1]'
171:                 .' /C0 [0 0 0 0]'
172:                 .' /C1 ['.$color['color']->getComponentsString().']'
173:                 .' /FunctionType 2'
174:                 .' /Domain [0 1]'
175:                 .' /N 1'
176:                 .'>>]'."\n"
177:                 .'endobj'."\n";
178:         }
179:         return $out;
180:     }
181: 
182:     /**
183:      * Returns the PDF command to output Spot color resources.
184:      *
185:      * @return string PDF command
186:      */
187:     public function getPdfSpotResources()
188:     {
189:         if (empty($this->spot_colors)) {
190:             return '';
191:         }
192:         $out = '/ColorSpace << ';
193:         foreach ($this->spot_colors as $color) {
194:             $out .= '/CS'.$color['i'].' '.$color['n'].' 0 R ';
195:         }
196:         $out .= '>>'."\n";
197:         return $out;
198:     }
199: }
200: 
 

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