File Coverage

blib/lib/WebColors.pm
Criterion Covered Total %
statement 101 104 97.1
branch 22 28 78.5
condition 28 44 63.6
subroutine 19 19 100.0
pod 10 12 83.3
total 180 207 86.9


line stmt bran cond sub pod time code
1             # ABSTRACT:
2              
3             =head1 NAME
4              
5             WebColors
6              
7             =head1 SYNOPSIS
8              
9             use 5.10.0 ;
10             use strict ;
11             use warnings ;
12             use WebColors;
13              
14             my ($r, $g, $b) = colorname_to_rgb( 'goldenrod') ;
15              
16             =head1 DESCRIPTION
17              
18             Get either the hex triplet value or the rgb values for a HTML named color.
19              
20             Values have been taken from https://en.wikipedia.org/wiki/HTML_color_names#HTML_color_names
21              
22             For me I want this module so that I can use the named colours to
23             extend Device::Hynpocube so that it can use the full set of named colors it
24             is also used in Device::BlinkStick
25              
26             Google material colors have spaces removed and their numerical values added, so
27              
28             Red 400 becomes red400, with accents Deep Purple A100 becomes deeppurplea100
29              
30             See Also
31              
32             Google material colors L
33              
34             L
35              
36             =cut
37              
38             # note _)rgb, darken and lighten have been lifted from Color::Mix and fitted into this module
39              
40             package WebColors ;
41             $WebColors::VERSION = '0.4.7';
42 1     1   53677 use 5.0.4 ;
  1         3  
43 1     1   5 use warnings ;
  1         2  
  1         22  
44 1     1   4 use strict ;
  1         2  
  1         23  
45 1     1   4 use Exporter ;
  1         1  
  1         30  
46 1     1   5 use vars qw( @EXPORT @ISA) ;
  1         1  
  1         2492  
47              
48             @ISA = qw(Exporter) ;
49              
50             # this is the list of things that will get imported into the loading packages
51             # namespace
52             @EXPORT = qw(
53             list_webcolors
54             to_rgb
55             colorname_to_hex
56             colorname_to_rgb
57             colorname_to_rgb_percent
58             rgb_to_colorname
59             hex_to_colorname
60             rgb_percent_to_colorname
61             inverse_rgb
62             luminance
63             lighten
64             darken
65             ) ;
66              
67             # ----------------------------------------------------------------------------
68              
69             my %web_colors = (
70              
71             # basic
72             black => [ 0, 0, 0 ],
73             silver => [ 192, 192, 192 ],
74             grey => [ 128, 128, 128 ],
75             gray => [ 128, 128, 128 ],
76             white => [ 255, 255, 255 ],
77             maroon => [ 128, 0, 0 ],
78             red => [ 255, 0, 0 ],
79             purple => [ 128, 0, 128 ],
80             fuchsia => [ 255, 0, 255 ],
81             green => [ 0, 128, 0 ], # not sure why green is not 255, but lime is
82             lime => [ 0, 255, 0 ],
83             olive => [ 128, 128, 0 ],
84             yellow => [ 255, 255, 0 ],
85             navy => [ 0, 0, 128 ],
86             blue => [ 0, 0, 255 ],
87             teal => [ 0, 128, 128 ],
88             aqua => [ 0, 255, 255 ],
89              
90             # extended
91             aliceblue => [ 240, 248, 255 ],
92             antiquewhite => [ 250, 235, 215 ],
93             # aqua => [ 0, 255, 255 ],
94             aquamarine => [ 127, 255, 212 ],
95             azure => [ 240, 255, 255 ],
96             beige => [ 245, 245, 220 ],
97             bisque => [ 255, 228, 196 ],
98             # black => [ 0, 0, 0 ],
99             blanchedalmond => [ 255, 235, 205 ],
100             # blue => [ 0, 0, 255 ],
101             blueviolet => [ 138, 43, 226 ],
102             brown => [ 165, 42, 42 ],
103             burlywood => [ 222, 184, 135 ],
104             cadetblue => [ 95, 158, 160 ],
105             chartreuse => [ 127, 255, 0 ],
106             chocolate => [ 210, 105, 30 ],
107             coral => [ 255, 127, 80 ],
108             cornflowerblue => [ 100, 149, 237 ],
109             cornsilk => [ 255, 248, 220 ],
110             crimson => [ 220, 20, 60 ],
111             cyan => [ 0, 255, 255 ],
112             darkblue => [ 0, 0, 139 ],
113             darkcyan => [ 0, 139, 139 ],
114             darkgoldenrod => [ 184, 134, 11 ],
115             darkgray => [ 169, 169, 169 ],
116             darkgrey => [ 169, 169, 169 ],
117             darkgreen => [ 0, 100, 0 ],
118             darkgrey => [ 169, 169, 169 ],
119             darkkhaki => [ 189, 183, 107 ],
120             darkmagenta => [ 139, 0, 139 ],
121             darkolivegreen => [ 85, 107, 47 ],
122             darkorange => [ 255, 140, 0 ],
123             darkorchid => [ 153, 50, 204 ],
124             darkred => [ 139, 0, 0 ],
125             darksalmon => [ 233, 150, 122 ],
126             darkseagreen => [ 143, 188, 143 ],
127             darkslateblue => [ 72, 61, 139 ],
128             darkslategray => [ 47, 79, 79 ],
129             darkslategrey => [ 47, 79, 79 ],
130             darkturquoise => [ 0, 206, 209 ],
131             darkviolet => [ 148, 0, 211 ],
132             deeppink => [ 255, 20, 147 ],
133             deepskyblue => [ 0, 191, 255 ],
134             dimgray => [ 105, 105, 105 ],
135             dimgrey => [ 105, 105, 105 ],
136             dodgerblue => [ 30, 144, 255 ],
137             firebrick => [ 178, 34, 34 ],
138             floralwhite => [ 255, 250, 240 ],
139             forestgreen => [ 34, 139, 34 ],
140             # fuchsia => [ 255, 0, 255 ],
141             gainsboro => [ 220, 220, 220 ],
142             ghostwhite => [ 248, 248, 255 ],
143             gold => [ 255, 215, 0 ],
144             goldenrod => [ 218, 165, 32 ],
145             # gray => [ 128, 128, 128 ],
146             # grey => [ 128, 128, 128 ],
147             # green => [ 0, 128, 0 ],
148             greenyellow => [ 173, 255, 47 ],
149             # grey => [ 128, 128, 128 ],
150             honeydew => [ 240, 255, 240 ],
151             hotpink => [ 255, 105, 180 ],
152             indianred => [ 205, 92, 92 ],
153             indigo => [ 75, 0, 130 ],
154             ivory => [ 255, 255, 240 ],
155             khaki => [ 240, 230, 140 ],
156             lavender => [ 230, 230, 250 ],
157             lavenderblush => [ 255, 240, 245 ],
158             lawngreen => [ 124, 252, 0 ],
159             lemonchiffon => [ 255, 250, 205 ],
160             lightblue => [ 173, 216, 230 ],
161             lightcoral => [ 240, 128, 128 ],
162             lightcyan => [ 224, 255, 255 ],
163             lightgoldenrodyellow => [ 250, 250, 210 ],
164             lightgray => [ 211, 211, 211 ],
165             lightgrey => [ 211, 211, 211 ],
166             lightgreen => [ 144, 238, 144 ],
167             lightpink => [ 255, 182, 193 ],
168             lightsalmon => [ 255, 160, 122 ],
169             lightseagreen => [ 32, 178, 170 ],
170             lightskyblue => [ 135, 206, 250 ],
171             lightslategray => [ 119, 136, 153 ],
172             lightslategrey => [ 119, 136, 153 ],
173             lightsteelblue => [ 176, 196, 222 ],
174             lightyellow => [ 255, 255, 224 ],
175             # lime => [ 0, 255, 0 ],
176             limegreen => [ 50, 205, 50 ],
177             linen => [ 250, 240, 230 ],
178             magenta => [ 255, 0, 255 ],
179             # maroon => [ 128, 0, 0 ],
180             mediumaquamarine => [ 102, 205, 170 ],
181             mediumblue => [ 0, 0, 205 ],
182             mediumorchid => [ 186, 85, 211 ],
183             mediumpurple => [ 147, 112, 219 ],
184             mediumseagreen => [ 60, 179, 113 ],
185             mediumslateblue => [ 123, 104, 238 ],
186             mediumspringgreen => [ 0, 250, 154 ],
187             mediumturquoise => [ 72, 209, 204 ],
188             mediumvioletred => [ 199, 21, 133 ],
189             midnightblue => [ 25, 25, 112 ],
190             mintcream => [ 245, 255, 250 ],
191             mistyrose => [ 255, 228, 225 ],
192             moccasin => [ 255, 228, 181 ],
193             navajowhite => [ 255, 222, 173 ],
194             # navy => [ 0, 0, 128 ],
195             oldlace => [ 253, 245, 230 ],
196             # olive => [ 128, 128, 0 ],
197             olivedrab => [ 107, 142, 35 ],
198             orange => [ 255, 165, 0 ],
199             orangered => [ 255, 69, 0 ],
200             orchid => [ 218, 112, 214 ],
201             palegoldenrod => [ 238, 232, 170 ],
202             palegreen => [ 152, 251, 152 ],
203             paleturquoise => [ 175, 238, 238 ],
204             palevioletred => [ 219, 112, 147 ],
205             papayawhip => [ 255, 239, 213 ],
206             peachpuff => [ 255, 218, 185 ],
207             peru => [ 205, 133, 63 ],
208             pink => [ 255, 192, 203 ],
209             plum => [ 221, 160, 221 ],
210             powderblue => [ 176, 224, 230 ],
211             # purple => [ 128, 0, 128 ],
212             # red => [ 255, 0, 0 ],
213             rebeccapurple => [ 102, 51, 153 ],
214             rosybrown => [ 188, 143, 143 ],
215             royalblue => [ 65, 105, 225 ],
216             saddlebrown => [ 139, 69, 19 ],
217             salmon => [ 250, 128, 114 ],
218             sandybrown => [ 244, 164, 96 ],
219             seagreen => [ 46, 139, 87 ],
220             seashell => [ 255, 245, 238 ],
221             sienna => [ 160, 82, 45 ],
222             # silver => [ 192, 192, 192 ],
223             skyblue => [ 135, 206, 235 ],
224             slateblue => [ 106, 90, 205 ],
225             slategray => [ 112, 128, 144 ],
226             slategrey => [ 112, 128, 144 ],
227             snow => [ 255, 250, 250 ],
228             springgreen => [ 0, 255, 127 ],
229             steelblue => [ 70, 130, 180 ],
230             tan => [ 210, 180, 140 ],
231             # teal => [ 0, 128, 128 ],
232             thistle => [ 216, 191, 216 ],
233             tomato => [ 255, 99, 71 ],
234             turquoise => [ 64, 224, 208 ],
235             violet => [ 238, 130, 238 ],
236             wheat => [ 245, 222, 179 ],
237             # white => [ 255, 255, 255 ],
238             whitesmoke => [ 245, 245, 245 ],
239             # yellow => [ 255, 255, 0 ],
240             yellowgreen => [ 154, 205, 50 ],
241             # I notice that this is a thing! https://stackoverflow.com/questions/8318911/why-does-html-think-chucknorris-is-a-color
242             chucknorris => [ 192, 0, 0 ],
243              
244             # in memory of prince, pantone released https://www.pantone.com/images/pages/21326/prince-announces-pantone-custom-color-purple.jpg
245             prince => [ 85, 58, 99 ],
246             lovesymbol2 => [ 85, 58, 99 ],
247             'love-symbol-2' => [ 85, 58, 99 ],
248             # more pantone colors
249             ultraviolet => [ 101, 78, 163 ],
250             'ultra-violet' => [ 101, 78, 163 ],
251             greenery => [ 132, 189, 0 ],
252             masarla => [ 173, 101, 95 ],
253             tangerinetango => [ 221, 65, 36 ],
254             'tangerine-tango' => [ 221, 65, 36 ],
255             chillipepper => [ 168, 41, 24 ],
256             'chilli-pepper' => [ 168, 41, 24 ],
257              
258             # google material colors from http://www.google.com/design/spec/style/color.html
259              
260             red50 => [ 0xff, 0xeb, 0xee ],
261             red100 => [ 0xff, 0xcd, 0xd2 ],
262             red200 => [ 0xef, 0x9a, 0x9a ],
263             red300 => [ 0xe5, 0x73, 0x73 ],
264             red400 => [ 0xef, 0x53, 0x50 ],
265             red500 => [ 0xf4, 0x43, 0x36 ],
266             red600 => [ 0xe5, 0x39, 0x35 ],
267             red700 => [ 0xd3, 0x2f, 0x2f ],
268             red800 => [ 0xc6, 0x28, 0x28 ],
269             red900 => [ 0xb7, 0x1c, 0x1c ],
270             reda100 => [ 0xff, 0x8a, 0x80 ],
271             reda200 => [ 0xff, 0x52, 0x52 ],
272             reda400 => [ 0xff, 0x17, 0x44 ],
273             reda700 => [ 0xd5, 0x00, 0x00 ],
274              
275             pink50 => [ 0xfc, 0xe4, 0xec ],
276             pink100 => [ 0xf8, 0xbb, 0xd0 ],
277             pink200 => [ 0xf4, 0x8f, 0xb1 ],
278             pink300 => [ 0xf0, 0x62, 0x92 ],
279             pink400 => [ 0xec, 0x40, 0x7a ],
280             pink500 => [ 0xe9, 0x1e, 0x63 ],
281             pink600 => [ 0xd8, 0x1b, 0x60 ],
282             pink700 => [ 0xc2, 0x18, 0x5b ],
283             pink800 => [ 0xad, 0x14, 0x57 ],
284             pink900 => [ 0x88, 0x0e, 0x4f ],
285             pinka100 => [ 0xff, 0x80, 0xab ],
286             pinka200 => [ 0xff, 0x40, 0x81 ],
287             pinka400 => [ 0xf5, 0x00, 0x57 ],
288             pinka700 => [ 0xc5, 0x11, 0x62 ],
289              
290             purple50 => [ 0xf3, 0xe5, 0xf5 ],
291             purple100 => [ 0xe1, 0xbe, 0xe7 ],
292             purple200 => [ 0xce, 0x93, 0xd8 ],
293             purple300 => [ 0xba, 0x68, 0xc8 ],
294             purple400 => [ 0xab, 0x47, 0xbc ],
295             purple500 => [ 0x9c, 0x27, 0xb0 ],
296             purple600 => [ 0x8e, 0x24, 0xaa ],
297             purple700 => [ 0x7b, 0x1f, 0xa2 ],
298             purple800 => [ 0x6a, 0x1b, 0x9a ],
299             purple900 => [ 0x4a, 0x14, 0x8c ],
300             purplea100 => [ 0xea, 0x80, 0xfc ],
301             purplea200 => [ 0xe0, 0x40, 0xfb ],
302             purplea400 => [ 0xd5, 0x00, 0xf9 ],
303             purplea700 => [ 0xaa, 0x00, 0xff ],
304              
305             deeppurple50 => [ 0xed, 0xe7, 0xf6 ],
306             deeppurple100 => [ 0xd1, 0xc4, 0xe9 ],
307             deeppurple200 => [ 0xb3, 0x9d, 0xdb ],
308             deeppurple300 => [ 0x95, 0x75, 0xcd ],
309             deeppurple400 => [ 0x7e, 0x57, 0xc2 ],
310             deeppurple500 => [ 0x67, 0x3a, 0xb7 ],
311             deeppurple600 => [ 0x5e, 0x35, 0xb1 ],
312             deeppurple700 => [ 0x51, 0x2d, 0xa8 ],
313             deeppurple800 => [ 0x45, 0x27, 0xa0 ],
314             deeppurple900 => [ 0x31, 0x1b, 0x92 ],
315             deeppurplea100 => [ 0xb3, 0x88, 0xff ],
316             deeppurplea200 => [ 0x7c, 0x4d, 0xff ],
317             deeppurplea400 => [ 0x65, 0x1f, 0xff ],
318             deeppurplea700 => [ 0x62, 0x00, 0xea ],
319              
320             indigo50 => [ 0xe8, 0xea, 0xf6 ],
321             indigo100 => [ 0xc5, 0xca, 0xe9 ],
322             indigo200 => [ 0x9f, 0xa8, 0xda ],
323             indigo300 => [ 0x79, 0x86, 0xcb ],
324             indigo400 => [ 0x5c, 0x6b, 0xc0 ],
325             indigo500 => [ 0x3f, 0x51, 0xb5 ],
326             indigo600 => [ 0x39, 0x49, 0xab ],
327             indigo700 => [ 0x30, 0x3f, 0x9f ],
328             indigo800 => [ 0x28, 0x35, 0x93 ],
329             indigo900 => [ 0x1a, 0x23, 0x7e ],
330             indigoa100 => [ 0x8c, 0x9e, 0xff ],
331             indigoa200 => [ 0x53, 0x6d, 0xfe ],
332             indigoa400 => [ 0x3d, 0x5a, 0xfe ],
333             indigoa700 => [ 0x30, 0x4f, 0xfe ],
334              
335             blue50 => [ 0xe3, 0xf2, 0xfd ],
336             blue100 => [ 0xbb, 0xde, 0xfb ],
337             blue200 => [ 0x90, 0xca, 0xf9 ],
338             blue300 => [ 0x64, 0xb5, 0xf6 ],
339             blue400 => [ 0x42, 0xa5, 0xf5 ],
340             blue500 => [ 0x21, 0x96, 0xf3 ],
341             blue600 => [ 0x1e, 0x88, 0xe5 ],
342             blue700 => [ 0x19, 0x76, 0xd2 ],
343             blue800 => [ 0x15, 0x65, 0xc0 ],
344             blue900 => [ 0x0d, 0x47, 0xa1 ],
345             bluea100 => [ 0x82, 0xb1, 0xff ],
346             bluea200 => [ 0x44, 0x8a, 0xff ],
347             bluea400 => [ 0x29, 0x79, 0xff ],
348             bluea700 => [ 0x29, 0x62, 0xff ],
349              
350             lightblue50 => [ 0xe1, 0xf5, 0xfe ],
351             lightblue100 => [ 0xb3, 0xe5, 0xfc ],
352             lightblue200 => [ 0x81, 0xd4, 0xfa ],
353             lightblue300 => [ 0x4f, 0xc3, 0xf7 ],
354             lightblue400 => [ 0x29, 0xb6, 0xf6 ],
355             lightblue500 => [ 0x03, 0xa9, 0xf4 ],
356             lightblue600 => [ 0x03, 0x9b, 0xe5 ],
357             lightblue700 => [ 0x02, 0x88, 0xd1 ],
358             lightblue800 => [ 0x02, 0x77, 0xbd ],
359             lightblue900 => [ 0x01, 0x57, 0x9b ],
360             lightbluea100 => [ 0x80, 0xd8, 0xff ],
361             lightbluea200 => [ 0x40, 0xc4, 0xff ],
362             lightbluea400 => [ 0x00, 0xb0, 0xff ],
363             lightbluea700 => [ 0x00, 0x91, 0xea ],
364              
365             cyan50 => [ 0xe0, 0xf7, 0xfa ],
366             cyan100 => [ 0xb2, 0xeb, 0xf2 ],
367             cyan200 => [ 0x80, 0xde, 0xea ],
368             cyan300 => [ 0x4d, 0xd0, 0xe1 ],
369             cyan400 => [ 0x26, 0xc6, 0xda ],
370             cyan500 => [ 0x00, 0xbc, 0xd4 ],
371             cyan600 => [ 0x00, 0xac, 0xc1 ],
372             cyan700 => [ 0x00, 0x97, 0xa7 ],
373             cyan800 => [ 0x00, 0x83, 0x8f ],
374             cyan900 => [ 0x00, 0x60, 0x64 ],
375             cyana100 => [ 0x84, 0xff, 0xff ],
376             cyana200 => [ 0x18, 0xff, 0xff ],
377             cyana400 => [ 0x00, 0xe5, 0xff ],
378             cyana700 => [ 0x00, 0xb8, 0xd4 ],
379              
380             teal50 => [ 0xe0, 0xf2, 0xf1 ],
381             teal100 => [ 0xb2, 0xdf, 0xdb ],
382             teal200 => [ 0x80, 0xcb, 0xc4 ],
383             teal300 => [ 0x4d, 0xb6, 0xac ],
384             teal400 => [ 0x26, 0xa6, 0x9a ],
385             teal500 => [ 0x00, 0x96, 0x88 ],
386             teal600 => [ 0x00, 0x89, 0x7b ],
387             teal700 => [ 0x00, 0x79, 0x6b ],
388             teal800 => [ 0x00, 0x69, 0x5c ],
389             teal900 => [ 0x00, 0x4d, 0x40 ],
390             teala100 => [ 0xa7, 0xff, 0xeb ],
391             teala200 => [ 0x64, 0xff, 0xda ],
392             teala400 => [ 0x1d, 0xe9, 0xb6 ],
393             teala700 => [ 0x00, 0xbf, 0xa5 ],
394              
395             green50 => [ 0xe8, 0xf5, 0xe9 ],
396             green100 => [ 0xc8, 0xe6, 0xc9 ],
397             green200 => [ 0xa5, 0xd6, 0xa7 ],
398             green300 => [ 0x81, 0xc7, 0x84 ],
399             green400 => [ 0x66, 0xbb, 0x6a ],
400             green500 => [ 0x4c, 0xaf, 0x50 ],
401             green600 => [ 0x43, 0xa0, 0x47 ],
402             green700 => [ 0x38, 0x8e, 0x3c ],
403             green800 => [ 0x2e, 0x7d, 0x32 ],
404             green900 => [ 0x1b, 0x5e, 0x20 ],
405             greena100 => [ 0xb9, 0xf6, 0xca ],
406             greena200 => [ 0x69, 0xf0, 0xae ],
407             greena400 => [ 0x00, 0xe6, 0x76 ],
408             greena700 => [ 0x00, 0xc8, 0x53 ],
409              
410             lightgreen50 => [ 0xf1, 0xf8, 0xe9 ],
411             lightgreen100 => [ 0xdc, 0xed, 0xc8 ],
412             lightgreen200 => [ 0xc5, 0xe1, 0xa5 ],
413             lightgreen300 => [ 0xae, 0xd5, 0x81 ],
414             lightgreen400 => [ 0x9c, 0xcc, 0x65 ],
415             lightgreen500 => [ 0x8b, 0xc3, 0x4a ],
416             lightgreen600 => [ 0x7c, 0xb3, 0x42 ],
417             lightgreen700 => [ 0x68, 0x9f, 0x38 ],
418             lightgreen800 => [ 0x55, 0x8b, 0x2f ],
419             lightgreen900 => [ 0x33, 0x69, 0x1e ],
420             lightgreena100 => [ 0xcc, 0xff, 0x90 ],
421             lightgreena200 => [ 0xb2, 0xff, 0x59 ],
422             lightgreena400 => [ 0x76, 0xff, 0x03 ],
423             lightgreena700 => [ 0x64, 0xdd, 0x17 ],
424              
425             lime50 => [ 0xf9, 0xfb, 0xe7 ],
426             lime100 => [ 0xf0, 0xf4, 0xc3 ],
427             lime200 => [ 0xe6, 0xee, 0x9c ],
428             lime300 => [ 0xdc, 0xe7, 0x75 ],
429             lime400 => [ 0xd4, 0xe1, 0x57 ],
430             lime500 => [ 0xcd, 0xdc, 0x39 ],
431             lime600 => [ 0xc0, 0xca, 0x33 ],
432             lime700 => [ 0xaf, 0xb4, 0x2b ],
433             lime800 => [ 0x9e, 0x9d, 0x24 ],
434             lime900 => [ 0x82, 0x77, 0x17 ],
435             limea100 => [ 0xf4, 0xff, 0x81 ],
436             limea200 => [ 0xee, 0xff, 0x41 ],
437             limea400 => [ 0xc6, 0xff, 0x00 ],
438             limea700 => [ 0xae, 0xea, 0x00 ],
439              
440             yellow50 => [ 0xff, 0xfd, 0xe7 ],
441             yellow100 => [ 0xff, 0xf9, 0xc4 ],
442             yellow200 => [ 0xff, 0xf5, 0x9d ],
443             yellow300 => [ 0xff, 0xf1, 0x76 ],
444             yellow400 => [ 0xff, 0xee, 0x58 ],
445             yellow500 => [ 0xff, 0xeb, 0x3b ],
446             yellow600 => [ 0xfd, 0xd8, 0x35 ],
447             yellow700 => [ 0xfb, 0xc0, 0x2d ],
448             yellow800 => [ 0xf9, 0xa8, 0x25 ],
449             yellow900 => [ 0xf5, 0x7f, 0x17 ],
450             yellowa100 => [ 0xff, 0xff, 0x8d ],
451             yellowa200 => [ 0xff, 0xff, 0x00 ],
452             yellowa400 => [ 0xff, 0xea, 0x00 ],
453             yellowa700 => [ 0xff, 0xd6, 0x00 ],
454              
455             amber50 => [ 0xff, 0xf8, 0xe1 ],
456             amber100 => [ 0xff, 0xec, 0xb3 ],
457             amber200 => [ 0xff, 0xe0, 0x82 ],
458             amber300 => [ 0xff, 0xd5, 0x4f ],
459             amber400 => [ 0xff, 0xca, 0x28 ],
460             amber500 => [ 0xff, 0xc1, 0x07 ],
461             amber600 => [ 0xff, 0xb3, 0x00 ],
462             amber700 => [ 0xff, 0xa0, 0x00 ],
463             amber800 => [ 0xff, 0x8f, 0x00 ],
464             amber900 => [ 0xff, 0x6f, 0x00 ],
465             ambera100 => [ 0xff, 0xe5, 0x7f ],
466             ambera200 => [ 0xff, 0xd7, 0x40 ],
467             ambera400 => [ 0xff, 0xc4, 0x00 ],
468             ambera700 => [ 0xff, 0xab, 0x00 ],
469              
470             orange50 => [ 0xff, 0xf3, 0xe0 ],
471             orange100 => [ 0xff, 0xe0, 0xb2 ],
472             orange200 => [ 0xff, 0xcc, 0x80 ],
473             orange300 => [ 0xff, 0xb7, 0x4d ],
474             orange400 => [ 0xff, 0xa7, 0x26 ],
475             orange500 => [ 0xff, 0x98, 0x00 ],
476             orange600 => [ 0xfb, 0x8c, 0x00 ],
477             orange700 => [ 0xf5, 0x7c, 0x00 ],
478             orange800 => [ 0xef, 0x6c, 0x00 ],
479             orange900 => [ 0xe6, 0x51, 0x00 ],
480             orangea100 => [ 0xff, 0xd1, 0x80 ],
481             orangea200 => [ 0xff, 0xab, 0x40 ],
482             orangea400 => [ 0xff, 0x91, 0x00 ],
483             orangea700 => [ 0xff, 0x6d, 0x00 ],
484              
485             deeporange50 => [ 0xfb, 0xe9, 0xe7 ],
486             deeporange100 => [ 0xff, 0xcc, 0xbc ],
487             deeporange200 => [ 0xff, 0xab, 0x91 ],
488             deeporange300 => [ 0xff, 0x8a, 0x65 ],
489             deeporange400 => [ 0xff, 0x70, 0x43 ],
490             deeporange500 => [ 0xff, 0x57, 0x22 ],
491             deeporange600 => [ 0xf4, 0x51, 0x1e ],
492             deeporange700 => [ 0xe6, 0x4a, 0x19 ],
493             deeporange800 => [ 0xd8, 0x43, 0x15 ],
494             deeporange900 => [ 0xbf, 0x36, 0x0c ],
495             deeporangea100 => [ 0xff, 0x9e, 0x80 ],
496             deeporangea200 => [ 0xff, 0x6e, 0x40 ],
497             deeporangea400 => [ 0xff, 0x3d, 0x00 ],
498             deeporangea700 => [ 0xdd, 0x2c, 0x00 ],
499              
500             brown50 => [ 0xef, 0xeb, 0xe9 ],
501             brown100 => [ 0xd7, 0xcc, 0xc8 ],
502             brown200 => [ 0xbc, 0xaa, 0xa4 ],
503             brown300 => [ 0xa1, 0x88, 0x7f ],
504             brown400 => [ 0x8d, 0x6e, 0x63 ],
505             brown500 => [ 0x79, 0x55, 0x48 ],
506             brown600 => [ 0x6d, 0x4c, 0x41 ],
507             brown700 => [ 0x5d, 0x40, 0x37 ],
508             brown800 => [ 0x4e, 0x34, 0x2e ],
509             brown900 => [ 0x3e, 0x27, 0x23 ],
510              
511             grey50 => [ 0xfa, 0xfa, 0xfa ],
512             grey100 => [ 0xf5, 0xf5, 0xf5 ],
513             grey200 => [ 0xee, 0xee, 0xee ],
514             grey300 => [ 0xe0, 0xe0, 0xe0 ],
515             grey400 => [ 0xbd, 0xbd, 0xbd ],
516             grey500 => [ 0x9e, 0x9e, 0x9e ],
517             grey600 => [ 0x75, 0x75, 0x75 ],
518             grey700 => [ 0x61, 0x61, 0x61 ],
519             grey800 => [ 0x42, 0x42, 0x42 ],
520             grey900 => [ 0x21, 0x21, 0x21 ],
521              
522             gray50 => [ 0xfa, 0xfa, 0xfa ],
523             gray100 => [ 0xf5, 0xf5, 0xf5 ],
524             gray200 => [ 0xee, 0xee, 0xee ],
525             gray300 => [ 0xe0, 0xe0, 0xe0 ],
526             gray400 => [ 0xbd, 0xbd, 0xbd ],
527             gray500 => [ 0x9e, 0x9e, 0x9e ],
528             gray600 => [ 0x75, 0x75, 0x75 ],
529             gray700 => [ 0x61, 0x61, 0x61 ],
530             gray800 => [ 0x42, 0x42, 0x42 ],
531             gray900 => [ 0x21, 0x21, 0x21 ],
532              
533             bluegrey50 => [ 0xec, 0xef, 0xf1 ],
534             bluegrey100 => [ 0xcf, 0xd8, 0xdc ],
535             bluegrey200 => [ 0xb0, 0xbe, 0xc5 ],
536             bluegrey300 => [ 0x90, 0xa4, 0xae ],
537             bluegrey400 => [ 0x78, 0x90, 0x9c ],
538             bluegrey500 => [ 0x60, 0x7d, 0x8b ],
539             bluegrey600 => [ 0x54, 0x6e, 0x7a ],
540             bluegrey700 => [ 0x45, 0x5a, 0x64 ],
541             bluegrey800 => [ 0x37, 0x47, 0x4f ],
542             bluegrey900 => [ 0x26, 0x32, 0x38 ],
543              
544             bluegray50 => [ 0xec, 0xef, 0xf1 ],
545             bluegray100 => [ 0xcf, 0xd8, 0xdc ],
546             bluegray200 => [ 0xb0, 0xbe, 0xc5 ],
547             bluegray300 => [ 0x90, 0xa4, 0xae ],
548             bluegray400 => [ 0x78, 0x90, 0x9c ],
549             bluegray500 => [ 0x60, 0x7d, 0x8b ],
550             bluegray600 => [ 0x54, 0x6e, 0x7a ],
551             bluegray700 => [ 0x45, 0x5a, 0x64 ],
552             bluegray800 => [ 0x37, 0x47, 0x4f ],
553             bluegray900 => [ 0x26, 0x32, 0x38 ],
554              
555             # open colors https://yeun.github.io/open-color/
556             # variations in names handled in colorname_to_rgb
557             'oc-gray-0' => [ 0xf8, 0xf9, 0xfa ],
558             'oc-gray-1' => [ 0xf1, 0xf3, 0xf5 ],
559             'oc-gray-2' => [ 0xe9, 0xec, 0xef ],
560             'oc-gray-3' => [ 0xde, 0xe2, 0xe6 ],
561             'oc-gray-4' => [ 0xce, 0xd4, 0xda ],
562             'oc-gray-5' => [ 0xad, 0xb5, 0xbd ],
563             'oc-gray-6' => [ 0x86, 0x8e, 0x96 ],
564             'oc-gray-7' => [ 0x49, 0x50, 0x57 ],
565             'oc-gray-8' => [ 0x34, 0x3a, 0x40 ],
566             'oc-gray-9' => [ 0x21, 0x25, 0x29 ],
567             'oc-grey-1' => [ 0xf1, 0xf3, 0xf5 ],
568             'oc-grey-2' => [ 0xe9, 0xec, 0xef ],
569             'oc-grey-3' => [ 0xde, 0xe2, 0xe6 ],
570             'oc-grey-4' => [ 0xce, 0xd4, 0xda ],
571             'oc-grey-5' => [ 0xad, 0xb5, 0xbd ],
572             'oc-grey-6' => [ 0x86, 0x8e, 0x96 ],
573             'oc-grey-7' => [ 0x49, 0x50, 0x57 ],
574             'oc-grey-8' => [ 0x34, 0x3a, 0x40 ],
575             'oc-grey-9' => [ 0x21, 0x25, 0x29 ],
576             'oc-red-0' => [ 0xff, 0xf5, 0xf5 ],
577             'oc-red-1' => [ 0xff, 0xe3, 0xe3 ],
578             'oc-red-2' => [ 0xff, 0xc9, 0xc9 ],
579             'oc-red-3' => [ 0xff, 0xa8, 0xa8 ],
580             'oc-red-4' => [ 0xff, 0x87, 0x87 ],
581             'oc-red-5' => [ 0xff, 0x6b, 0x6b ],
582             'oc-red-6' => [ 0xfa, 0x52, 0x52 ],
583             'oc-red-7' => [ 0xf0, 0x3e, 0x3e ],
584             'oc-red-8' => [ 0xe0, 0x31, 0x31 ],
585             'oc-red-9' => [ 0xc9, 0x2a, 0x2a ],
586             'oc-pink-0' => [ 0xff, 0xf0, 0xf6 ],
587             'oc-pink-1' => [ 0xff, 0xde, 0xeb ],
588             'oc-pink-2' => [ 0xfc, 0xc2, 0xd7 ],
589             'oc-pink-3' => [ 0xfa, 0xa2, 0xc1 ],
590             'oc-pink-4' => [ 0xf7, 0x83, 0xac ],
591             'oc-pink-5' => [ 0xf0, 0x65, 0x95 ],
592             'oc-pink-6' => [ 0xe6, 0x49, 0x80 ],
593             'oc-pink-7' => [ 0xd6, 0x33, 0x6c ],
594             'oc-pink-8' => [ 0xc2, 0x25, 0x5c ],
595             'oc-pink-9' => [ 0xa6, 0x1e, 0x4d ],
596             'oc-grape-0' => [ 0xf8, 0xf0, 0xfc ],
597             'oc-grape-1' => [ 0xf3, 0xd9, 0xfa ],
598             'oc-grape-2' => [ 0xee, 0xbe, 0xfa ],
599             'oc-grape-3' => [ 0xe5, 0x99, 0xf7 ],
600             'oc-grape-4' => [ 0xda, 0x77, 0xf2 ],
601             'oc-grape-5' => [ 0xcc, 0x5d, 0xe8 ],
602             'oc-grape-6' => [ 0xbe, 0x4b, 0xdb ],
603             'oc-grape-7' => [ 0xae, 0x3e, 0xc9 ],
604             'oc-grape-8' => [ 0x9c, 0x36, 0xb5 ],
605             'oc-grape-9' => [ 0x86, 0x2e, 0x9c ],
606             'oc-violet-0' => [ 0xf3, 0xf0, 0xff ],
607             'oc-violet-1' => [ 0xe5, 0xdb, 0xff ],
608             'oc-violet-2' => [ 0xd0, 0xbf, 0xff ],
609             'oc-violet-3' => [ 0xb1, 0x97, 0xfc ],
610             'oc-violet-4' => [ 0x97, 0x75, 0xfa ],
611             'oc-violet-5' => [ 0x84, 0x5e, 0xf7 ],
612             'oc-violet-6' => [ 0x79, 0x50, 0xf2 ],
613             'oc-violet-7' => [ 0x70, 0x48, 0xe8 ],
614             'oc-violet-8' => [ 0x67, 0x41, 0xd9 ],
615             'oc-violet-9' => [ 0x5f, 0x3d, 0xc4 ],
616             'oc-indigo-0' => [ 0xed, 0xf2, 0xff ],
617             'oc-indigo-1' => [ 0xdb, 0xe4, 0xff ],
618             'oc-indigo-2' => [ 0xba, 0xc8, 0xff ],
619             'oc-indigo-3' => [ 0x91, 0xa7, 0xff ],
620             'oc-indigo-4' => [ 0x74, 0x8f, 0xfc ],
621             'oc-indigo-5' => [ 0x5c, 0x7c, 0xfa ],
622             'oc-indigo-6' => [ 0x4c, 0x6e, 0xf5 ],
623             'oc-indigo-7' => [ 0x42, 0x63, 0xeb ],
624             'oc-indigo-8' => [ 0x3b, 0x5b, 0xdb ],
625             'oc-indigo-9' => [ 0x36, 0x4f, 0xc7 ],
626             'oc-blue-0' => [ 0xe8, 0xf7, 0xff ],
627             'oc-blue-1' => [ 0xcc, 0xed, 0xff ],
628             'oc-blue-2' => [ 0xa3, 0xda, 0xff ],
629             'oc-blue-3' => [ 0x72, 0xc3, 0xfc ],
630             'oc-blue-4' => [ 0x4d, 0xad, 0xf7 ],
631             'oc-blue-5' => [ 0x32, 0x9a, 0xf0 ],
632             'oc-blue-6' => [ 0x22, 0x8a, 0xe6 ],
633             'oc-blue-7' => [ 0x1c, 0x7c, 0xd6 ],
634             'oc-blue-8' => [ 0x1b, 0x6e, 0xc2 ],
635             'oc-blue-9' => [ 0x18, 0x62, 0xab ],
636             'oc-cyan-0' => [ 0xe3, 0xfa, 0xfc ],
637             'oc-cyan-1' => [ 0xc5, 0xf6, 0xfa ],
638             'oc-cyan-2' => [ 0x99, 0xe9, 0xf2 ],
639             'oc-cyan-3' => [ 0x66, 0xd9, 0xe8 ],
640             'oc-cyan-4' => [ 0x3b, 0xc9, 0xdb ],
641             'oc-cyan-5' => [ 0x22, 0xb8, 0xcf ],
642             'oc-cyan-6' => [ 0x15, 0xaa, 0xbf ],
643             'oc-cyan-7' => [ 0x10, 0x98, 0xad ],
644             'oc-cyan-8' => [ 0x0c, 0x85, 0x99 ],
645             'oc-cyan-9' => [ 0x0b, 0x72, 0x85 ],
646             'oc-teal-0' => [ 0xe6, 0xfc, 0xf5 ],
647             'oc-teal-1' => [ 0xc3, 0xfa, 0xe8 ],
648             'oc-teal-2' => [ 0x96, 0xf2, 0xd7 ],
649             'oc-teal-3' => [ 0x63, 0xe6, 0xbe ],
650             'oc-teal-4' => [ 0x38, 0xd9, 0xa9 ],
651             'oc-teal-5' => [ 0x20, 0xc9, 0x97 ],
652             'oc-teal-6' => [ 0x12, 0xb8, 0x86 ],
653             'oc-teal-7' => [ 0x0c, 0xa6, 0x78 ],
654             'oc-teal-8' => [ 0x09, 0x92, 0x68 ],
655             'oc-teal-9' => [ 0x08, 0x7f, 0x5b ],
656             'oc-green-0' => [ 0xeb, 0xfb, 0xee ],
657             'oc-green-1' => [ 0xd3, 0xf9, 0xd8 ],
658             'oc-green-2' => [ 0xb2, 0xf2, 0xbb ],
659             'oc-green-3' => [ 0x8c, 0xe9, 0x9a ],
660             'oc-green-4' => [ 0x69, 0xdb, 0x7c ],
661             'oc-green-5' => [ 0x51, 0xcf, 0x66 ],
662             'oc-green-6' => [ 0x40, 0xc0, 0x57 ],
663             'oc-green-7' => [ 0x37, 0xb2, 0x4d ],
664             'oc-green-8' => [ 0x2f, 0x9e, 0x44 ],
665             'oc-green-9' => [ 0x2b, 0x8a, 0x3e ],
666             'oc-lime-0' => [ 0xf4, 0xfc, 0xe3 ],
667             'oc-lime-1' => [ 0xe9, 0xfa, 0xc8 ],
668             'oc-lime-2' => [ 0xd8, 0xf5, 0xa2 ],
669             'oc-lime-3' => [ 0xc0, 0xeb, 0x75 ],
670             'oc-lime-4' => [ 0xa9, 0xe3, 0x4b ],
671             'oc-lime-5' => [ 0x94, 0xd8, 0x2d ],
672             'oc-lime-6' => [ 0x82, 0xc9, 0x1e ],
673             'oc-lime-7' => [ 0x74, 0xb8, 0x16 ],
674             'oc-lime-8' => [ 0x66, 0xa8, 0x0f ],
675             'oc-lime-9' => [ 0x5c, 0x94, 0x0d ],
676             'oc-yellow-0' => [ 0xff, 0xf9, 0xdb ],
677             'oc-yellow-1' => [ 0xff, 0xf3, 0xbf ],
678             'oc-yellow-2' => [ 0xff, 0xec, 0x99 ],
679             'oc-yellow-3' => [ 0xff, 0xe0, 0x66 ],
680             'oc-yellow-4' => [ 0xff, 0xd4, 0x3b ],
681             'oc-yellow-5' => [ 0xfc, 0xc4, 0x19 ],
682             'oc-yellow-6' => [ 0xfa, 0xb0, 0x05 ],
683             'oc-yellow-7' => [ 0xf5, 0x9f, 0x00 ],
684             'oc-yellow-8' => [ 0xf0, 0x8c, 0x00 ],
685             'oc-yellow-9' => [ 0xe6, 0x77, 0x00 ],
686             'oc-orange-0' => [ 0xff, 0xf4, 0xe6 ],
687             'oc-orange-1' => [ 0xff, 0xe8, 0xcc ],
688             'oc-orange-2' => [ 0xff, 0xd8, 0xa8 ],
689             'oc-orange-3' => [ 0xff, 0xc0, 0x78 ],
690             'oc-orange-4' => [ 0xff, 0xa9, 0x4d ],
691             'oc-orange-5' => [ 0xff, 0x92, 0x2b ],
692             'oc-orange-6' => [ 0xfd, 0x7e, 0x14 ],
693             'oc-orange-7' => [ 0xf7, 0x67, 0x07 ],
694             'oc-orange-8' => [ 0xe8, 0x59, 0x0c ],
695             'oc-orange-9' => [ 0xd9, 0x48, 0x0f ],
696              
697             ) ;
698              
699             =head1 Public Functions
700              
701             =over 4
702              
703             =cut
704              
705             # ----------------------------------------------------------------------------
706              
707             =item list_webcolors
708              
709             list the colors covered in this module
710              
711             my @colors = list_colors() ;
712              
713             =cut
714              
715             sub list_webcolors
716             {
717 1     1 1 396 my @c = sort keys %web_colors ;
718 1         69 return @c ;
719             }
720              
721              
722             # ----------------------------------------------------------------------------
723              
724             # get rgb values from a hex triplet
725              
726             sub _hex_to_rgb
727             {
728 10     10   12 my ($hex) = @_ ;
729              
730 10   50     19 $hex ||= '000000' ;
731 10         23 $hex =~ s/^#// ;
732 10         13 $hex = lc($hex) ;
733              
734 10         13 my ( $r, $g, $b ) ;
735 10 100       44 if ( $hex =~ /^[0-9a-f]{6}$/ ) {
    100          
736 3         18 ( $r, $g, $b ) = ( $hex =~ /(\w{2})/g ) ;
737             } elsif ( $hex =~ /^[0-9a-f]{3}$/ ) {
738 1         5 ( $r, $g, $b ) = ( $hex =~ /(\w)/g ) ;
739             # double up to make the colors correct
740 1         5 ( $r, $g, $b ) = ( "$r$r", "$g$g", "$b$b" ) ;
741             } else {
742 6         2020 return ( undef, undef, undef ) ;
743             }
744              
745 4         12 return ( hex($r), hex($g), hex($b) ) ;
746             }
747              
748             # ----------------------------------------------------------------------------
749              
750             =item to_rbg
751              
752             get rgb for a hex triplet, or a colorname. if the hex value is only 3 characters
753             then it wil be expanded to 6
754              
755             my ($r,$g,$b) = to_rgb( 'ff00ab') ;
756             ($r,$g,$b) = to_rgb( 'red') ;
757             ($r,$g,$b) = to_rgb( 'abc') ;
758              
759             entries will be null if there is no match
760              
761             =cut
762              
763             sub to_rgb
764             {
765 9     9 0 2273 my ($name) = @_ ;
766 9   50     18 $name ||= 'black' ;
767             # first up try as hex
768 9         14 my ( $r, $g, $b ) = _hex_to_rgb($name) ;
769              
770             # try as a name then
771 9 100       28 if ( !defined $r ) {
772 6         14 ( $r, $g, $b ) = colorname_to_rgb($name) ;
773             }
774              
775 9         26 return ( $r, $g, $b ) ;
776             }
777              
778             # ----------------------------------------------------------------------------
779              
780             =item colorname_to_rgb
781              
782             get the rgb values 0..255 to match a color
783              
784             my ($r, $g, $b) = colorname_to_rgb( 'goldenrod') ;
785              
786             # get a material color
787             ($r, $g, $b) = colorname_to_rgb( 'bluegrey500') ;
788              
789             # open colors
790             ($r, $g, $b) = colorname_to_rgb( 'oc-lime-5') ;
791              
792             entries will be null if there is no match
793              
794             =cut
795              
796             sub colorname_to_rgb
797             {
798 20     20 1 1348 my ($name) = @_ ;
799              
800 20         34 $name =~ s/#//g ;
801 20   50     36 $name ||= "black" ;
802 20         29 $name = lc($name) ;
803              
804             # allow variations on open color names
805 20         34 $name =~ s/^oc(\w+)(\d)$/oc-$1-$2/ ;
806 20         25 $name =~ s/^oc(\w+)-(\d)$/oc-$1-$2/ ;
807 20         33 $name =~ s/^oc-(\w+)(\d)$/oc-$1-$2/ ;
808              
809             # deref the arraryref
810 20         37 my $rgb = $web_colors{$name} ;
811 20 100 100     59 if ( !$rgb && $name =~ /^[0-9a-f]{6}$/i ) {
812 2         9 $rgb = [
813             hex( substr( $name, 0, 2 ) ),
814             hex( substr( $name, 2, 2 ) ),
815             hex( substr( $name, 4, 2 ) )
816             ] ;
817             }
818              
819 20 100       32 $rgb = [ undef, undef, undef ] if ( !$rgb ) ;
820 20         44 return @$rgb ;
821             }
822              
823             # ----------------------------------------------------------------------------
824              
825             =item colorname_to_hex
826              
827             get the color value as a hex triplet '12ffee' to match a color
828              
829             my $hex => colorname_to_hex( 'darkslategray') ;
830              
831             # get a material color, accented red
832             $hex => colorname_to_hex( 'reda300') ;
833              
834             entries will be null if there is no match
835              
836             =cut
837              
838             sub colorname_to_hex
839             {
840 7     7 1 1836 my ($name) = @_ ;
841             # $name ||= 'black' ;
842 7         17 $name =~ s/grey/gray/ ;
843 7         12 my @c = colorname_to_rgb($name) ;
844 7         9 my $str ;
845 7 100       31 $str = sprintf( "%02x%02x%02x", $c[0], $c[1], $c[2] )
846             if ( defined $c[0] ) ;
847 7         17 return $str ;
848             }
849              
850             # ----------------------------------------------------------------------------
851              
852             =item colorname_to_rgb_percent
853              
854             get the rgb values as an integer percentage 0..100% to match a color
855              
856             my ($r, $g, $b) = colorname_to_percent( 'goldenrod') ;
857              
858             entries will be null if there is no match
859              
860             =cut
861              
862             sub colorname_to_rgb_percent
863             {
864 2     2 1 630 my ($name) = @_ ;
865 2   50     6 $name ||= 'black' ;
866              
867 2         5 my @c = colorname_to_rgb($name) ;
868              
869 2 50       4 if ( defined $c[0] ) {
870 2         7 for ( my $i = 0; $i < scalar(@c); $i++ ) {
871 6         16 $c[$i] = int( $c[$i] * 100 / 255 ) ;
872             }
873             }
874 2         5 return @c ;
875             }
876              
877             # ----------------------------------------------------------------------------
878             # test if a value is almost +/- 1 another value
879             sub _almost
880             {
881 1242     1242   1370 my ( $a, $b ) = @_ ;
882              
883 1242 100 100     3918 ( $a == $b || ( $a + 1 ) == $b || ( $a - 1 == $b ) ) ? 1 : 0 ;
884             }
885              
886             # ----------------------------------------------------------------------------
887              
888             =item rgb_to_colorname
889              
890             match a name from a rgb triplet, matches within +/-1 of the values
891              
892             my $name = rgb_to_colorname( 255, 0, 0) ;
893              
894             returns null if there is no match
895              
896             =cut
897              
898             sub rgb_to_colorname
899             {
900 4     4 1 345 my ( $r, $g, $b ) = @_ ;
901 4 50       14 if ( $r eq 'black' ) {
902 0         0 ( $r, $g, $b ) = ( 0, 0, 0 ) ;
903             }
904              
905 4   50     7 $r //= 0 ;
906 4   50     8 $g //= 0 ;
907 4   50     7 $b //= 0 ;
908              
909 4         5 my $color ;
910 4         924 foreach my $c ( sort keys %web_colors ) {
911              
912             # no need for fancy compares
913 1116         2119 my ( $r1, $g1, $b1 ) = @{ $web_colors{$c} } ;
  1116         1452  
914              
915 1116 100 100     1296 if ( _almost( $r, $r1 ) && _almost( $g, $g1 ) && _almost( $b, $b1 ) ) {
      100        
916 4         16 $color = $c ;
917 4         8 last ;
918             }
919             }
920              
921 4         81 return $color ;
922             }
923              
924             # ----------------------------------------------------------------------------
925              
926             =item rgb_percent_to_colorname
927              
928             match a name from a rgb_percet triplet, matches within +/-1 of the value
929              
930             my $name = rgb_percent_to_colorname( 100, 0, 100) ;
931              
932             returns null if there is no match
933              
934             =cut
935              
936             sub rgb_percent_to_colorname
937             {
938 2     2 1 751 my ( $r, $g, $b ) = @_ ;
939 2 50       14 if ( $r eq 'black' ) {
940 0         0 ( $r, $g, $b ) = ( 0, 0, 0 ) ;
941             }
942              
943 2   50     6 $r //= 0 ;
944 2   50     6 $g //= 0 ;
945 2   50     3 $b //= 0 ;
946              
947 2         13 return rgb_to_colorname( int( $r * 255 / 100 ), int( $g * 255 / 100 ), int( $b * 255 / 100 ) ) ;
948             }
949              
950             # ----------------------------------------------------------------------------
951              
952             =item hex_to_colorname
953              
954             match a name from a hex triplet, matches within +/-1 of the value
955              
956             my $name = hex_to_colorname( 'ff0000') ;
957              
958             returns null if there is no match
959              
960             =cut
961              
962             sub hex_to_colorname
963             {
964 1     1 1 327 my ($hex) = @_ ;
965 1   50     10 $hex ||= '000000' ;
966              
967 1         4 my ( $r, $g, $b ) = _hex_to_rgb($hex) ;
968              
969 1         6 return rgb_to_colorname( $r, $g, $b ) ;
970             }
971              
972             # ----------------------------------------------------------------------------
973              
974             =item inverse_rgb
975              
976             Get the inverse of the RGB values
977              
978             my ($i_r, $i_g, $i_b) = inverse_rgb( 0xff, 0x45, 0x34) ;
979              
980             =cut
981              
982             sub inverse_rgb
983             {
984 2     2 1 4 my ( $r, $g, $b ) = @_ ;
985 2 50       6 if ( $r eq 'black' ) {
986 0         0 ( $r, $g, $b ) = ( 0, 0, 0 ) ;
987             }
988              
989 2   50     4 $r //= 0 ;
990 2   50     4 $g //= 0 ;
991 2   50     3 $b //= 0 ;
992              
993 2         23 return ( 255 - $r, 255 - $g, 255 - $b ) ;
994             }
995              
996             # ----------------------------------------------------------------------------
997             # source was http://stackoverflow.com/questions/596216/formula-to-determine-brightness-of-rgb-color
998              
999             =item luminance
1000              
1001             Calculate the luminance of an rgb value
1002              
1003             Rough calculation using Photometric/digital ITU-R:
1004              
1005             Y = 0.2126 R + 0.7152 G + 0.0722 B
1006              
1007             my $luma = luminance( to_rgb( 'green')) ;
1008              
1009             =cut
1010              
1011             sub luminance
1012             {
1013 3     3 1 5 my ( $r, $g, $b ) = @_ ;
1014              
1015 3         10 return int( ( 0.2126 * $r ) + ( 0.7152 * $g ) + ( 0.0722 * $b ) ) ;
1016             }
1017              
1018             # # ----------------------------------------------------------------------------
1019              
1020             # =item approx_luminance
1021              
1022             # Calculate the approximate luminance of an rgb value, rough/ready/fast
1023              
1024             # my $luma = approx_luminance( to_rgb( 'green')) ;
1025              
1026             # =cut
1027              
1028             # sub approx_luminance
1029             # {
1030             # my ( $r, $g, $b ) = @_ ;
1031              
1032             # return int( ( ( 2 * $r ) + ( 3 * $g ) + ($b) ) / 6 ) ;
1033             # }
1034              
1035              
1036             =back
1037              
1038             =cut
1039              
1040             # -----------------------------------------------------------------------------
1041              
1042             =item lighten
1043              
1044             lighten a color by an optional amount, returns a hex color string
1045             amount to adjust is a multiple of 32
1046              
1047             my $hex = lighten( 'goldenrod', 3) ;
1048              
1049             =cut
1050              
1051             sub lighten
1052             {
1053 1     1 1 210 my ( $color, $by ) = @_ ;
1054 1   50     14 $by ||= 1 ;
1055 1         2 my $shade = 32 ; # $self->get_shade;
1056             return sprintf( '%.2x%.2x%.2x',
1057 1 50       3 map { ( $_ + $shade * $by > 255 ) ? 255 : $_ + $shade * $by } colorname_to_rgb($color) ) ;
  3         12  
1058             }
1059              
1060             # -----------------------------------------------------------------------------
1061             =item darken
1062              
1063             darken a color by an optional amount, returns a hex color string
1064             amount to adjust is a multiple of 32
1065              
1066              
1067             my $hex = darken( 'goldenrod', 2) ;
1068              
1069             =cut
1070              
1071             sub darken
1072             {
1073 1     1 0 209 my ( $color, $by ) = @_ ;
1074 1   50     7 $by ||= 1 ;
1075 1         1 my $shade = 32 ; # $self->get_shade;
1076             return sprintf( '%.2x%.2x%.2x',
1077 1 50       3 map { ( $_ - $shade * $by < 0 ) ? 0 : $_ - $shade * $by } colorname_to_rgb($color) ) ;
  3         13  
1078             }
1079              
1080              
1081             # ----------------------------------------------------------------------------
1082              
1083             1 ;