File indexing completed on 2024-12-22 05:36:54

0001 <?php
0002 /**
0003  * Zend Framework
0004  *
0005  * LICENSE
0006  *
0007  * This source file is subject to the new BSD license that is bundled
0008  * with this package in the file LICENSE.txt.
0009  * It is also available through the world-wide-web at this URL:
0010  * http://framework.zend.com/license/new-bsd
0011  * If you did not receive a copy of the license and are unable to
0012  * obtain it through the world-wide-web, please send an email
0013  * to license@zend.com so we can send you a copy immediately.
0014  *
0015  * @category   Zend
0016  * @package    Zend_Pdf
0017  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0018  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0019  * @version    $Id$
0020  */
0021 
0022 /** Zend_Pdf_Color */
0023 // require_once 'Zend/Pdf/Color.php';
0024 
0025 
0026 /**
0027  * HTML color implementation
0028  *
0029  * Factory class which vends Zend_Pdf_Color objects from typical HTML
0030  * representations.
0031  *
0032  * @category   Zend
0033  * @package    Zend_Pdf
0034  * @copyright  Copyright (c) 2005-2015 Zend Technologies USA Inc. (http://www.zend.com)
0035  * @license    http://framework.zend.com/license/new-bsd     New BSD License
0036  */
0037 class Zend_Pdf_Color_Html extends Zend_Pdf_Color
0038 {
0039 
0040     /**
0041      * Color
0042      *
0043      * @var Zend_Pdf_Color
0044      */
0045     private $_color;
0046 
0047     /**
0048      * Class constructor.
0049      *
0050      * @param mixed $color
0051      * @throws Zend_Pdf_Exception
0052      */
0053     public function __construct($color)
0054     {
0055         $this->_color = self::color($color);
0056     }
0057 
0058 
0059     /**
0060      * Instructions, which can be directly inserted into content stream
0061      * to switch color.
0062      * Color set instructions differ for stroking and nonstroking operations.
0063      *
0064      * @param boolean $stroking
0065      * @return string
0066      */
0067     public function instructions($stroking)
0068     {
0069         return $this->_color->instructions($stroking);
0070     }
0071 
0072     /**
0073      * Get color components (color space dependent)
0074      *
0075      * @return array
0076      */
0077     public function getComponents()
0078     {
0079         return $this->_color->getComponents();
0080     }
0081 
0082     /**
0083      * Creates a Zend_Pdf_Color object from the HTML representation.
0084      *
0085      * @param string $color May either be a hexidecimal number of the form
0086      *    #rrggbb or one of the 140 well-known names (black, white, blue, etc.)
0087      * @return Zend_Pdf_Color
0088      */
0089     public static function color($color)
0090     {
0091         $pattern = '/^#([A-Fa-f0-9]{2})([A-Fa-f0-9]{2})([A-Fa-f0-9]{2})$/';
0092         if (preg_match($pattern, $color, $matches)) {
0093             $r = round((hexdec($matches[1]) / 255), 3);
0094             $g = round((hexdec($matches[2]) / 255), 3);
0095             $b = round((hexdec($matches[3]) / 255), 3);
0096             if (($r == $g) && ($g == $b)) {
0097                 // require_once 'Zend/Pdf/Color/GrayScale.php';
0098                 return new Zend_Pdf_Color_GrayScale($r);
0099             } else {
0100                 // require_once 'Zend/Pdf/Color/Rgb.php';
0101                 return new Zend_Pdf_Color_Rgb($r, $g, $b);
0102             }
0103         } else {
0104             return Zend_Pdf_Color_Html::namedColor($color);
0105         }
0106     }
0107 
0108     /**
0109      * Creates a Zend_Pdf_Color object from the named color.
0110      *
0111      * @param string $color One of the 140 well-known color names (black, white,
0112      *    blue, etc.)
0113      * @return Zend_Pdf_Color
0114      */
0115     public static function namedColor($color)
0116     {
0117         switch (strtolower($color)) {
0118             case 'aqua':
0119                 $r = 0.0;   $g = 1.0;   $b = 1.0;   break;
0120             case 'black':
0121                 $r = 0.0;   $g = 0.0;   $b = 0.0;   break;
0122             case 'blue':
0123                 $r = 0.0;   $g = 0.0;   $b = 1.0;   break;
0124             case 'fuchsia':
0125                 $r = 1.0;   $g = 0.0;   $b = 1.0;   break;
0126             case 'gray':
0127                 $r = 0.502; $g = 0.502; $b = 0.502; break;
0128             case 'green':
0129                 $r = 0.0;   $g = 0.502; $b = 0.0;   break;
0130             case 'lime':
0131                 $r = 0.0;   $g = 1.0;   $b = 0.0;   break;
0132             case 'maroon':
0133                 $r = 0.502; $g = 0.0;   $b = 0.0;   break;
0134             case 'navy':
0135                 $r = 0.0;   $g = 0.0;   $b = 0.502; break;
0136             case 'olive':
0137                 $r = 0.502; $g = 0.502; $b = 0.0;   break;
0138             case 'purple':
0139                 $r = 0.502; $g = 0.0;   $b = 0.502; break;
0140             case 'red':
0141                 $r = 1.0;   $g = 0.0;   $b = 0.0;   break;
0142             case 'silver':
0143                 $r = 0.753; $g = 0.753; $b = 0.753; break;
0144             case 'teal':
0145                 $r = 0.0;   $g = 0.502; $b = 0.502; break;
0146             case 'white':
0147                 $r = 1.0;   $g = 1.0;   $b = 1.0;   break;
0148             case 'yellow':
0149                 $r = 1.0;   $g = 1.0;   $b = 0.0;   break;
0150 
0151             case 'aliceblue':
0152                 $r = 0.941; $g = 0.973; $b = 1.0;   break;
0153             case 'antiquewhite':
0154                 $r = 0.980; $g = 0.922; $b = 0.843; break;
0155             case 'aquamarine':
0156                 $r = 0.498; $g = 1.0;   $b = 0.831; break;
0157             case 'azure':
0158                 $r = 0.941; $g = 1.0;   $b = 1.0;   break;
0159             case 'beige':
0160                 $r = 0.961; $g = 0.961; $b = 0.863; break;
0161             case 'bisque':
0162                 $r = 1.0;   $g = 0.894; $b = 0.769; break;
0163             case 'blanchedalmond':
0164                 $r = 1.0;   $g = 1.0;   $b = 0.804; break;
0165             case 'blueviolet':
0166                 $r = 0.541; $g = 0.169; $b = 0.886; break;
0167             case 'brown':
0168                 $r = 0.647; $g = 0.165; $b = 0.165; break;
0169             case 'burlywood':
0170                 $r = 0.871; $g = 0.722; $b = 0.529; break;
0171             case 'cadetblue':
0172                 $r = 0.373; $g = 0.620; $b = 0.627; break;
0173             case 'chartreuse':
0174                 $r = 0.498; $g = 1.0;   $b = 0.0;   break;
0175             case 'chocolate':
0176                 $r = 0.824; $g = 0.412; $b = 0.118; break;
0177             case 'coral':
0178                 $r = 1.0;   $g = 0.498; $b = 0.314; break;
0179             case 'cornflowerblue':
0180                 $r = 0.392; $g = 0.584; $b = 0.929; break;
0181             case 'cornsilk':
0182                 $r = 1.0;   $g = 0.973; $b = 0.863; break;
0183             case 'crimson':
0184                 $r = 0.863; $g = 0.078; $b = 0.235; break;
0185             case 'cyan':
0186                 $r = 0.0;   $g = 1.0;   $b = 1.0;   break;
0187             case 'darkblue':
0188                 $r = 0.0;   $g = 0.0;   $b = 0.545; break;
0189             case 'darkcyan':
0190                 $r = 0.0;   $g = 0.545; $b = 0.545; break;
0191             case 'darkgoldenrod':
0192                 $r = 0.722; $g = 0.525; $b = 0.043; break;
0193             case 'darkgray':
0194                 $r = 0.663; $g = 0.663; $b = 0.663; break;
0195             case 'darkgreen':
0196                 $r = 0.0;   $g = 0.392; $b = 0.0;   break;
0197             case 'darkkhaki':
0198                 $r = 0.741; $g = 0.718; $b = 0.420; break;
0199             case 'darkmagenta':
0200                 $r = 0.545; $g = 0.0;   $b = 0.545; break;
0201             case 'darkolivegreen':
0202                 $r = 0.333; $g = 0.420; $b = 0.184; break;
0203             case 'darkorange':
0204                 $r = 1.0;   $g = 0.549; $b = 0.0;   break;
0205             case 'darkorchid':
0206                 $r = 0.6;   $g = 0.196; $b = 0.8;   break;
0207             case 'darkred':
0208                 $r = 0.545; $g = 0.0;   $b = 0.0;   break;
0209             case 'darksalmon':
0210                 $r = 0.914; $g = 0.588; $b = 0.478; break;
0211             case 'darkseagreen':
0212                 $r = 0.561; $g = 0.737; $b = 0.561; break;
0213             case 'darkslateblue':
0214                 $r = 0.282; $g = 0.239; $b = 0.545; break;
0215             case 'darkslategray':
0216                 $r = 0.184; $g = 0.310; $b = 0.310; break;
0217             case 'darkturquoise':
0218                 $r = 0.0;   $g = 0.808; $b = 0.820; break;
0219             case 'darkviolet':
0220                 $r = 0.580; $g = 0.0;   $b = 0.827; break;
0221             case 'deeppink':
0222                 $r = 1.0;   $g = 0.078; $b = 0.576; break;
0223             case 'deepskyblue':
0224                 $r = 0.0;   $g = 0.749; $b = 1.0;   break;
0225             case 'dimgray':
0226                 $r = 0.412; $g = 0.412; $b = 0.412; break;
0227             case 'dodgerblue':
0228                 $r = 0.118; $g = 0.565; $b = 1.0;   break;
0229             case 'firebrick':
0230                 $r = 0.698; $g = 0.133; $b = 0.133; break;
0231             case 'floralwhite':
0232                 $r = 1.0;   $g = 0.980; $b = 0.941; break;
0233             case 'forestgreen':
0234                 $r = 0.133; $g = 0.545; $b = 0.133; break;
0235             case 'gainsboro':
0236                 $r = 0.863; $g = 0.863; $b = 0.863; break;
0237             case 'ghostwhite':
0238                 $r = 0.973; $g = 0.973; $b = 1.0;   break;
0239             case 'gold':
0240                 $r = 1.0;   $g = 0.843; $b = 0.0;   break;
0241             case 'goldenrod':
0242                 $r = 0.855; $g = 0.647; $b = 0.125; break;
0243             case 'greenyellow':
0244                 $r = 0.678; $g = 1.0;   $b = 0.184; break;
0245             case 'honeydew':
0246                 $r = 0.941; $g = 1.0;   $b = 0.941; break;
0247             case 'hotpink':
0248                 $r = 1.0;   $g = 0.412; $b = 0.706; break;
0249             case 'indianred':
0250                 $r = 0.804; $g = 0.361; $b = 0.361; break;
0251             case 'indigo':
0252                 $r = 0.294; $g = 0.0;   $b = 0.510; break;
0253             case 'ivory':
0254                 $r = 1.0;   $g = 0.941; $b = 0.941; break;
0255             case 'khaki':
0256                 $r = 0.941; $g = 0.902; $b = 0.549; break;
0257             case 'lavender':
0258                 $r = 0.902; $g = 0.902; $b = 0.980; break;
0259             case 'lavenderblush':
0260                 $r = 1.0;   $g = 0.941; $b = 0.961; break;
0261             case 'lawngreen':
0262                 $r = 0.486; $g = 0.988; $b = 0.0;   break;
0263             case 'lemonchiffon':
0264                 $r = 1.0;   $g = 0.980; $b = 0.804; break;
0265             case 'lightblue':
0266                 $r = 0.678; $g = 0.847; $b = 0.902; break;
0267             case 'lightcoral':
0268                 $r = 0.941; $g = 0.502; $b = 0.502; break;
0269             case 'lightcyan':
0270                 $r = 0.878; $g = 1.0;   $b = 1.0;   break;
0271             case 'lightgoldenrodyellow':
0272                 $r = 0.980; $g = 0.980; $b = 0.824; break;
0273             case 'lightgreen':
0274                 $r = 0.565; $g = 0.933; $b = 0.565; break;
0275             case 'lightgrey':
0276                 $r = 0.827; $g = 0.827; $b = 0.827; break;
0277             case 'lightpink':
0278                 $r = 1.0;   $g = 0.714; $b = 0.757; break;
0279             case 'lightsalmon':
0280                 $r = 1.0;   $g = 0.627; $b = 0.478; break;
0281             case 'lightseagreen':
0282                 $r = 0.125; $g = 0.698; $b = 0.667; break;
0283             case 'lightskyblue':
0284                 $r = 0.529; $g = 0.808; $b = 0.980; break;
0285             case 'lightslategray':
0286                 $r = 0.467; $g = 0.533; $b = 0.6;   break;
0287             case 'lightsteelblue':
0288                 $r = 0.690; $g = 0.769; $b = 0.871; break;
0289             case 'lightyellow':
0290                 $r = 1.0;   $g = 1.0;   $b = 0.878; break;
0291             case 'limegreen':
0292                 $r = 0.196; $g = 0.804; $b = 0.196; break;
0293             case 'linen':
0294                 $r = 0.980; $g = 0.941; $b = 0.902; break;
0295             case 'magenta':
0296                 $r = 1.0;   $g = 0.0;   $b = 1.0;   break;
0297             case 'mediumaquamarine':
0298                 $r = 0.4;   $g = 0.804; $b = 0.667; break;
0299             case 'mediumblue':
0300                 $r = 0.0;   $g = 0.0;   $b = 0.804; break;
0301             case 'mediumorchid':
0302                 $r = 0.729; $g = 0.333; $b = 0.827; break;
0303             case 'mediumpurple':
0304                 $r = 0.576; $g = 0.439; $b = 0.859; break;
0305             case 'mediumseagreen':
0306                 $r = 0.235; $g = 0.702; $b = 0.443; break;
0307             case 'mediumslateblue':
0308                 $r = 0.482; $g = 0.408; $b = 0.933; break;
0309             case 'mediumspringgreen':
0310                 $r = 0.0;   $g = 0.980; $b = 0.604; break;
0311             case 'mediumturquoise':
0312                 $r = 0.282; $g = 0.820; $b = 0.8;   break;
0313             case 'mediumvioletred':
0314                 $r = 0.780; $g = 0.082; $b = 0.522; break;
0315             case 'midnightblue':
0316                 $r = 0.098; $g = 0.098; $b = 0.439; break;
0317             case 'mintcream':
0318                 $r = 0.961; $g = 1.0;   $b = 0.980; break;
0319             case 'mistyrose':
0320                 $r = 1.0;   $g = 0.894; $b = 0.882; break;
0321             case 'moccasin':
0322                 $r = 1.0;   $g = 0.894; $b = 0.710; break;
0323             case 'navajowhite':
0324                 $r = 1.0;   $g = 0.871; $b = 0.678; break;
0325             case 'oldlace':
0326                 $r = 0.992; $g = 0.961; $b = 0.902; break;
0327             case 'olivedrab':
0328                 $r = 0.420; $g = 0.557; $b = 0.137; break;
0329             case 'orange':
0330                 $r = 1.0;   $g = 0.647; $b = 0.0;   break;
0331             case 'orangered':
0332                 $r = 1.0;   $g = 0.271; $b = 0.0;   break;
0333             case 'orchid':
0334                 $r = 0.855; $g = 0.439; $b = 0.839; break;
0335             case 'palegoldenrod':
0336                 $r = 0.933; $g = 0.910; $b = 0.667; break;
0337             case 'palegreen':
0338                 $r = 0.596; $g = 0.984; $b = 0.596; break;
0339             case 'paleturquoise':
0340                 $r = 0.686; $g = 0.933; $b = 0.933; break;
0341             case 'palevioletred':
0342                 $r = 0.859; $g = 0.439; $b = 0.576; break;
0343             case 'papayawhip':
0344                 $r = 1.0;   $g = 0.937; $b = 0.835; break;
0345             case 'peachpuff':
0346                 $r = 1.0;   $g = 0.937; $b = 0.835; break;
0347             case 'peru':
0348                 $r = 0.804; $g = 0.522; $b = 0.247; break;
0349             case 'pink':
0350                 $r = 1.0;   $g = 0.753; $b = 0.796; break;
0351             case 'plum':
0352                 $r = 0.867; $g = 0.627; $b = 0.867; break;
0353             case 'powderblue':
0354                 $r = 0.690; $g = 0.878; $b = 0.902; break;
0355             case 'rosybrown':
0356                 $r = 0.737; $g = 0.561; $b = 0.561; break;
0357             case 'royalblue':
0358                 $r = 0.255; $g = 0.412; $b = 0.882; break;
0359             case 'saddlebrown':
0360                 $r = 0.545; $g = 0.271; $b = 0.075; break;
0361             case 'salmon':
0362                 $r = 0.980; $g = 0.502; $b = 0.447; break;
0363             case 'sandybrown':
0364                 $r = 0.957; $g = 0.643; $b = 0.376; break;
0365             case 'seagreen':
0366                 $r = 0.180; $g = 0.545; $b = 0.341; break;
0367             case 'seashell':
0368                 $r = 1.0;   $g = 0.961; $b = 0.933; break;
0369             case 'sienna':
0370                 $r = 0.627; $g = 0.322; $b = 0.176; break;
0371             case 'skyblue':
0372                 $r = 0.529; $g = 0.808; $b = 0.922; break;
0373             case 'slateblue':
0374                 $r = 0.416; $g = 0.353; $b = 0.804; break;
0375             case 'slategray':
0376                 $r = 0.439; $g = 0.502; $b = 0.565; break;
0377             case 'snow':
0378                 $r = 1.0;   $g = 0.980; $b = 0.980; break;
0379             case 'springgreen':
0380                 $r = 0.0;   $g = 1.0;   $b = 0.498; break;
0381             case 'steelblue':
0382                 $r = 0.275; $g = 0.510; $b = 0.706; break;
0383             case 'tan':
0384                 $r = 0.824; $g = 0.706; $b = 0.549; break;
0385             case 'thistle':
0386                 $r = 0.847; $g = 0.749; $b = 0.847; break;
0387             case 'tomato':
0388                 $r = 0.992; $g = 0.388; $b = 0.278; break;
0389             case 'turquoise':
0390                 $r = 0.251; $g = 0.878; $b = 0.816; break;
0391             case 'violet':
0392                 $r = 0.933; $g = 0.510; $b = 0.933; break;
0393             case 'wheat':
0394                 $r = 0.961; $g = 0.871; $b = 0.702; break;
0395             case 'whitesmoke':
0396                 $r = 0.961; $g = 0.961; $b = 0.961; break;
0397             case 'yellowgreen':
0398                 $r = 0.604; $g = 0.804; $b = 0.196; break;
0399 
0400             default:
0401                 // require_once 'Zend/Pdf/Exception.php';
0402                 throw new Zend_Pdf_Exception('Unknown color name: ' . $color);
0403         }
0404         if (($r == $g) && ($g == $b)) {
0405             // require_once 'Zend/Pdf/Color/GrayScale.php';
0406             return new Zend_Pdf_Color_GrayScale($r);
0407         } else {
0408             // require_once 'Zend/Pdf/Color/Rgb.php';
0409             return new Zend_Pdf_Color_Rgb($r, $g, $b);
0410         }
0411     }
0412 }