PageRenderTime 36ms CodeModel.GetById 12ms app.highlight 20ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/vendor/zend/Zend/Pdf/Color/Html.php

http://zoop.googlecode.com/
PHP | 412 lines | 331 code | 12 blank | 69 comment | 13 complexity | 6d733b74b820b5fb9022d00c8ccfc29d MD5 | raw file
  1<?php
  2/**
  3 * Zend Framework
  4 *
  5 * LICENSE
  6 *
  7 * This source file is subject to the new BSD license that is bundled
  8 * with this package in the file LICENSE.txt.
  9 * It is also available through the world-wide-web at this URL:
 10 * http://framework.zend.com/license/new-bsd
 11 * If you did not receive a copy of the license and are unable to
 12 * obtain it through the world-wide-web, please send an email
 13 * to license@zend.com so we can send you a copy immediately.
 14 *
 15 * @category   Zend
 16 * @package    Zend_Pdf
 17 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 18 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 19 * @version    $Id: Html.php 20096 2010-01-06 02:05:09Z bkarwin $
 20 */
 21
 22/** Zend_Pdf_Color */
 23require_once 'Zend/Pdf/Color.php';
 24
 25
 26/**
 27 * HTML color implementation
 28 *
 29 * Factory class which vends Zend_Pdf_Color objects from typical HTML
 30 * representations.
 31 *
 32 * @category   Zend
 33 * @package    Zend_Pdf
 34 * @copyright  Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
 35 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 36 */
 37class Zend_Pdf_Color_Html extends Zend_Pdf_Color
 38{
 39
 40    /**
 41     * Color
 42     *
 43     * @var Zend_Pdf_Color
 44     */
 45    private $_color;
 46
 47    /**
 48     * Class constructor.
 49     *
 50     * @param mixed $color
 51     * @throws Zend_Pdf_Exception
 52     */
 53    public function __construct($color)
 54    {
 55        $this->_color = self::color($color);
 56    }
 57
 58
 59    /**
 60     * Instructions, which can be directly inserted into content stream
 61     * to switch color.
 62     * Color set instructions differ for stroking and nonstroking operations.
 63     *
 64     * @param boolean $stroking
 65     * @return string
 66     */
 67    public function instructions($stroking)
 68    {
 69        return $this->_color->instructions($stroking);
 70    }
 71
 72    /**
 73     * Get color components (color space dependent)
 74     *
 75     * @return array
 76     */
 77    public function getComponents()
 78    {
 79        return $this->_color->getComponents();
 80    }
 81
 82    /**
 83     * Creates a Zend_Pdf_Color object from the HTML representation.
 84     *
 85     * @param string $color May either be a hexidecimal number of the form
 86     *    #rrggbb or one of the 140 well-known names (black, white, blue, etc.)
 87     * @return Zend_Pdf_Color
 88     */
 89    public static function color($color)
 90    {
 91        $pattern = '/^#([A-Fa-f0-9]{2})([A-Fa-f0-9]{2})([A-Fa-f0-9]{2})$/';
 92        if (preg_match($pattern, $color, $matches)) {
 93            $r = round((hexdec($matches[1]) / 255), 3);
 94            $g = round((hexdec($matches[2]) / 255), 3);
 95            $b = round((hexdec($matches[3]) / 255), 3);
 96            if (($r == $g) && ($g == $b)) {
 97                require_once 'Zend/Pdf/Color/GrayScale.php';
 98                return new Zend_Pdf_Color_GrayScale($r);
 99            } else {
100                require_once 'Zend/Pdf/Color/Rgb.php';
101                return new Zend_Pdf_Color_Rgb($r, $g, $b);
102            }
103        } else {
104            return Zend_Pdf_Color_Html::namedColor($color);
105        }
106    }
107
108    /**
109     * Creates a Zend_Pdf_Color object from the named color.
110     *
111     * @param string $color One of the 140 well-known color names (black, white,
112     *    blue, etc.)
113     * @return Zend_Pdf_Color
114     */
115    public static function namedColor($color)
116    {
117        switch (strtolower($color)) {
118            case 'aqua':
119                $r = 0.0;   $g = 1.0;   $b = 1.0;   break;
120            case 'black':
121                $r = 0.0;   $g = 0.0;   $b = 0.0;   break;
122            case 'blue':
123                $r = 0.0;   $g = 0.0;   $b = 1.0;   break;
124            case 'fuchsia':
125                $r = 1.0;   $g = 0.0;   $b = 1.0;   break;
126            case 'gray':
127                $r = 0.502; $g = 0.502; $b = 0.502; break;
128            case 'green':
129                $r = 0.0;   $g = 0.502; $b = 0.0;   break;
130            case 'lime':
131                $r = 0.0;   $g = 1.0;   $b = 0.0;   break;
132            case 'maroon':
133                $r = 0.502; $g = 0.0;   $b = 0.0;   break;
134            case 'navy':
135                $r = 0.0;   $g = 0.0;   $b = 0.502; break;
136            case 'olive':
137                $r = 0.502; $g = 0.502; $b = 0.0;   break;
138            case 'purple':
139                $r = 0.502; $g = 0.0;   $b = 0.502; break;
140            case 'red':
141                $r = 1.0;   $g = 0.0;   $b = 0.0;   break;
142            case 'silver':
143                $r = 0.753; $g = 0.753; $b = 0.753; break;
144            case 'teal':
145                $r = 0.0;   $g = 0.502; $b = 0.502; break;
146            case 'white':
147                $r = 1.0;   $g = 1.0;   $b = 1.0;   break;
148            case 'yellow':
149                $r = 1.0;   $g = 1.0;   $b = 0.0;   break;
150
151            case 'aliceblue':
152                $r = 0.941; $g = 0.973; $b = 1.0;   break;
153            case 'antiquewhite':
154                $r = 0.980; $g = 0.922; $b = 0.843; break;
155            case 'aquamarine':
156                $r = 0.498; $g = 1.0;   $b = 0.831; break;
157            case 'azure':
158                $r = 0.941; $g = 1.0;   $b = 1.0;   break;
159            case 'beige':
160                $r = 0.961; $g = 0.961; $b = 0.863; break;
161            case 'bisque':
162                $r = 1.0;   $g = 0.894; $b = 0.769; break;
163            case 'blanchedalmond':
164                $r = 1.0;   $g = 1.0;   $b = 0.804; break;
165            case 'blueviolet':
166                $r = 0.541; $g = 0.169; $b = 0.886; break;
167            case 'brown':
168                $r = 0.647; $g = 0.165; $b = 0.165; break;
169            case 'burlywood':
170                $r = 0.871; $g = 0.722; $b = 0.529; break;
171            case 'cadetblue':
172                $r = 0.373; $g = 0.620; $b = 0.627; break;
173            case 'chartreuse':
174                $r = 0.498; $g = 1.0;   $b = 0.0;   break;
175            case 'chocolate':
176                $r = 0.824; $g = 0.412; $b = 0.118; break;
177            case 'coral':
178                $r = 1.0;   $g = 0.498; $b = 0.314; break;
179            case 'cornflowerblue':
180                $r = 0.392; $g = 0.584; $b = 0.929; break;
181            case 'cornsilk':
182                $r = 1.0;   $g = 0.973; $b = 0.863; break;
183            case 'crimson':
184                $r = 0.863; $g = 0.078; $b = 0.235; break;
185            case 'cyan':
186                $r = 0.0;   $g = 1.0;   $b = 1.0;   break;
187            case 'darkblue':
188                $r = 0.0;   $g = 0.0;   $b = 0.545; break;
189            case 'darkcyan':
190                $r = 0.0;   $g = 0.545; $b = 0.545; break;
191            case 'darkgoldenrod':
192                $r = 0.722; $g = 0.525; $b = 0.043; break;
193            case 'darkgray':
194                $r = 0.663; $g = 0.663; $b = 0.663; break;
195            case 'darkgreen':
196                $r = 0.0;   $g = 0.392; $b = 0.0;   break;
197            case 'darkkhaki':
198                $r = 0.741; $g = 0.718; $b = 0.420; break;
199            case 'darkmagenta':
200                $r = 0.545; $g = 0.0;   $b = 0.545; break;
201            case 'darkolivegreen':
202                $r = 0.333; $g = 0.420; $b = 0.184; break;
203            case 'darkorange':
204                $r = 1.0;   $g = 0.549; $b = 0.0;   break;
205            case 'darkorchid':
206                $r = 0.6;   $g = 0.196; $b = 0.8;   break;
207            case 'darkred':
208                $r = 0.545; $g = 0.0;   $b = 0.0;   break;
209            case 'darksalmon':
210                $r = 0.914; $g = 0.588; $b = 0.478; break;
211            case 'darkseagreen':
212                $r = 0.561; $g = 0.737; $b = 0.561; break;
213            case 'darkslateblue':
214                $r = 0.282; $g = 0.239; $b = 0.545; break;
215            case 'darkslategray':
216                $r = 0.184; $g = 0.310; $b = 0.310; break;
217            case 'darkturquoise':
218                $r = 0.0;   $g = 0.808; $b = 0.820; break;
219            case 'darkviolet':
220                $r = 0.580; $g = 0.0;   $b = 0.827; break;
221            case 'deeppink':
222                $r = 1.0;   $g = 0.078; $b = 0.576; break;
223            case 'deepskyblue':
224                $r = 0.0;   $g = 0.749; $b = 1.0;   break;
225            case 'dimgray':
226                $r = 0.412; $g = 0.412; $b = 0.412; break;
227            case 'dodgerblue':
228                $r = 0.118; $g = 0.565; $b = 1.0;   break;
229            case 'firebrick':
230                $r = 0.698; $g = 0.133; $b = 0.133; break;
231            case 'floralwhite':
232                $r = 1.0;   $g = 0.980; $b = 0.941; break;
233            case 'forestgreen':
234                $r = 0.133; $g = 0.545; $b = 0.133; break;
235            case 'gainsboro':
236                $r = 0.863; $g = 0.863; $b = 0.863; break;
237            case 'ghostwhite':
238                $r = 0.973; $g = 0.973; $b = 1.0;   break;
239            case 'gold':
240                $r = 1.0;   $g = 0.843; $b = 0.0;   break;
241            case 'goldenrod':
242                $r = 0.855; $g = 0.647; $b = 0.125; break;
243            case 'greenyellow':
244                $r = 0.678; $g = 1.0;   $b = 0.184; break;
245            case 'honeydew':
246                $r = 0.941; $g = 1.0;   $b = 0.941; break;
247            case 'hotpink':
248                $r = 1.0;   $g = 0.412; $b = 0.706; break;
249            case 'indianred':
250                $r = 0.804; $g = 0.361; $b = 0.361; break;
251            case 'indigo':
252                $r = 0.294; $g = 0.0;   $b = 0.510; break;
253            case 'ivory':
254                $r = 1.0;   $g = 0.941; $b = 0.941; break;
255            case 'khaki':
256                $r = 0.941; $g = 0.902; $b = 0.549; break;
257            case 'lavender':
258                $r = 0.902; $g = 0.902; $b = 0.980; break;
259            case 'lavenderblush':
260                $r = 1.0;   $g = 0.941; $b = 0.961; break;
261            case 'lawngreen':
262                $r = 0.486; $g = 0.988; $b = 0.0;   break;
263            case 'lemonchiffon':
264                $r = 1.0;   $g = 0.980; $b = 0.804; break;
265            case 'lightblue':
266                $r = 0.678; $g = 0.847; $b = 0.902; break;
267            case 'lightcoral':
268                $r = 0.941; $g = 0.502; $b = 0.502; break;
269            case 'lightcyan':
270                $r = 0.878; $g = 1.0;   $b = 1.0;   break;
271            case 'lightgoldenrodyellow':
272                $r = 0.980; $g = 0.980; $b = 0.824; break;
273            case 'lightgreen':
274                $r = 0.565; $g = 0.933; $b = 0.565; break;
275            case 'lightgrey':
276                $r = 0.827; $g = 0.827; $b = 0.827; break;
277            case 'lightpink':
278                $r = 1.0;   $g = 0.714; $b = 0.757; break;
279            case 'lightsalmon':
280                $r = 1.0;   $g = 0.627; $b = 0.478; break;
281            case 'lightseagreen':
282                $r = 0.125; $g = 0.698; $b = 0.667; break;
283            case 'lightskyblue':
284                $r = 0.529; $g = 0.808; $b = 0.980; break;
285            case 'lightslategray':
286                $r = 0.467; $g = 0.533; $b = 0.6;   break;
287            case 'lightsteelblue':
288                $r = 0.690; $g = 0.769; $b = 0.871; break;
289            case 'lightyellow':
290                $r = 1.0;   $g = 1.0;   $b = 0.878; break;
291            case 'limegreen':
292                $r = 0.196; $g = 0.804; $b = 0.196; break;
293            case 'linen':
294                $r = 0.980; $g = 0.941; $b = 0.902; break;
295            case 'magenta':
296                $r = 1.0;   $g = 0.0;   $b = 1.0;   break;
297            case 'mediumaquamarine':
298                $r = 0.4;   $g = 0.804; $b = 0.667; break;
299            case 'mediumblue':
300                $r = 0.0;   $g = 0.0;   $b = 0.804; break;
301            case 'mediumorchid':
302                $r = 0.729; $g = 0.333; $b = 0.827; break;
303            case 'mediumpurple':
304                $r = 0.576; $g = 0.439; $b = 0.859; break;
305            case 'mediumseagreen':
306                $r = 0.235; $g = 0.702; $b = 0.443; break;
307            case 'mediumslateblue':
308                $r = 0.482; $g = 0.408; $b = 0.933; break;
309            case 'mediumspringgreen':
310                $r = 0.0;   $g = 0.980; $b = 0.604; break;
311            case 'mediumturquoise':
312                $r = 0.282; $g = 0.820; $b = 0.8;   break;
313            case 'mediumvioletred':
314                $r = 0.780; $g = 0.082; $b = 0.522; break;
315            case 'midnightblue':
316                $r = 0.098; $g = 0.098; $b = 0.439; break;
317            case 'mintcream':
318                $r = 0.961; $g = 1.0;   $b = 0.980; break;
319            case 'mistyrose':
320                $r = 1.0;   $g = 0.894; $b = 0.882; break;
321            case 'moccasin':
322                $r = 1.0;   $g = 0.894; $b = 0.710; break;
323            case 'navajowhite':
324                $r = 1.0;   $g = 0.871; $b = 0.678; break;
325            case 'oldlace':
326                $r = 0.992; $g = 0.961; $b = 0.902; break;
327            case 'olivedrab':
328                $r = 0.420; $g = 0.557; $b = 0.137; break;
329            case 'orange':
330                $r = 1.0;   $g = 0.647; $b = 0.0;   break;
331            case 'orangered':
332                $r = 1.0;   $g = 0.271; $b = 0.0;   break;
333            case 'orchid':
334                $r = 0.855; $g = 0.439; $b = 0.839; break;
335            case 'palegoldenrod':
336                $r = 0.933; $g = 0.910; $b = 0.667; break;
337            case 'palegreen':
338                $r = 0.596; $g = 0.984; $b = 0.596; break;
339            case 'paleturquoise':
340                $r = 0.686; $g = 0.933; $b = 0.933; break;
341            case 'palevioletred':
342                $r = 0.859; $g = 0.439; $b = 0.576; break;
343            case 'papayawhip':
344                $r = 1.0;   $g = 0.937; $b = 0.835; break;
345            case 'peachpuff':
346                $r = 1.0;   $g = 0.937; $b = 0.835; break;
347            case 'peru':
348                $r = 0.804; $g = 0.522; $b = 0.247; break;
349            case 'pink':
350                $r = 1.0;   $g = 0.753; $b = 0.796; break;
351            case 'plum':
352                $r = 0.867; $g = 0.627; $b = 0.867; break;
353            case 'powderblue':
354                $r = 0.690; $g = 0.878; $b = 0.902; break;
355            case 'rosybrown':
356                $r = 0.737; $g = 0.561; $b = 0.561; break;
357            case 'royalblue':
358                $r = 0.255; $g = 0.412; $b = 0.882; break;
359            case 'saddlebrown':
360                $r = 0.545; $g = 0.271; $b = 0.075; break;
361            case 'salmon':
362                $r = 0.980; $g = 0.502; $b = 0.447; break;
363            case 'sandybrown':
364                $r = 0.957; $g = 0.643; $b = 0.376; break;
365            case 'seagreen':
366                $r = 0.180; $g = 0.545; $b = 0.341; break;
367            case 'seashell':
368                $r = 1.0;   $g = 0.961; $b = 0.933; break;
369            case 'sienna':
370                $r = 0.627; $g = 0.322; $b = 0.176; break;
371            case 'skyblue':
372                $r = 0.529; $g = 0.808; $b = 0.922; break;
373            case 'slateblue':
374                $r = 0.416; $g = 0.353; $b = 0.804; break;
375            case 'slategray':
376                $r = 0.439; $g = 0.502; $b = 0.565; break;
377            case 'snow':
378                $r = 1.0;   $g = 0.980; $b = 0.980; break;
379            case 'springgreen':
380                $r = 0.0;   $g = 1.0;   $b = 0.498; break;
381            case 'steelblue':
382                $r = 0.275; $g = 0.510; $b = 0.706; break;
383            case 'tan':
384                $r = 0.824; $g = 0.706; $b = 0.549; break;
385            case 'thistle':
386                $r = 0.847; $g = 0.749; $b = 0.847; break;
387            case 'tomato':
388                $r = 0.992; $g = 0.388; $b = 0.278; break;
389            case 'turquoise':
390                $r = 0.251; $g = 0.878; $b = 0.816; break;
391            case 'violet':
392                $r = 0.933; $g = 0.510; $b = 0.933; break;
393            case 'wheat':
394                $r = 0.961; $g = 0.871; $b = 0.702; break;
395            case 'whitesmoke':
396                $r = 0.961; $g = 0.961; $b = 0.961; break;
397            case 'yellowgreen':
398                $r = 0.604; $g = 0.804; $b = 0.196; break;
399
400            default:
401                require_once 'Zend/Pdf/Exception.php';
402                throw new Zend_Pdf_Exception('Unknown color name: ' . $color);
403        }
404        if (($r == $g) && ($g == $b)) {
405            require_once 'Zend/Pdf/Color/GrayScale.php';
406            return new Zend_Pdf_Color_GrayScale($r);
407        } else {
408            require_once 'Zend/Pdf/Color/Rgb.php';
409            return new Zend_Pdf_Color_Rgb($r, $g, $b);
410        }
411    }
412}