1<?php
2// $Header: /cvsroot/html2ps/css.colors.inc.php,v 1.10 2007/01/24 18:55:51 Konstantin Exp $
3
4$GLOBALS['g_colors'] = array(
5  // Standard HTML colors
6  "black"   => array(0,0,0),
7  "silver"  => array(192,192,192),
8  "gray"    => array(128,128,128),
9  "white"   => array(255,255,255),
10  "maroon"  => array(128,0,0),
11  "red"     => array(255,0,0),
12  "purple"  => array(128,0,128),
13  "fuchsia" => array(255,0,255),
14  "green"   => array(0,128,0),
15  "lime"    => array(0,255,0),
16  "olive"   => array(128,128,0),
17  "yellow"  => array(255,255,0),
18  "navy"    => array(0,0,128),
19  "blue"    => array(0,0,255),
20  "teal"    => array(0,128,128),
21  "aqua"    => array(0,255,255),
22
23  // Widely-used non-stadard color names
24  "aliceblue"            => array(240,248,255),
25  "antiquewhite"         => array(250,235,215),
26  "aquamarine"           => array(127,255,212),
27  "azure"                => array(240,255,255),
28  "beige"                => array(245,245,220),
29  "bisque"               => array(255,228,196),
30  "blanchedalmond"       => array(255,235,205),
31  "blueviolet"           => array(138, 43,226),
32  "brown"                => array(165, 42, 42),
33  "burlywood"            => array(222,184,135),
34  "cadetblue"            => array( 95,158,160),
35  "chartreuse"           => array(127,255,  0),
36  "chocolate"            => array(210,105, 30),
37  "coral"                => array(255,127, 80),
38  "cornflowerblue"       => array(100,149,237),
39  "cornsilk"             => array(255,248,220),
40  "crimson"              => array(220, 20, 60),
41  "cyan"                 => array(  0,255,255),
42  "darkblue"             => array(  0,  0,139),
43  "darkcyan"             => array(  0,139,139),
44  "darkgoldenrod"        => array(184,134, 11),
45  "darkgray"             => array(169,169,169),
46  "darkgreen"            => array(  0,100,  0),
47  "darkkhaki"            => array(189,183,107),
48  "darkmagenta"          => array(139,  0,139),
49  "darkolivegreen"       => array( 85,107, 47),
50  "darkorange"           => array(255,140,  0),
51  "darkorchid"           => array(153, 50,204),
52  "darkred"              => array(139,  0,  0),
53  "darksalmon"           => array(233,150,122),
54  "darkseagreen"         => array(143,188,143),
55  "darkslateblue"        => array( 72, 61,139),
56  "darkslategray"        => array( 47, 79, 79),
57  "darkturquoise"        => array(  0,206,209),
58  "darkviolet"           => array(148,  0,211),
59  "deeppink"             => array(255, 20,147),
60  "deepskyblue"          => array(  0,191,255),
61  "dimgray"              => array(105,105,105),
62  "dodgerblue"           => array( 30,144,255),
63  "firebrick"            => array(178, 34, 34),
64  "floralwhite"          => array(255,250,240),
65  "forestgreen"          => array( 34,139, 34),
66  "gainsboro"            => array(220,220,220),
67  "ghostwhite"           => array(248,248,255),
68  "gold"                 => array(255,215,  0),
69  "goldenrod"            => array(218,165, 32),
70  "greenyellow"          => array(173,255, 47),
71  "honeydew"             => array(240,255,240),
72  "hotpink"              => array(255,105,180),
73  "indianred"            => array(205, 92, 92),
74  "indigo"               => array( 75,  0,130),
75  "ivory"                => array(255,255,240),
76  "khaki"                => array(240,230,140),
77  "lavender"             => array(230,230,250),
78  "lavenderblush"        => array(255,240,245),
79  "lawngreen"            => array(124,252,  0),
80  "lemonchiffon"         => array(255,250,205),
81  "lightblue"            => array(173,216,230),
82  "lightcoral"           => array(240,128,128),
83  "lightcyan"            => array(224,255,255),
84  "lightgoldenrodyellow" => array(250,250,210),
85  "lightgreen"           => array(144,238,244),
86  "lightgrey"            => array(211,211,211),
87  "lightpink"            => array(255,182,193),
88  "lightsalmon"          => array(255,160,122),
89  "lightseagreen"        => array( 32,178,170),
90  "lightskyblue"         => array(135,206,250),
91  "lightslategray"       => array(119,136,153),
92  "lightsteelblue"       => array(176,196,222),
93  "lightyellow"          => array(255,255,224),
94  "limegreen"            => array( 50,205, 50),
95  "linen"                => array(250,240,230),
96  "magenta"              => array(255,  0,255),
97  "mediumaquamarine"     => array(102,205,170),
98  "mediumblue"           => array(  0,  0,205),
99  "mediumorchid"         => array(186, 85,211),
100  "mediumpurple"         => array(147,112,219),
101  "mediumseagreen"       => array( 60,179,113),
102  "mediumslateblue"      => array(123,104,238),
103  "mediumspringgreen"    => array(  0,250,154),
104  "mediumturquoise"      => array( 72,209,204),
105  "mediumvioletred"      => array(199, 21,133),
106  "midnightblue"         => array( 25, 25,112),
107  "mintcream"            => array(245,255,250),
108  "mistyrose"            => array(255,228,225),
109  "moccasin"             => array(255,228,181),
110  "navajowhite"          => array(255,222,173),
111  "oldlace"              => array(253,245,230),
112  "olivedrab"            => array(107,142, 35),
113  "orange"               => array(255,165,  0),
114  "orangered"            => array(255, 69,  0),
115  "orchid"               => array(218,112,214),
116  "palegoldenrod"        => array(238,232,170),
117  "palegreen"            => array(152,251,152),
118  "paleturquoise"        => array(175,238,238),
119  "palevioletred"        => array(219,112,147),
120  "papayawhip"           => array(255,239,213),
121  "peachpuff"            => array(255,218,185),
122  "peru"                 => array(205,133, 63),
123  "pink"                 => array(255,192,203),
124  "plum"                 => array(221,160,221),
125  "powderblue"           => array(176,224,230),
126  "rosybrown"            => array(188,143,143),
127  "royalblue"            => array( 65,105,225),
128  "saddlebrown"          => array(139, 69, 19),
129  "salmon"               => array(250,128,114),
130  "sandybrown"           => array(244,164, 96),
131  "seagreen"             => array( 46,139, 87),
132  "seashell"             => array(255,245,238),
133  "sienna"               => array(160, 82, 45),
134  "skyblue"              => array(135,206,235),
135  "slateblue"            => array(106, 90,205),
136  "slategray"            => array(112,128,144),
137  "snow"                 => array(255,250,250),
138  "springgreen"          => array(  0,255,127),
139  "steelblue"            => array( 70,130,180),
140  "tan"                  => array(210,180,140),
141  "thistle"              => array(216,191,216),
142  "tomato"               => array(255, 99, 71),
143  "turquoise"            => array( 64,224,208),
144  "violet"               => array(238,130,238),
145  "wheat"                => array(245,222,179),
146  "whitesmoke"           => array(245,245,245),
147  "yellowgreen"          => array(154,205, 50)
148);
149
150function &parse_color_declaration($decl) {
151  $color = _parse_color_declaration($decl, $success);
152  $color_obj =& new Color($color, is_transparent($color));
153  return $color_obj;
154};
155
156
157function _parse_color_declaration($decl, &$success) {
158  $success = true;
159
160  global $g_colors;
161  if (isset($g_colors[strtolower($decl)])) { return $g_colors[strtolower($decl)]; };
162
163  // Parse color keywords
164  switch (strtolower($decl)) {
165    case "transparent":
166      return array(-1,-1,-1);
167  }
168
169  // rgb(0,0,0) form
170  if (preg_match("/rgb\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)\s*\)/",$decl,$matches)) {
171    $r = min(255,max(0,$matches[1]));
172    $g = min(255,max(0,$matches[2]));
173    $b = min(255,max(0,$matches[3]));
174    return array($r,$g,$b);
175  };
176
177  // rgb(0%,0%,0%) form
178  if (preg_match("/rgb\(\s*(\d+)%\s*,\s*(\d+)%\s*,\s*(\d+)%\s*\)/",$decl,$matches)) {
179    $r = min(255,max(0,$matches[1]*255/100));
180    $g = min(255,max(0,$matches[2]*255/100));
181    $b = min(255,max(0,$matches[3]*255/100));
182    return array($r,$g,$b);
183  };
184
185  // We've already checked every non-hexadecimal forms; now only color declarations starting
186  // with # left; nevertheless, sometimes designers forget to put #-sign before the
187  // color declaration. Thus, we'll add sharp sign automatically if it is missing
188  //
189  if (strlen($decl) > 0) {
190    if ($decl{0} !== "#") { $decl = "#" . $decl; };
191  };
192
193  // #000000 form
194  if (preg_match("/^#([[:xdigit:]]{2})([[:xdigit:]]{2})([[:xdigit:]]{2})$/",$decl,$matches)) {
195    $arrr = unpack("C",pack("H2",$matches[1]));
196    $arrg = unpack("C",pack("H2",$matches[2]));
197    $arrb = unpack("C",pack("H2",$matches[3]));
198
199    // Note that array indices returned by unpack differ in different versions of PHP. Unfortunately
200    // we unable to directly access values - compatibility is an issue...
201
202    $r = array_pop($arrr);
203    $g = array_pop($arrg);
204    $b = array_pop($arrb);
205
206    return array($r,$g,$b);
207  };
208
209  // #000 form
210  if (preg_match("/^#([[:xdigit:]])([[:xdigit:]])([[:xdigit:]])$/",$decl,$matches)) {
211    $arrr = unpack("C",pack("H2",$matches[1].$matches[1]));
212    $arrg = unpack("C",pack("H2",$matches[2].$matches[2]));
213    $arrb = unpack("C",pack("H2",$matches[3].$matches[3]));
214
215    // Note that array indices returned by unpack differ in different versions of PHP. Unfortunately
216    // we unable to directly access values - compatibility is an issue...
217
218    $r = array_pop($arrr);
219    $g = array_pop($arrg);
220    $b = array_pop($arrb);
221
222    return array($r,$g,$b);
223  };
224
225  // Transparent color - by default
226  $success = false;
227  return array(-1,-1,-1);
228}
229
230function is_transparent($color) {
231  return $color[0] < 0;
232}
233
234?>