/[drupal]/contributions/modules/drawing/modules/drawing_gd/drawing_gd_color.inc
ViewVC logotype

Diff of /contributions/modules/drawing/modules/drawing_gd/drawing_gd_color.inc

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph | View Patch Patch

revision 1.1, Sat May 9 05:56:28 2009 UTC revision 1.1.2.1, Sat May 9 05:56:28 2009 UTC
# Line 0  Line 1 
1    <?php
2    // $Id$
3    
4    /**
5     * @file
6     * The color functions for the GD module.
7     */
8    
9    /**
10     * Returns RGB values from the color functions.
11     *
12     *  @param $color
13     *    Can be english HTML/X11 color, hex color, array(r, g, b), or
14     *    "rgb(r, g, b)" string.
15     *  @return
16     *    Array with keys 'r', 'g', & 'b', and corresponding 0-255 values.
17     */
18    function drawing_gd_color($color, $default = array('r' => 0, 'g' => 0, 'b' => 0)) {
19      static $functions, $known_colors;
20      // if it's already in the correct format, just return it
21      if (is_array($color) && $color['r'] && $color['g'] && $color['b']) {
22        return $color;
23      }
24      // we need to deal with strings, so serialize if it's not a string or number
25      if (!is_string($color) && !is_numeric($color)) {
26        $color = serialize($color);
27      }
28      $color = trim((string)$color);
29      // If we've already checked this color return it now
30      if (isset($known_colors[$color])) {
31        return $known_colors[$color];
32      }
33      // Get the functions to check the color against
34      if (empty($functions)) {
35        $functions = array(
36          'drawing_gd_color_rgb',
37          'drawing_gd_color_eng',
38          'drawing_gd_color_hex',
39        );
40        // Allow modules to add their own color parsing
41        drupal_alter('drawing_gd_color_functions', $functions);
42      }
43      // Search the functions for the first one that returns a good value
44      foreach ($functions as $func) {
45        $result = $func($color);
46        if ($result) {
47          break;
48        }
49      }
50      // use default color
51      if (!$result) {
52        $result = $default;
53      }
54      // store result statically for reuse in this request
55      $known_colors[$color] = $result;
56      // return the value
57      return $result;
58    }
59    
60    /**
61     * Returns RGB values for hex colors
62     *
63     *  @param $color
64     *    String in the format rgb(r, g, b) representing a color.
65     *  @return
66     *    Array with keys 'r', 'g', & 'b', and corresponding 0-255 values, or
67     *    FALSE on failing to identify the color.
68     */
69    function drawing_gd_color_rgb($color) {
70      if (substr($color, 0, 3) == 'rgb') {
71        $color = str_replace('(', '', str_replace(')', '', $color));
72        $color_array = explode(',', $color);
73        if (count($color_array) == 3) {
74          return array(
75            'r' => (int)trim($color_array[0]),
76            'g' => (int)trim($color_array[1]),
77            'b' => (int)trim($color_array[2]),
78          );
79        }
80      }
81      return FALSE;
82    }
83    
84    /**
85     * Returns RGB values for HTML and X11 english colors.
86     * Note that the colors Gray, Green, Maroon, and Purple use the HTML color and
87     * the X11 variants are also available as GrayX11, GreenX11, MaroonX11, and
88     * PurpleX11.
89     *
90     *  @param $color
91     *    English HTML/X11 color.
92     *  @return
93     *    Array with keys 'r', 'g', & 'b', and corresponding 0-255 values, FALSE on
94     *    failing to identify the color, or the entire array of handled colors if
95     *    the $color param is not supplied.
96     */
97    function drawing_gd_color_eng($color = NULL) {
98      static $colors;
99      if (empty($colors)) {
100        $colors = array(
101          'aliceblue' => array(240, 248, 255),
102          'antiquewhite' => array(250, 235, 215),
103          'aqua' => array(0, 255, 255),
104          'aquamarine' => array(127, 255, 212),
105          'azure' => array(240, 255, 255),
106          'beige' => array(245, 245, 220),
107          'bisque' => array(255, 228, 196),
108          'black' => array(0, 0, 0),
109          'blanchedalmond' => array(255, 235, 205),
110          'blue' => array(0, 0, 255),
111          'blueviolet' => array(138, 43, 226),
112          'brown' => array(165, 42, 42),
113          'burlywood' => array(222, 184, 135),
114          'cadetblue' => array(95, 158, 160),
115          'chartreuse' => array(127, 255, 0),
116          'chocolate' => array(210, 105, 30),
117          'coral' => array(255, 127, 80),
118          'cornflower' => array(100, 149, 237),
119          'cornsilk' => array(255, 248, 220),
120          'crimson' => array(220, 20, 60),
121          'cyan' => array(0, 255, 255),
122          'darkblue' => array(0, 0, 139),
123          'darkcyan' => array(0, 139, 139),
124          'darkgoldenrod' => array(184, 134, 11),
125          'darkgray' => array(169, 169, 169),
126          'darkgreen' => array(0, 100, 0),
127          'darkkhaki' => array(189, 183, 107),
128          'darkmagenta' => array(139, 0, 139),
129          'darkolivegreen' => array(85, 107, 47),
130          'darkorange' => array(255, 140, 0),
131          'darkorchid' => array(153, 50, 204),
132          'darkred' => array(139, 0, 0),
133          'darksalmon' => array(233, 150, 122),
134          'darkseagreen' => array(143, 188, 143),
135          'darkslateblue' => array(72, 61, 139),
136          'darkslategray' => array(47, 79, 79),
137          'darkturquoise' => array(0, 206, 209),
138          'darkviolet' => array(148, 0, 211),
139          'deeppink' => array(255, 20, 147),
140          'deepskyblue' => array(0, 191, 255),
141          'dimgray' => array(105, 105, 105),
142          'dodgerblue' => array(30, 144, 255),
143          'firebrick' => array(178, 34, 34),
144          'floralwhite' => array(255, 250, 240),
145          'forestgreen' => array(34, 139, 34),
146          'fuchsia' => array(255, 0, 255),
147          'gainsboro' => array(220, 220, 220),
148          'ghostwhite' => array(248, 248, 255),
149          'gold' => array(255, 215, 0),
150          'goldenrod' => array(218, 165, 32),
151          'gray' => array(128, 128, 128),
152          'grayx11' => array(190, 190, 190),
153          'green' => array(0, 128, 0),
154          'greenx11' => array(0, 255, 0),
155          'greenyellow' => array(173, 255, 47),
156          'honeydew' => array(240, 255, 240),
157          'hotpink' => array(255, 105, 180),
158          'indianred' => array(205, 92, 92),
159          'indigo' => array(75, 0, 130),
160          'ivory' => array(255, 255, 240),
161          'khaki' => array(240, 230, 140),
162          'lavender' => array(230, 230, 250),
163          'lavenderblush' => array(255, 240, 245),
164          'lawngreen' => array(124, 252, 0),
165          'lemonchiffon' => array(255, 250, 205),
166          'lightblue' => array(173, 216, 230),
167          'lightcoral' => array(240, 128, 128),
168          'lightcyan' => array(224, 255, 255),
169          'lightgoldenrod' => array(250, 250, 210),
170          'lightgreen' => array(144, 238, 144),
171          'lightgrey' => array(211, 211, 211),
172          'lightpink' => array(255, 182, 193),
173          'lightsalmon' => array(255, 160, 122),
174          'lightseagreen' => array(32, 178, 170),
175          'lightskyblue' => array(135, 206, 250),
176          'lightslategray' => array(119, 136, 153),
177          'lightsteelblue' => array(176, 196, 222),
178          'lightyellow' => array(255, 255, 224),
179          'lime' => array(0, 255, 0),
180          'limegreen' => array(50, 205, 50),
181          'linen' => array(250, 240, 230),
182          'magenta' => array(255, 0, 255),
183          'maroon' => array(128, 0, 0),
184          'maroonx11' => array(176, 48, 96),
185          'mediumaquamarine' => array(102, 205, 170),
186          'mediumblue' => array(0, 0, 205),
187          'mediumorchid' => array(186, 85, 211),
188          'mediumpurple' => array(147, 112, 219),
189          'mediumseagreen' => array(60, 179, 113),
190          'mediumslateblue' => array(123, 104, 238),
191          'mediumspringgreen' => array(0, 250, 154),
192          'mediumturquoise' => array(72, 209, 204),
193          'mediumvioletred' => array(199, 21, 133),
194          'midnightblue' => array(25, 25, 112),
195          'mintcream' => array(245, 255, 250),
196          'mistyrose' => array(255, 228, 225),
197          'moccasin' => array(255, 228, 181),
198          'navajowhite' => array(255, 222, 173),
199          'navy' => array(0, 0, 128),
200          'oldlace' => array(253, 245, 230),
201          'olive' => array(128, 128, 0),
202          'olivedrab' => array(107, 142, 35),
203          'orange' => array(255, 165, 0),
204          'orangered' => array(255, 69, 0),
205          'orchid' => array(218, 112, 214),
206          'palegoldenrod' => array(238, 232, 170),
207          'palegreen' => array(152, 251, 152),
208          'paleturquoise' => array(175, 238, 238),
209          'palevioletred' => array(219, 112, 147),
210          'papayawhip' => array(255, 239, 213),
211          'peachpuff' => array(255, 218, 185),
212          'peru' => array(205, 133, 63),
213          'pink' => array(255, 192, 203),
214          'plum' => array(221, 160, 221),
215          'powderblue' => array(176, 224, 230),
216          'purple' => array(128, 0, 128),
217          'purplex11' => array(160, 32, 240),
218          'red' => array(255, 0, 0),
219          'rosybrown' => array(188, 143, 143),
220          'royalblue' => array(65, 105, 225),
221          'saddlebrown' => array(139, 69, 19),
222          'salmon' => array(250, 128, 114),
223          'sandybrown' => array(244, 164, 96),
224          'seagreen' => array(46, 139, 87),
225          'seashell' => array(255, 245, 238),
226          'sienna' => array(160, 82, 45),
227          'silver' => array(192, 192, 192),
228          'skyblue' => array(135, 206, 235),
229          'slateblue' => array(106, 90, 205),
230          'slategray' => array(112, 128, 144),
231          'snow' => array(255, 250, 250),
232          'springgreen' => array(0, 255, 127),
233          'steelblue' => array(70, 130, 180),
234          'tan' => array(210, 180, 140),
235          'teal' => array(0, 128, 128),
236          'thistle' => array(216, 191, 216),
237          'tomato' => array(255, 99, 71),
238          'turquoise' => array(64, 224, 208),
239          'violet' => array(238, 130, 238),
240          'wheat' => array(245, 222, 179),
241          'white' => array(255, 255, 255),
242          'whitesmoke' => array(245, 245, 245),
243          'yellow' => array(255, 255, 0),
244          'yellowgreen' => array(154, 205, 50),
245        );
246        drupal_alter('drawing_gd_color_eng', $colors);
247      }
248      if (!is_null($color)) {
249        $color = str_replace(' ', '', strtolower($color));
250        if (array_key_exists($color, $colors)) {
251          return array(
252            'r' => $colors[$color][0],
253            'g' => $colors[$color][1],
254            'b' => $colors[$color][2],
255          );
256        }
257        return FALSE;
258      }
259      return $colors;
260    }
261    
262    /**
263     * Returns RGB values for hex colors.
264     *
265     *  @param $color
266     *    Hex color.
267     *  @return
268     *    Array with keys 'r', 'g', & 'b', and corresponding 0-255 values,
269     *    or FALSE on failing to identify the color.
270     */
271    function drawing_gd_color_hex($color) {
272      if ($color[0] == '#') {
273          $color = substr($color, 1);
274      }
275      if (strlen($color) == 6) {
276        list($r, $g, $b) = array(
277          $color[0] . $color[1],
278          $color[2] . $color[3],
279          $color[4] . $color[5],
280        );
281      }
282      elseif (strlen($color) == 3) {
283        list($r, $g, $b) = array(
284          $color[0] . $color[0],
285          $color[1] . $color[1],
286          $color[2] . $color[2],
287        );
288      }
289      else {
290        return FALSE;
291      }
292      return array(
293        'r' => hexdec($r),
294        'g' => hexdec($g),
295        'b' => hexdec($b),
296      );
297    }

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.1.2.1

  ViewVC Help
Powered by ViewVC 1.1.2