File Coverage

blib/lib/WebColors.pm
Criterion Covered Total %
statement 65 65 100.0
branch 15 16 93.7
condition 9 9 100.0
subroutine 17 17 100.0
pod 9 10 90.0
total 115 117 98.2


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