source-class-Com.Tecnick.Color.Css

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:  * Css.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: 
 20: /**
 21:  * Com\Tecnick\Color\Css
 22:  *
 23:  * Css Color class
 24:  *
 25:  * @since       2015-02-21
 26:  * @category    Library
 27:  * @package     Color
 28:  * @author      Nicola Asuni <info@tecnick.com>
 29:  * @copyright   2015-2015 Nicola Asuni - Tecnick.com LTD
 30:  * @license     http://www.gnu.org/copyleft/lesser.html GNU-LGPL v3 (see LICENSE.TXT)
 31:  * @link        https://github.com/tecnickcom/tc-lib-color
 32:  */
 33: abstract class Css
 34: {
 35:     /**
 36:      * Get the color object from acrobat Javascript syntax
 37:      *
 38:      * @param string $color color specification (e.g.: ["RGB",0.1,0.3,1])
 39:      *
 40:      * @return \Com\Tecnick\Color\Model or null for transparent color
 41:      *
 42:      * @throws ColorException if the color is not found
 43:      */
 44:     protected function getColorObjFromJs($color)
 45:     {
 46:         if (!isset($color[2]) || (strpos('tgrc', $color[2]) === false)) {
 47:              throw new ColorException('invalid javascript color: '.$color);
 48:         }
 49:         switch ($color[2]) {
 50:             case 'g':
 51:                 $rex = '/[\[][\"\']g[\"\'][\,]([0-9\.]+)[\]]/';
 52:                 if (preg_match($rex, $color, $col) !== 1) {
 53:                     throw new ColorException('invalid javascript color: '.$color);
 54:                 }
 55:                 return new \Com\Tecnick\Color\Model\Gray(array('gray' => $col[1], 'alpha' => 1));
 56:             case 'r':
 57:                 $rex = '/[\[][\"\']rgb[\"\'][\,]([0-9\.]+)[\,]([0-9\.]+)[\,]([0-9\.]+)[\]]/';
 58:                 if (preg_match($rex, $color, $col) !== 1) {
 59:                     throw new ColorException('invalid javascript color: '.$color);
 60:                 }
 61:                 return new \Com\Tecnick\Color\Model\Rgb(
 62:                     array(
 63:                         'red'   => $col[1],
 64:                         'green' => $col[2],
 65:                         'blue'  => $col[3],
 66:                         'alpha' => 1
 67:                     )
 68:                 );
 69:             case 'c':
 70:                 $rex = '/[\[][\"\']cmyk[\"\'][\,]([0-9\.]+)[\,]([0-9\.]+)[\,]([0-9\.]+)[\,]([0-9\.]+)[\]]/';
 71:                 if (preg_match($rex, $color, $col) !== 1) {
 72:                     throw new ColorException('invalid javascript color: '.$color);
 73:                 }
 74:                 return new \Com\Tecnick\Color\Model\Cmyk(
 75:                     array(
 76:                         'cyan'    => $col[1],
 77:                         'magenta' => $col[2],
 78:                         'yellow'  => $col[3],
 79:                         'key'     => $col[4],
 80:                         'alpha'   => 1
 81:                     )
 82:                 );
 83:         }
 84:         // case 't'
 85:         return null;
 86:     }
 87: 
 88:     /**
 89:      * Get the color object from a CSS color string
 90:      *
 91:      * @param string $type  color type: t, g, rgb, rgba, hsl, hsla, cmyk
 92:      * @param string $color color specification (e.g.: rgb(255,128,64))
 93:      *
 94:      * @return \Com\Tecnick\Color\Model or null for transparency (default)
 95:      *
 96:      * @throws ColorException if the color is not found
 97:      */
 98:     protected function getColorObjFromCss($type, $color)
 99:     {
100:         switch ($type) {
101:             case 'g':
102:                 return $this->getColorObjFromCssGray($color);
103:             case 'rgb':
104:             case 'rgba':
105:                 return $this->getColorObjFromCssRgb($color);
106:             case 'hsl':
107:             case 'hsla':
108:                 return $this->getColorObjFromCssHsl($color);
109:             case 'cmyk':
110:             case 'cmyka':
111:                 return $this->getColorObjFromCssCmyk($color);
112:         }
113:         // case 't'
114:         return null;
115:     }
116: 
117:     /**
118:      * Get the color object from a CSS CMYK color string
119:      *
120:      * @param string $color color specification (e.g.: rgb(255,128,64))
121:      *
122:      * @return \Com\Tecnick\Color\Model
123:      *
124:      * @throws ColorException if the color is not found
125:      */
126:     private function getColorObjFromCssGray($color)
127:     {
128:         $rex = '/[\(]([0-9\%]+)[\)]/';
129:         if (preg_match($rex, $color, $col) !== 1) {
130:             throw new ColorException('invalid css color: '.$color);
131:         }
132:         return new \Com\Tecnick\Color\Model\Gray(
133:             array(
134:                 'gray' => $this->normalizeValue($col[1], 255),
135:                 'alpha' => 1
136:             )
137:         );
138:     }
139: 
140:     /**
141:      * Get the color object from a CSS CMYK color string
142:      *
143:      * @param string $color color specification (e.g.: rgb(255,128,64))
144:      *
145:      * @return \Com\Tecnick\Color\Model
146:      *
147:      * @throws ColorException if the color is not found
148:      */
149:     private function getColorObjFromCssRgb($color)
150:     {
151:         $rex = '/[\(]([0-9\%]+)[\,]([0-9\%]+)[\,]([0-9\%]+)[\,]?([0-9\.]*)[\)]/';
152:         if (preg_match($rex, $color, $col) !== 1) {
153:             throw new ColorException('invalid css color: '.$color);
154:         }
155:         return new \Com\Tecnick\Color\Model\Rgb(
156:             array(
157:                 'red'   => $this->normalizeValue($col[1], 255),
158:                 'green' => $this->normalizeValue($col[2], 255),
159:                 'blue'  => $this->normalizeValue($col[3], 255),
160:                 'alpha' => (isset($col[4][0]) ? $col[4] : 1)
161:             )
162:         );
163:     }
164: 
165:     /**
166:      * Get the color object from a CSS CMYK color string
167:      *
168:      * @param string $color color specification (e.g.: rgb(255,128,64))
169:      *
170:      * @return \Com\Tecnick\Color\Model
171:      *
172:      * @throws ColorException if the color is not found
173:      */
174:     private function getColorObjFromCssHsl($color)
175:     {
176:         $rex = '/[\(]([0-9\%]+)[\,]([0-9\%]+)[\,]([0-9\%]+)[\,]?([0-9\.]*)[\)]/';
177:         if (preg_match($rex, $color, $col) !== 1) {
178:             throw new ColorException('invalid css color: '.$color);
179:         }
180:         return new \Com\Tecnick\Color\Model\Hsl(
181:             array(
182:                 'hue'        => $this->normalizeValue($col[1], 360),
183:                 'saturation' => $this->normalizeValue($col[2], 1),
184:                 'lightness'  => $this->normalizeValue($col[3], 1),
185:                 'alpha'      => (isset($col[4][0]) ? $col[4] : 1)
186:             )
187:         );
188:     }
189: 
190:     /**
191:      * Get the color object from a CSS CMYK color string
192:      *
193:      * @param string $color color specification (e.g.: rgb(255,128,64))
194:      *
195:      * @return \Com\Tecnick\Color\Model
196:      *
197:      * @throws ColorException if the color is not found
198:      */
199:     private function getColorObjFromCssCmyk($color)
200:     {
201:         $rex = '/[\(]([0-9\%]+)[\,]([0-9\%]+)[\,]([0-9\%]+)[\,]([0-9\%]+)[\,]?([0-9\.]*)[\)]/';
202:         if (preg_match($rex, $color, $col) !== 1) {
203:             throw new ColorException('invalid css color: '.$color);
204:         }
205:         return new \Com\Tecnick\Color\Model\Cmyk(
206:             array(
207:                 'cyan'    => $this->normalizeValue($col[1], 100),
208:                 'magenta' => $this->normalizeValue($col[2], 100),
209:                 'yellow'  => $this->normalizeValue($col[3], 100),
210:                 'key'     => $this->normalizeValue($col[4], 100),
211:                 'alpha'   => (isset($col[5][0]) ? $col[5] : 1)
212:             )
213:         );
214:     }
215: }
216: 
 

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