File Coverage

blib/lib/Graphics/ColorUtils.pm
Criterion Covered Total %
statement 166 169 98.2
branch 75 78 96.1
condition 3 3 100.0
subroutine 26 26 100.0
pod 9 17 52.9
total 279 293 95.2


line stmt bran cond sub pod time code
1              
2             package Graphics::ColorUtils;
3              
4 4     4   241545 use 5.008003;
  4         17  
  4         233  
5 4     4   23 use strict;
  4         10  
  4         439  
6 4     4   22 use warnings;
  4         23  
  4         243  
7              
8 4     4   20 use Carp;
  4         7  
  4         30525  
9              
10             require Exporter;
11              
12             our @ISA = qw(Exporter);
13              
14             our %EXPORT_TAGS = ( 'gradients' => [ qw( gradient
15             grad2rgb
16             available_gradients
17             register_gradient) ],
18             'names' => [ qw( name2rgb
19             available_names
20             register_name
21             set_default_namespace
22             get_default_namespace ) ],
23             'all' => [ qw( rgb2yiq yiq2rgb
24             rgb2cmy cmy2rgb
25             rgb2hls hls2rgb
26             rgb2hsv hsv2rgb
27             gradient
28             grad2rgb
29             available_gradients
30             register_gradient
31             name2rgb
32             available_names
33             register_name
34             set_default_namespace
35             get_default_namespace ) ],
36             );
37              
38             our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
39              
40             our @EXPORT = qw( rgb2yiq yiq2rgb
41             rgb2cmy cmy2rgb
42             rgb2hls hls2rgb
43             rgb2hsv hsv2rgb );
44              
45             our $VERSION = '0.17';
46              
47             # ==================================================
48             # ++++++++++++++++++++++++++++++++++++++++++++++++++
49             # ==================================================
50              
51             # ==================================================
52             # Utility
53              
54             # Takes a (r,g,b) triple of numbers (possibly floats) and returns
55             # - a string like '#33FF21' in scalar context
56             # - a triple of corresponding integers in array context
57             sub _fmt {
58 1361 100   1361   7491 return wantarray ? map { int } @_ : sprintf( "#%02x%02x%02x", @_ );
  3069         12285  
59             }
60              
61             # ==================================================
62             # YIQ
63              
64             sub rgb2yiq {
65             # $r, $g, $b : 0..255
66 189     189 0 114530 my ( $r, $g, $b ) = map { $_/255.0 } @_; # Scale RGB to 0..1
  567         1157  
67              
68 189         450 my $y = 0.299*$r + 0.587*$g + 0.114*$b;
69 189         767 my $i = 0.596*$r - 0.275*$g - 0.321*$b;
70 189         301 my $q = 0.212*$r - 0.523*$g + 0.311*$b;
71              
72 189         688 return ( $y, $i, $q );
73             }
74              
75             sub yiq2rgb {
76             # $y, $i, $q : 0..1
77 314     314 0 223059 my ( $y, $i, $q ) = @_;
78              
79 314         638 my $r = 255.0*( $y + 0.956*$i + 0.621*$q );
80 314         555 my $g = 255.0*( $y - 0.272*$i - 0.647*$q );
81 314         430 my $b = 255.0*( $y - 1.105*$i + 1.705*$q );
82              
83 314         2752 return _fmt( $r, $g, $b );
84             }
85              
86             # ==================================================
87             # CMY
88              
89             sub rgb2cmy {
90             # $r, $g, $b : 0..255
91 253     253 0 277125 my ( $r, $g, $b ) = map { $_/255.0 } @_; # Scale RGB to 0..1
  759         2043  
92              
93 253         1209 return ( 1.0 - $r, 1.0 - $g, 1.0 - $b );
94             }
95              
96             sub cmy2rgb {
97             # $c, $m, $y : 0..1
98 314     314 0 226757 my ( $c, $m, $y ) = @_;
99              
100 314         1551 return _fmt( 255*(1.0-$c), 255*(1.0-$m), 255*(1.0-$y) );
101             }
102              
103             # ==================================================
104             # HLS
105              
106             # Foley, van Dam, et al:
107             # Computer Grapics-Principles and Practice (1990) p595f
108              
109             sub rgb2hls {
110             # $r, $g, $b : 0..255
111             # Note special name '$bb' to avoid conflict with ($a,$b) in sort()
112 314     314 0 98896 my ( $r, $g, $bb ) = map { $_/255.0 } @_; # Scale RGB to 0..1
  942         2181  
113              
114 314         961 my ( $minc, $maxc ) = ( sort { $a <=> $b } ( $r, $g, $bb ) )[0,2];
  868         1555  
115              
116 314         924 my $m = $minc + $maxc; # "Mean"
117              
118 314 100       724 if( $maxc == $minc ) { return ( 0, 0.5*$m, 0 ); } # Achromatic case
  54         224  
119              
120 260         361 my $d = $maxc - $minc; # "Delta"
121 260 100       559 my $s = ( $m <= 1.0 ) ? $d/$m : $d/(2.0-$m ); # Saturation
122              
123 260         308 my $h = 0; # Hue
124 260 100       1053 if( $r == $maxc ) { $h = ( $g-$bb )/$d; }
  86 100       135  
    50          
125 100         161 elsif( $g == $maxc ) { $h = 2 + ( $bb-$r )/$d; }
126 74         129 elsif( $bb == $maxc ) { $h = 4 + ( $r-$g )/$d; }
127             else {
128             # Never get here!
129 0         0 croak "Internal Error: Unexpected value ,$maxc, in Graphics::ColorUtils::rgb2hls( $r, $g, $bb )";
130             }
131              
132 260         369 $h *= 60; # Convert to degrees
133 260 100       461 if( $h < 0 ) { $h += 360; } # Ensure positive hue
  30         38  
134              
135 260         1045 return ( $h, 0.5*$m, $s );
136             }
137              
138             sub hls2rgb {
139             # $h: 0..360 (red=0->yellow->green=120->cyan->blue=240->magenta steps of 60)
140             # $l, $s : 0..1 (inclusive)
141 328     328 0 256109 my ( $h, $l, $s ) = @_;
142              
143 328 100       1056 if( $s == 0.0 ) { return _fmt(255*$l, 255*$l, 255*$l); } # achromatic (grey)
  5         32  
144              
145             # This is the INCORRECT line as it is in the book quoted above:
146             # my $m2 = ( $l <= 0.5 ) ? ($l*($l+$s)) : ($l - $l*$s + $s);
147             # This is the CORRECT line: (first alternative: 1 vs $l)
148 323 100       906 my $m2 = ( $l <= 0.5 ) ? ($l*(1+$s)) : ($l - $l*$s + $s);
149 323         648 my $m1 = 2.0*$l - $m2;
150              
151 323         716 my $r = 255 * _value( $m1, $m2, $h + 120 );
152 323         845 my $g = 255 * _value( $m1, $m2, $h );
153 323         781 my $b = 255 * _value( $m1, $m2, $h - 120 );
154              
155 323         763 return _fmt( $r, $g, $b );
156             }
157              
158             sub _value {
159 969     969   1374 my ( $n1, $n2, $hue ) = @_;
160              
161 969 100       2491 if( $hue > 360 ) { $hue -= 360; }
  95 100       115  
162 128         158 elsif( $hue < 0 ) { $hue += 360; }
163              
164 969 100       2082 if( $hue < 60 ) { return $n1 + $hue * ( $n2-$n1 )/60.0; }
  158 100       425  
    100          
165 323         566 elsif( $hue < 180 ) { return $n2; }
166 157         589 elsif( $hue < 240 ) { return $n1 + ( 240-$hue ) * ( $n2-$n1 )/60.0; }
167 331         970 else { return $n1; }
168             }
169              
170             # ==================================================
171             # HSV
172              
173             # Foley, van Dam, et al:
174             # Computer Grapics-Principles and Practice (1990) p592f
175              
176             sub rgb2hsv {
177             # $r, $g, $b : 0..25
178             # Note special name '$bb' to avoid conflict with ($a,$b) in sort()
179 314     314 0 71776 my ( $r, $g, $bb ) = map { $_/255.0 } @_; # Scale RGB to 0..1
  942         1909  
180              
181 314         1228 my ( $minc, $maxc ) = ( sort { $a <=> $b } ( $r, $g, $bb ) )[0,2];
  868         2127  
182              
183 314         372 my $v = $maxc; # Value
184 314         434 my $d = $maxc - $minc; # "Delta"
185 314 100       825 my $s = ( $maxc == 0 ) ? 0 : $d/$maxc; # No saturation if R=G=B=0
186              
187 314 100       673 if( $s == 0 ) { return ( 0, 0, $v ); } # Achromatic case
  54         220  
188              
189 260         315 my $h = 0; # Hue
190 260 100       836 if( $r == $maxc ) { $h = ( $g-$bb )/$d; }
  86 100       133  
    50          
191 100         178 elsif( $g == $maxc ) { $h = 2 + ( $bb-$r )/$d; }
192 74         126 elsif( $bb == $maxc ) { $h = 4 + ( $r-$g )/$d; }
193             else {
194             # Never get here!
195 0         0 croak "Internal Error: Unexpected value ,$maxc, in Graphics::ColorUtils::rgb2hsv( $r, $g, $bb )";
196             }
197              
198 260         568 $h *= 60; # Convert to degrees
199 260 100       2020 if( $h < 0 ) { $h += 360; } # Ensure positive hue
  30         47  
200              
201 260         1350 return ( $h, $s, $v );
202             }
203              
204             sub hsv2rgb {
205             # $h: 0..360 (red=0->yellow->green=120->cyan->blue=240->magenta steps of 60)
206             # (tolerates larger values of $h by reducing them to the standard circle)
207             # $s, $v : 0..1 (inclusive)
208 328     328 0 426063 my ( $h, $s, $v ) = @_;
209              
210 328         1087 $v *= 255;
211 328 100       901 if( $s == 0 ) { return _fmt( $v, $v, $v ); } # achromatic (grey)
  6         18  
212              
213 322         2391 my $i = int( $h/60 ); # sector 0 to 5
214 322         495 my $f = ($h/60) - $i; # fractional part of h/60
215              
216 322         449 my $p = $v * ( 1 - $s );
217 322         469 my $q = $v * ( 1 - $s * $f );
218 322         550 my $t = $v * ( 1 - $s * ( 1 - $f ) );
219              
220 322         424 $i %= 6; # tolerate values of $h larger than 360
221 322 100       1165 if( $i==0 ) { return _fmt( $v, $t, $p ); }
  69 100       182  
    100          
    100          
    100          
    50          
222 60         491 elsif( $i==1 ) { return _fmt( $q, $v, $p ); }
223 36         616 elsif( $i==2 ) { return _fmt( $p, $v, $t ); }
224 60         321 elsif( $i==3 ) { return _fmt( $p, $q, $v ); }
225 61         141 elsif( $i==4 ) { return _fmt( $t, $p, $v ); }
226 36         87 elsif( $i==5 ) { return _fmt( $v, $p, $q ); }
227             else {
228             # Never get here!
229 0         0 croak "Internal Error: Unexpected value ,$i, in Graphics::ColorUtils::hsv2rgb( $h, $s, $v )";
230             }
231             }
232              
233             # ==================================================
234             # Gradients
235              
236             # Gradients grey, heat, map, and rainbow have been inspired by similar
237             # ideas in Yorick.
238             # For Yorick, cf http://yorick.sourceforge.net
239             # and also http://www.maumae.net/yorick/doc/index.php
240             # as well as http://www.mhatt.aps.anl.gov/dohn/software/yorick/
241              
242             BEGIN {
243 4     4   27020 my %_gradients = (
244             'grey' => [
245             [ 0, 0, 0],[ 1, 1, 1],[ 2, 2, 2],[ 3, 3, 3],[ 4, 4, 4],
246             [ 5, 5, 5],[ 6, 6, 6],[ 7, 7, 7],[ 9, 9, 9],[ 10, 10, 10],
247             [ 11, 11, 11],[ 12, 12, 12],[ 13, 13, 13],[ 14, 14, 14],[ 15, 15, 15],
248             [ 16, 16, 16],[ 17, 17, 17],[ 18, 18, 18],[ 19, 19, 19],[ 20, 20, 20],
249             [ 21, 21, 21],[ 22, 22, 22],[ 23, 23, 23],[ 25, 25, 25],[ 26, 26, 26],
250             [ 27, 27, 27],[ 28, 28, 28],[ 29, 29, 29],[ 30, 30, 30],[ 31, 31, 31],
251             [ 32, 32, 32],[ 33, 33, 33],[ 34, 34, 34],[ 35, 35, 35],[ 36, 36, 36],
252             [ 37, 37, 37],[ 38, 38, 38],[ 39, 39, 39],[ 41, 41, 41],[ 42, 42, 42],
253             [ 43, 43, 43],[ 44, 44, 44],[ 45, 45, 45],[ 46, 46, 46],[ 47, 47, 47],
254             [ 48, 48, 48],[ 49, 49, 49],[ 50, 50, 50],[ 51, 51, 51],[ 52, 52, 52],
255             [ 53, 53, 53],[ 54, 54, 54],[ 55, 55, 55],[ 57, 57, 57],[ 58, 58, 58],
256             [ 59, 59, 59],[ 60, 60, 60],[ 61, 61, 61],[ 62, 62, 62],[ 63, 63, 63],
257             [ 64, 64, 64],[ 65, 65, 65],[ 66, 66, 66],[ 67, 67, 67],[ 68, 68, 68],
258             [ 69, 69, 69],[ 70, 70, 70],[ 71, 71, 71],[ 73, 73, 73],[ 74, 74, 74],
259             [ 75, 75, 75],[ 76, 76, 76],[ 77, 77, 77],[ 78, 78, 78],[ 79, 79, 79],
260             [ 80, 80, 80],[ 81, 81, 81],[ 82, 82, 82],[ 83, 83, 83],[ 84, 84, 84],
261             [ 85, 85, 85],[ 86, 86, 86],[ 87, 87, 87],[ 89, 89, 89],[ 90, 90, 90],
262             [ 91, 91, 91],[ 92, 92, 92],[ 93, 93, 93],[ 94, 94, 94],[ 95, 95, 95],
263             [ 96, 96, 96],[ 97, 97, 97],[ 98, 98, 98],[ 99, 99, 99],[100,100,100],
264             [101,101,101],[102,102,102],[103,103,103],[105,105,105],[106,106,106],
265             [107,107,107],[108,108,108],[109,109,109],[110,110,110],[111,111,111],
266             [112,112,112],[113,113,113],[114,114,114],[115,115,115],[116,116,116],
267             [117,117,117],[118,118,118],[119,119,119],[121,121,121],[122,122,122],
268             [123,123,123],[124,124,124],[125,125,125],[126,126,126],[127,127,127],
269             [128,128,128],[129,129,129],[130,130,130],[131,131,131],[132,132,132],
270             [133,133,133],[134,134,134],[135,135,135],[137,137,137],[138,138,138],
271             [139,139,139],[140,140,140],[141,141,141],[142,142,142],[143,143,143],
272             [144,144,144],[145,145,145],[146,146,146],[147,147,147],[148,148,148],
273             [149,149,149],[150,150,150],[151,151,151],[153,153,153],[154,154,154],
274             [155,155,155],[156,156,156],[157,157,157],[158,158,158],[159,159,159],
275             [160,160,160],[161,161,161],[162,162,162],[163,163,163],[164,164,164],
276             [165,165,165],[166,166,166],[167,167,167],[169,169,169],[170,170,170],
277             [171,171,171],[172,172,172],[173,173,173],[174,174,174],[175,175,175],
278             [176,176,176],[177,177,177],[178,178,178],[179,179,179],[180,180,180],
279             [181,181,181],[182,182,182],[183,183,183],[185,185,185],[186,186,186],
280             [187,187,187],[188,188,188],[189,189,189],[190,190,190],[191,191,191],
281             [192,192,192],[193,193,193],[194,194,194],[195,195,195],[196,196,196],
282             [197,197,197],[198,198,198],[199,199,199],[201,201,201],[202,202,202],
283             [203,203,203],[204,204,204],[205,205,205],[206,206,206],[207,207,207],
284             [208,208,208],[209,209,209],[210,210,210],[211,211,211],[212,212,212],
285             [213,213,213],[214,214,214],[215,215,215],[217,217,217],[218,218,218],
286             [219,219,219],[220,220,220],[221,221,221],[222,222,222],[223,223,223],
287             [224,224,224],[225,225,225],[226,226,226],[227,227,227],[228,228,228],
288             [229,229,229],[230,230,230],[231,231,231],[233,233,233],[234,234,234],
289             [235,235,235],[236,236,236],[237,237,237],[238,238,238],[239,239,239],
290             [240,240,240],[241,241,241],[242,242,242],[243,243,243],[244,244,244],
291             [245,245,245],[246,246,246],[247,247,247],[249,249,249],[250,250,250],
292             [251,251,251],[252,252,252],[253,253,253],[254,254,254],[255,255,255]
293             ],
294             'heat' => [
295             [ 0, 0, 0],[ 1, 0, 0],[ 2, 0, 0],[ 4, 0, 0],[ 5, 0, 0],
296             [ 7, 0, 0],[ 8, 0, 0],[ 10, 0, 0],[ 11, 0, 0],[ 13, 0, 0],
297             [ 15, 0, 0],[ 17, 0, 0],[ 18, 0, 0],[ 20, 0, 0],[ 21, 0, 0],
298             [ 23, 0, 0],[ 24, 0, 0],[ 26, 0, 0],[ 27, 0, 0],[ 28, 0, 0],
299             [ 30, 0, 0],[ 31, 0, 0],[ 33, 0, 0],[ 34, 0, 0],[ 36, 0, 0],
300             [ 37, 0, 0],[ 39, 0, 0],[ 40, 0, 0],[ 42, 0, 0],[ 43, 0, 0],
301             [ 46, 0, 0],[ 47, 0, 0],[ 49, 0, 0],[ 50, 0, 0],[ 52, 0, 0],
302             [ 53, 0, 0],[ 55, 0, 0],[ 56, 0, 0],[ 57, 0, 0],[ 59, 0, 0],
303             [ 60, 0, 0],[ 62, 0, 0],[ 63, 0, 0],[ 65, 0, 0],[ 66, 0, 0],
304             [ 68, 0, 0],[ 69, 0, 0],[ 70, 0, 0],[ 72, 0, 0],[ 73, 0, 0],
305             [ 76, 0, 0],[ 78, 0, 0],[ 79, 0, 0],[ 81, 0, 0],[ 82, 0, 0],
306             [ 84, 0, 0],[ 85, 0, 0],[ 86, 0, 0],[ 88, 0, 0],[ 89, 0, 0],
307             [ 92, 0, 0],[ 94, 0, 0],[ 95, 0, 0],[ 97, 0, 0],[ 98, 0, 0],
308             [ 99, 0, 0],[101, 0, 0],[102, 0, 0],[104, 0, 0],[105, 0, 0],
309             [108, 0, 0],[110, 0, 0],[111, 0, 0],[113, 0, 0],[114, 0, 0],
310             [115, 0, 0],[117, 0, 0],[118, 0, 0],[120, 0, 0],[121, 0, 0],
311             [123, 0, 0],[124, 0, 0],[126, 0, 0],[127, 0, 0],[128, 0, 0],
312             [130, 0, 0],[131, 0, 0],[133, 0, 0],[134, 0, 0],[136, 0, 0],
313             [139, 0, 0],[140, 0, 0],[141, 0, 0],[143, 0, 0],[144, 0, 0],
314             [146, 0, 0],[147, 0, 0],[149, 0, 0],[150, 0, 0],[152, 0, 0],
315             [153, 0, 0],[155, 0, 0],[156, 0, 0],[157, 0, 0],[159, 0, 0],
316             [160, 0, 0],[162, 0, 0],[163, 0, 0],[165, 0, 0],[166, 0, 0],
317             [169, 0, 0],[170, 0, 0],[172, 0, 0],[173, 0, 0],[175, 1, 0],
318             [176, 3, 0],[178, 5, 0],[179, 7, 0],[181, 9, 0],[182, 11, 0],
319             [185, 15, 0],[186, 17, 0],[188, 18, 0],[189, 20, 0],[191, 22, 0],
320             [192, 24, 0],[194, 26, 0],[195, 28, 0],[197, 30, 0],[198, 32, 0],
321             [201, 35, 0],[202, 37, 0],[204, 39, 0],[205, 41, 0],[207, 43, 0],
322             [208, 45, 0],[210, 47, 0],[211, 49, 0],[212, 51, 0],[214, 52, 0],
323             [215, 54, 0],[217, 56, 0],[218, 58, 0],[220, 60, 0],[221, 62, 0],
324             [223, 64, 0],[224, 66, 0],[226, 68, 0],[227, 69, 0],[228, 71, 0],
325             [231, 75, 0],[233, 77, 0],[234, 79, 0],[236, 81, 0],[237, 83, 0],
326             [239, 85, 0],[240, 86, 0],[241, 88, 0],[243, 90, 0],[244, 92, 0],
327             [246, 94, 0],[247, 96, 0],[249, 98, 0],[250,100, 0],[252,102, 0],
328             [253,103, 0],[255,105, 0],[255,107, 0],[255,109, 0],[255,111, 0],
329             [255,115, 0],[255,117, 0],[255,119, 0],[255,120, 0],[255,122, 0],
330             [255,124, 0],[255,126, 0],[255,128, 0],[255,130, 0],[255,132, 0],
331             [255,136, 7],[255,137, 11],[255,139, 15],[255,141, 19],[255,143, 23],
332             [255,145, 27],[255,147, 31],[255,149, 35],[255,151, 39],[255,153, 43],
333             [255,156, 51],[255,158, 54],[255,160, 58],[255,162, 62],[255,164, 66],
334             [255,166, 70],[255,168, 74],[255,170, 78],[255,171, 82],[255,173, 86],
335             [255,175, 90],[255,177, 94],[255,179, 98],[255,181,102],[255,183,105],
336             [255,185,109],[255,187,113],[255,188,117],[255,190,121],[255,192,125],
337             [255,196,133],[255,198,137],[255,200,141],[255,202,145],[255,204,149],
338             [255,205,153],[255,207,156],[255,209,160],[255,211,164],[255,213,168],
339             [255,215,172],[255,217,176],[255,219,180],[255,221,184],[255,222,188],
340             [255,224,192],[255,226,196],[255,228,200],[255,230,204],[255,232,207],
341             [255,236,215],[255,238,219],[255,239,223],[255,241,227],[255,243,231],
342             [255,245,235],[255,247,239],[255,249,243],[255,251,247],[255,253,251]
343             ],
344             'map' => [
345             [ 0, 0, 0],[ 0, 0, 46],[ 0, 0, 58],[ 0, 0, 69],[ 0, 0, 81],
346             [ 0, 0, 92],[ 0, 0,104],[ 0, 0,116],[ 0, 3,116],[ 1, 6,116],
347             [ 2, 8,116],[ 2, 11,116],[ 3, 13,117],[ 4, 16,117],[ 5, 18,117],
348             [ 5, 21,117],[ 6, 23,117],[ 7, 26,118],[ 8, 28,118],[ 8, 31,118],
349             [ 9, 33,118],[ 10, 36,118],[ 11, 38,119],[ 11, 41,119],[ 12, 43,119],
350             [ 13, 45,119],[ 14, 48,119],[ 15, 50,120],[ 15, 52,120],[ 16, 55,120],
351             [ 17, 57,120],[ 18, 59,120],[ 18, 61,121],[ 19, 64,121],[ 20, 66,121],
352             [ 21, 68,121],[ 22, 70,121],[ 22, 72,122],[ 23, 74,122],[ 24, 77,122],
353             [ 25, 79,122],[ 26, 81,122],[ 26, 83,123],[ 27, 85,123],[ 28, 87,123],
354             [ 29, 89,123],[ 30, 91,123],[ 31, 93,124],[ 31, 95,124],[ 32, 97,124],
355             [ 33, 99,124],[ 34,100,124],[ 35,102,125],[ 36,104,125],[ 36,106,125],
356             [ 37,108,125],[ 38,109,125],[ 39,111,126],[ 40,113,126],[ 41,115,126],
357             [ 41,116,126],[ 42,118,126],[ 43,120,127],[ 44,121,127],[ 45,123,127],
358             [ 46,125,127],[ 47,126,127],[ 48,128,128],[ 48,128,126],[ 48,129,125],
359             [ 49,129,124],[ 49,130,123],[ 50,131,122],[ 50,131,120],[ 51,132,119],
360             [ 51,133,118],[ 52,133,117],[ 52,134,115],[ 53,134,114],[ 53,135,113],
361             [ 54,136,111],[ 54,136,110],[ 55,137,109],[ 55,138,108],[ 56,138,106],
362             [ 56,139,105],[ 57,140,104],[ 57,140,102],[ 58,141,101],[ 58,141,100],
363             [ 59,142, 98],[ 59,143, 97],[ 60,143, 96],[ 61,144, 94],[ 61,145, 93],
364             [ 62,145, 92],[ 62,146, 90],[ 63,146, 89],[ 63,147, 88],[ 64,148, 86],
365             [ 64,148, 85],[ 65,149, 84],[ 65,150, 82],[ 66,150, 81],[ 67,151, 80],
366             [ 67,151, 78],[ 68,152, 77],[ 68,153, 76],[ 69,153, 74],[ 69,154, 73],
367             [ 70,155, 71],[ 71,155, 70],[ 73,156, 71],[ 76,156, 72],[ 78,157, 72],
368             [ 81,158, 73],[ 83,158, 73],[ 86,159, 74],[ 88,160, 75],[ 91,160, 75],
369             [ 94,161, 76],[ 96,161, 76],[ 99,162, 77],[101,163, 77],[104,163, 78],
370             [106,164, 79],[109,165, 79],[111,165, 80],[114,166, 80],[117,166, 81],
371             [119,167, 82],[121,168, 82],[122,168, 82],[124,168, 83],[126,169, 83],
372             [128,169, 83],[129,170, 84],[131,170, 84],[133,171, 84],[135,171, 85],
373             [136,172, 85],[138,172, 85],[140,172, 86],[141,173, 86],[143,173, 86],
374             [145,174, 87],[147,174, 87],[149,175, 87],[150,175, 88],[152,175, 88],
375             [154,176, 88],[156,176, 89],[157,177, 89],[159,177, 89],[161,178, 90],
376             [163,178, 90],[165,179, 90],[166,179, 91],[168,179, 91],[170,180, 91],
377             [172,180, 92],[174,181, 92],[175,181, 92],[177,182, 93],[179,182, 93],
378             [181,183, 93],[183,183, 94],[183,182, 94],[184,181, 94],[184,181, 95],
379             [185,180, 95],[185,179, 95],[186,178, 96],[186,177, 96],[187,176, 97],
380             [187,175, 97],[187,174, 97],[188,173, 98],[188,172, 98],[189,171, 98],
381             [189,170, 99],[190,169, 99],[190,168, 99],[190,167,100],[191,166,100],
382             [191,165,100],[192,164,101],[192,163,101],[193,163,104],[195,164,106],
383             [196,164,108],[197,165,111],[198,165,113],[199,166,116],[201,167,118],
384             [202,167,121],[203,168,123],[204,169,126],[205,170,129],[207,171,131],
385             [208,172,134],[209,173,137],[210,174,139],[211,175,142],[213,176,145],
386             [214,177,148],[215,178,150],[216,179,153],[217,181,156],[219,182,159],
387             [220,184,162],[221,185,165],[222,187,168],[223,188,170],[225,190,173],
388             [226,192,176],[227,194,179],[228,196,182],[229,198,185],[231,200,189],
389             [232,202,192],[233,204,195],[234,206,198],[235,208,201],[237,211,204],
390             [238,213,207],[239,215,211],[240,218,214],[241,221,217],[243,223,220],
391             [244,226,224],[245,229,227],[246,232,230],[247,235,234],[249,238,237],
392             [250,241,241],[251,244,244],[252,248,248],[253,251,251],[255,255,255]
393             ],
394             'rainbow' => [
395             [255, 0, 42],[255, 0, 36],[255, 0, 31],[255, 0, 26],[255, 0, 20],
396             [255, 0, 15],[255, 0, 10],[255, 0, 4],[255, 5, 0],[255, 11, 0],
397             [255, 16, 0],[255, 22, 0],[255, 27, 0],[255, 32, 0],[255, 38, 0],
398             [255, 43, 0],[255, 48, 0],[255, 54, 0],[255, 59, 0],[255, 65, 0],
399             [255, 70, 0],[255, 75, 0],[255, 81, 0],[255, 91, 0],[255, 97, 0],
400             [255,102, 0],[255,108, 0],[255,113, 0],[255,118, 0],[255,124, 0],
401             [255,129, 0],[255,135, 0],[255,140, 0],[255,145, 0],[255,151, 0],
402             [255,156, 0],[255,161, 0],[255,167, 0],[255,178, 0],[255,183, 0],
403             [255,188, 0],[255,194, 0],[255,199, 0],[255,204, 0],[255,210, 0],
404             [255,215, 0],[255,221, 0],[255,226, 0],[255,231, 0],[255,237, 0],
405             [255,242, 0],[255,247, 0],[255,253, 0],[245,255, 0],[240,255, 0],
406             [235,255, 0],[229,255, 0],[224,255, 0],[219,255, 0],[213,255, 0],
407             [208,255, 0],[202,255, 0],[197,255, 0],[192,255, 0],[186,255, 0],
408             [181,255, 0],[175,255, 0],[170,255, 0],[159,255, 0],[154,255, 0],
409             [149,255, 0],[143,255, 0],[138,255, 0],[132,255, 0],[127,255, 0],
410             [122,255, 0],[116,255, 0],[111,255, 0],[106,255, 0],[100,255, 0],
411             [ 95,255, 0],[ 89,255, 0],[ 84,255, 0],[ 73,255, 0],[ 68,255, 0],
412             [ 63,255, 0],[ 57,255, 0],[ 52,255, 0],[ 46,255, 0],[ 41,255, 0],
413             [ 36,255, 0],[ 30,255, 0],[ 25,255, 0],[ 19,255, 0],[ 14,255, 0],
414             [ 9,255, 0],[ 3,255, 0],[ 0,255, 1],[ 0,255, 12],[ 0,255, 17],
415             [ 0,255, 23],[ 0,255, 28],[ 0,255, 33],[ 0,255, 39],[ 0,255, 44],
416             [ 0,255, 49],[ 0,255, 55],[ 0,255, 60],[ 0,255, 66],[ 0,255, 71],
417             [ 0,255, 76],[ 0,255, 82],[ 0,255, 87],[ 0,255, 98],[ 0,255,103],
418             [ 0,255,109],[ 0,255,114],[ 0,255,119],[ 0,255,125],[ 0,255,130],
419             [ 0,255,135],[ 0,255,141],[ 0,255,146],[ 0,255,152],[ 0,255,157],
420             [ 0,255,162],[ 0,255,168],[ 0,255,173],[ 0,255,184],[ 0,255,189],
421             [ 0,255,195],[ 0,255,200],[ 0,255,205],[ 0,255,211],[ 0,255,216],
422             [ 0,255,222],[ 0,255,227],[ 0,255,232],[ 0,255,238],[ 0,255,243],
423             [ 0,255,248],[ 0,255,254],[ 0,250,255],[ 0,239,255],[ 0,234,255],
424             [ 0,228,255],[ 0,223,255],[ 0,218,255],[ 0,212,255],[ 0,207,255],
425             [ 0,201,255],[ 0,196,255],[ 0,191,255],[ 0,185,255],[ 0,180,255],
426             [ 0,174,255],[ 0,169,255],[ 0,164,255],[ 0,153,255],[ 0,148,255],
427             [ 0,142,255],[ 0,137,255],[ 0,131,255],[ 0,126,255],[ 0,121,255],
428             [ 0,115,255],[ 0,110,255],[ 0,105,255],[ 0, 99,255],[ 0, 94,255],
429             [ 0, 88,255],[ 0, 83,255],[ 0, 78,255],[ 0, 67,255],[ 0, 62,255],
430             [ 0, 56,255],[ 0, 51,255],[ 0, 45,255],[ 0, 40,255],[ 0, 35,255],
431             [ 0, 29,255],[ 0, 24,255],[ 0, 18,255],[ 0, 13,255],[ 0, 8,255],
432             [ 0, 2,255],[ 2, 0,255],[ 7, 0,255],[ 18, 0,255],[ 24, 0,255],
433             [ 29, 0,255],[ 34, 0,255],[ 40, 0,255],[ 45, 0,255],[ 50, 0,255],
434             [ 56, 0,255],[ 61, 0,255],[ 67, 0,255],[ 72, 0,255],[ 77, 0,255],
435             [ 83, 0,255],[ 88, 0,255],[ 93, 0,255],[104, 0,255],[110, 0,255],
436             [115, 0,255],[120, 0,255],[126, 0,255],[131, 0,255],[136, 0,255],
437             [142, 0,255],[147, 0,255],[153, 0,255],[158, 0,255],[163, 0,255],
438             [169, 0,255],[174, 0,255],[180, 0,255],[190, 0,255],[196, 0,255],
439             [201, 0,255],[206, 0,255],[212, 0,255],[217, 0,255],[223, 0,255],
440             [228, 0,255],[233, 0,255],[239, 0,255],[244, 0,255],[249, 0,255],
441             [255, 0,254],[255, 0,249],[255, 0,243],[255, 0,233],[255, 0,227],
442             [255, 0,222],[255, 0,217],[255, 0,211],[255, 0,206],[255, 0,201]
443             ]
444             );
445              
446             # Returns a hash: gradient-name => color-count
447             sub available_gradients {
448 1     1 1 12 return map { $_, scalar( @{ $_gradients{$_} } ) } keys %_gradients;
  4         6  
  4         20  
449             }
450              
451             # Returns array-ref of rgb-triples, undef if gradient-name not found
452             sub gradient {
453 4     4 1 78578 my ( $name ) = @_;
454              
455 4 100       21 unless( exists $_gradients{ $name } ) { return; }
  1         5  
456              
457 3         15 return $_gradients{$name};
458             }
459              
460             # Returns the color corresponding to the position in the gradient given by f.
461             # Returns undef when gradient not found or f outside valid range.
462             sub grad2rgb {
463 31     31 1 25647 my ( $name, $frac ) = @_;
464              
465 31 100       102 unless( exists $_gradients{ $name } ) { return; }
  1         5  
466 30 100 100     2847 if( $frac < 0.0 || $frac >= 1.0 ) { return; }
  3         14  
467              
468 27         38 my $idx = int( $frac * scalar( @{$_gradients{$name}} ) );
  27         365  
469 27         34 return _fmt( @{ $_gradients{$name}[$idx] } );
  27         103  
470             }
471              
472             # Expects a gradient and and array-ref to an array of rgb triples.
473             # If the name already exists, the function returns the old array; undef otherws
474             sub register_gradient {
475 3     3 1 5 my ( $name, $array_ref ) = @_;
476              
477 3 100       10 if( exists $_gradients{ $name } ) {
478 2         24 my $old = $_gradients{ $name };
479 2         4 $_gradients{ $name } = $array_ref;
480 2         6 return $old;
481             }
482              
483 1         4 $_gradients{ $name } = $array_ref;
484 1         3 return undef;
485             }
486              
487             } # end BEGIN (Gradients)
488              
489             # ==================================================
490             # Names
491              
492             BEGIN {
493              
494 4     4   17 my $_default_namespace = 'x11';
495              
496 4         6226 my %_colors = (
497             'www:aqua' => [ 0,255,255],'www:black' => [ 0, 0, 0],
498             'www:blue' => [ 0, 0,255],'www:fuchsia' => [255, 0,255],
499             'www:gray' => [190,190,190],'www:green' => [ 0,128, 0],
500             'www:lime' => [ 0,255, 0],'www:maroon' => [128, 0, 0],
501             'www:navy' => [ 0, 0,128],'www:olive' => [128,128, 0],
502             'www:purple' => [128, 0,128],'www:red' => [255, 0, 0],
503             'www:silver' => [192,192,192],'www:teal' => [ 0,128,128],
504             'www:white' => [255,255,255],'www:yellow' => [255,255, 0],
505             'www:orange' => [255,165, 0],
506              
507             'svg:palevioletred' => [219,112,147],'svg:mediumslateblue' => [123,104,238],
508             'svg:gold' => [255,215,0],'svg:gainsboro' => [220,220,220],
509             'svg:yellow' => [255,255,0],'svg:limegreen' => [50,205,50],
510             'svg:lightgoldenrodyellow' => [250,250,210],'svg:lavenderblush' => [255,240,245],
511             'svg:darkmagenta' => [139,0,139],'svg:darkgrey' => [169,169,169],
512             'svg:blanchedalmond' => [255,235,205],'svg:ghostwhite' => [248,248,255],
513             'svg:floralwhite' => [255,250,240],'svg:coral' => [255,127,80],
514             'svg:honeydew' => [240,255,240],'svg:mistyrose' => [255,228,225],
515             'svg:slateblue' => [106,90,205],'svg:goldenrod' => [218,165,32],
516             'svg:darkcyan' => [0,139,139],'svg:moccasin' => [255,228,181],
517             'svg:mediumvioletred' => [199,21,133],'svg:maroon' => [128,0,0],
518             'svg:lightpink' => [255,182,193],'svg:lightsalmon' => [255,160,122],
519             'svg:paleturquoise' => [175,238,238],'svg:darksalmon' => [233,150,122],
520             'svg:yellowgreen' => [154,205,50],'svg:mediumturquoise' => [72,209,204],
521             'svg:chartreuse' => [127,255,0],'svg:peru' => [205,133,63],
522             'svg:palegoldenrod' => [238,232,170],'svg:red' => [255,0,0],
523             'svg:lavender' => [230,230,250],'svg:lightseagreen' => [32,178,170],
524             'svg:powderblue' => [176,224,230],'svg:orchid' => [218,112,214],
525             'svg:cornsilk' => [255,248,220],'svg:seagreen' => [46,139,87],
526             'svg:royalblue' => [65,105,225],'svg:ivory' => [255,255,240],
527             'svg:tan' => [210,180,140],'svg:linen' => [250,240,230],
528             'svg:darkorchid' => [153,50,204],'svg:tomato' => [255,99,71],
529             'svg:lightcyan' => [224,255,255],'svg:darkolivegreen' => [85,107,47],
530             'svg:sienna' => [160,82,45],'svg:lightsteelblue' => [176,196,222],
531             'svg:indigo' => [75,0,130],'svg:peachpuff' => [255,218,185],
532             'svg:lime' => [0,255,0],'svg:mediumspringgreen' => [0,250,154],
533             'svg:silver' => [192,192,192],'svg:saddlebrown' => [139,69,19],
534             'svg:lightyellow' => [255,255,224],'svg:grey' => [128,128,128],
535             'svg:thistle' => [216,191,216],'svg:deepskyblue' => [0,191,255],
536             'svg:lightgreen' => [144,238,144],'svg:blueviolet' => [138,43,226],
537             'svg:aqua' => [0,255,255],'svg:cyan' => [0,255,255],
538             'svg:papayawhip' => [255,239,213],'svg:deeppink' => [255,20,147],
539             'svg:firebrick' => [178,34,34],'svg:navy' => [0,0,128],
540             'svg:hotpink' => [255,105,180],'svg:pink' => [255,192,203],
541             'svg:darkturquoise' => [0,206,209],'svg:navajowhite' => [255,222,173],
542             'svg:lightslategrey' => [119,136,153],'svg:lawngreen' => [124,252,0],
543             'svg:lightcoral' => [240,128,128],'svg:palegreen' => [152,251,152],
544             'svg:dodgerblue' => [30,144,255],'svg:greenyellow' => [173,255,47],
545             'svg:lightskyblue' => [135,206,250],'svg:brown' => [165,42,42],
546             'svg:dimgrey' => [105,105,105],'svg:aquamarine' => [127,255,212],
547             'svg:darkseagreen' => [143,188,143],'svg:fuchsia' => [255,0,255],
548             'svg:magenta' => [255,0,255],'svg:chocolate' => [210,105,30],
549             'svg:mediumseagreen' => [60,179,113],'svg:cadetblue' => [95,158,160],
550             'svg:purple' => [128,0,128],'svg:turquoise' => [64,224,208],
551             'svg:darkkhaki' => [189,183,107],'svg:antiquewhite' => [250,235,215],
552             'svg:skyblue' => [135,206,235],'svg:sandybrown' => [244,164,96],
553             'svg:mediumblue' => [0,0,205],'svg:steelblue' => [70,130,180],
554             'svg:indianred' => [205,92,92],'svg:khaki' => [240,230,140],
555             'svg:lightblue' => [173,216,230],'svg:green' => [0,128,0],
556             'svg:olive' => [128,128,0],'svg:mediumorchid' => [186,85,211],
557             'svg:blue' => [0,0,255],'svg:snow' => [255,250,250],
558             'svg:rosybrown' => [188,143,143],'svg:orange' => [255,165,0],
559             'svg:slategrey' => [112,128,144],'svg:darkorange' => [255,140,0],
560             'svg:violet' => [238,130,238],'svg:darkslategrey' => [47,79,79],
561             'svg:whitesmoke' => [245,245,245],'svg:burlywood' => [222,184,135],
562             'svg:darkgreen' => [0,100,0],'svg:lemonchiffon' => [255,250,205],
563             'svg:midnightblue' => [25,25,112],'svg:mintcream' => [245,255,250],
564             'svg:oldlace' => [253,245,230],'svg:black' => [0,0,0],
565             'svg:bisque' => [255,228,196],'svg:mediumaquamarine' => [102,205,170],
566             'svg:olivedrab' => [107,142,35],'svg:salmon' => [250,128,114],
567             'svg:teal' => [0,128,128],'svg:seashell' => [255,245,238],
568             'svg:springgreen' => [0,255,127],'svg:plum' => [221,160,221],
569             'svg:darkviolet' => [148,0,211],'svg:wheat' => [245,222,179],
570             'svg:mediumpurple' => [147,112,219],'svg:cornflowerblue' => [100,149,237],
571             'svg:forestgreen' => [34,139,34],'svg:darkgoldenrod' => [184,134,11],
572             'svg:aliceblue' => [240,248,255],'svg:white' => [255,255,255],
573             'svg:darkblue' => [0,0,139],'svg:azure' => [240,255,255],
574             'svg:darkred' => [139,0,0],'svg:orangered' => [255,69,0],
575             'svg:darkslateblue' => [72,61,139],'svg:crimson' => [220,20,60],
576             'svg:lightgrey' => [211,211,211],'svg:beige' => [245,245,220],
577              
578             'x11:deepskyblue3' => [0,154,205],'x11:gold' => [255,215,0],
579             'x11:gold1' => [255,215,0],'x11:mediumpurple3' => [137,104,205],
580             'x11:royalblue3' => [58,95,205],'x11:lightgoldenrodyellow' => [250,250,210],
581             'x11:lavenderblush' => [255,240,245],'x11:lavenderblush1' => [255,240,245],
582             'x11:pink1' => [255,181,197],'x11:green3' => [0,205,0],
583             'x11:lightsteelblue1' => [202,225,255],'x11:blanchedalmond' => [255,235,205],
584             'x11:salmon1' => [255,140,105],'x11:ghostwhite' => [248,248,255],
585             'x11:floralwhite' => [255,250,240],'x11:dodgerblue4' => [16,78,139],
586             'x11:grey43' => [110,110,110],'x11:indianred4' => [139,58,58],
587             'x11:mistyrose1' => [255,228,225],'x11:mistyrose' => [255,228,225],
588             'x11:dodgerblue2' => [28,134,238],'x11:grey37' => [94,94,94],
589             'x11:grey9' => [23,23,23],'x11:purple4' => [85,26,139],
590             'x11:orchid2' => [238,122,233],'x11:cornsilk3' => [205,200,177],
591             'x11:goldenrod' => [218,165,32],'x11:hotpink4' => [139,58,98],
592             'x11:lightpink' => [255,182,193],'x11:coral2' => [238,106,80],
593             'x11:cyan2' => [0,238,238],'x11:grey87' => [222,222,222],
594             'x11:grey91' => [232,232,232],'x11:violetred4' => [139,34,82],
595             'x11:violetred2' => [238,58,140],'x11:indianred2' => [238,99,99],
596             'x11:lightyellow3' => [205,205,180],'x11:darkolivegreen2' => [188,238,104],
597             'x11:magenta3' => [205,0,205],'x11:grey64' => [163,163,163],
598             'x11:honeydew3' => [193,205,193],'x11:lightsalmon3' => [205,129,98],
599             'x11:springgreen4' => [0,139,69],'x11:grey57' => [145,145,145],
600             'x11:grey50' => [127,127,127],'x11:grey66' => [168,168,168],
601             'x11:antiquewhite1' => [255,239,219],'x11:paleturquoise' => [175,238,238],
602             'x11:navajowhite2' => [238,207,161],'x11:lightpink3' => [205,140,149],
603             'x11:darksalmon' => [233,150,122],'x11:grey52' => [133,133,133],
604             'x11:slategrey3' => [159,182,205],'x11:darkseagreen4' => [105,139,105],
605             'x11:chartreuse' => [127,255,0],'x11:chartreuse1' => [127,255,0],
606             'x11:grey42' => [107,107,107],'x11:peru' => [205,133,63],
607             'x11:tan3' => [205,133,63],'x11:grey19' => [48,48,48],
608             'x11:palegreen3' => [124,205,124],'x11:lavender' => [230,230,250],
609             'x11:red3' => [205,0,0],'x11:orchid' => [218,112,214],
610             'x11:powderblue' => [176,224,230],'x11:grey35' => [89,89,89],
611             'x11:plum4' => [139,102,139],'x11:cornsilk' => [255,248,220],
612             'x11:cornsilk1' => [255,248,220],'x11:royalblue' => [65,105,225],
613             'x11:darkgoldenrod2' => [238,173,14],'x11:lightpink4' => [139,95,101],
614             'x11:springgreen2' => [0,238,118],'x11:tan' => [210,180,140],
615             'x11:lightslateblue' => [132,112,255],'x11:darkorchid' => [153,50,204],
616             'x11:orangered2' => [238,64,0],'x11:palevioletred1' => [255,130,171],
617             'x11:grey63' => [161,161,161],'x11:maroon2' => [238,48,167],
618             'x11:blue2' => [0,0,238],'x11:turquoise4' => [0,134,139],
619             'x11:lightcyan1' => [224,255,255],'x11:lightcyan' => [224,255,255],
620             'x11:springgreen3' => [0,205,102],'x11:darkorchid4' => [104,34,139],
621             'x11:sienna' => [160,82,45],'x11:goldenrod2' => [238,180,34],
622             'x11:lightgoldenrod3' => [205,190,112],'x11:green' => [0,255,0],
623             'x11:green1' => [0,255,0],'x11:peachpuff1' => [255,218,185],
624             'x11:peachpuff' => [255,218,185],'x11:yellow3' => [205,205,0],
625             'x11:mediumspringgreen' => [0,250,154],'x11:cadetblue3' => [122,197,205],
626             'x11:royalblue1' => [72,118,255],'x11:deepskyblue1' => [0,191,255],
627             'x11:deepskyblue' => [0,191,255],'x11:firebrick1' => [255,48,48],
628             'x11:grey80' => [204,204,204],'x11:grey28' => [71,71,71],
629             'x11:palegreen2' => [144,238,144],'x11:lightgreen' => [144,238,144],
630             'x11:blueviolet' => [138,43,226],'x11:deeppink1' => [255,20,147],
631             'x11:deeppink' => [255,20,147],'x11:deeppink2' => [238,18,137],
632             'x11:lightskyblue2' => [164,211,238],'x11:grey77' => [196,196,196],
633             'x11:grey72' => [184,184,184],'x11:tomato2' => [238,92,66],
634             'x11:steelblue2' => [92,172,238],'x11:hotpink' => [255,105,180],
635             'x11:slateblue4' => [71,60,139],'x11:pink' => [255,192,203],
636             'x11:darkturquoise' => [0,206,209],'x11:antiquewhite3' => [205,192,176],
637             'x11:grey32' => [82,82,82],'x11:lightyellow2' => [238,238,209],
638             'x11:olivedrab4' => [105,139,34],'x11:lightblue4' => [104,131,139],
639             'x11:royalblue2' => [67,110,238],'x11:navajowhite1' => [255,222,173],
640             'x11:navajowhite' => [255,222,173],'x11:lightgoldenrod' => [238,221,130],
641             'x11:grey85' => [217,217,217],'x11:maroon4' => [139,28,98],
642             'x11:grey90' => [229,229,229],'x11:grey17' => [43,43,43],
643             'x11:seashell4' => [139,134,130],'x11:greenyellow' => [173,255,47],
644             'x11:dodgerblue1' => [30,144,255],'x11:dodgerblue' => [30,144,255],
645             'x11:grey89' => [227,227,227],'x11:brown2' => [238,59,59],
646             'x11:paleturquoise2' => [174,238,238],'x11:lightskyblue' => [135,206,250],
647             'x11:salmon4' => [139,76,57],'x11:chocolate3' => [205,102,29],
648             'x11:grey70' => [179,179,179],'x11:grey25' => [64,64,64],
649             'x11:darkolivegreen4' => [110,139,61],'x11:mediumorchid2' => [209,95,238],
650             'x11:brown' => [165,42,42],'x11:grey67' => [171,171,171],
651             'x11:grey41' => [105,105,105],'x11:dimgrey' => [105,105,105],
652             'x11:grey60' => [153,153,153],'x11:indianred3' => [205,85,85],
653             'x11:chocolate' => [210,105,30],'x11:darkslategrey1' => [151,255,255],
654             'x11:grey2' => [5,5,5],'x11:firebrick3' => [205,38,38],
655             'x11:snow4' => [139,137,137],'x11:mediumseagreen' => [60,179,113],
656             'x11:darkorchid1' => [191,62,255],'x11:pink3' => [205,145,158],
657             'x11:violetred1' => [255,62,150],'x11:grey83' => [212,212,212],
658             'x11:olivedrab1' => [192,255,62],'x11:darkkhaki' => [189,183,107],
659             'x11:deepskyblue4' => [0,104,139],'x11:darkorchid2' => [178,58,238],
660             'x11:skyblue' => [135,206,235],'x11:mediumorchid3' => [180,82,205],
661             'x11:rosybrown4' => [139,105,105],'x11:grey16' => [41,41,41],
662             'x11:yellow4' => [139,139,0],'x11:maroon' => [176,48,96],
663             'x11:turquoise2' => [0,229,238],'x11:mistyrose2' => [238,213,210],
664             'x11:blue3' => [0,0,205],'x11:mediumblue' => [0,0,205],
665             'x11:grey4' => [10,10,10],'x11:pink2' => [238,169,184],
666             'x11:chocolate2' => [238,118,33],'x11:lightyellow4' => [139,139,122],
667             'x11:grey99' => [252,252,252],'x11:red2' => [238,0,0],
668             'x11:tan4' => [139,90,43],'x11:yellow2' => [238,238,0],
669             'x11:grey12' => [31,31,31],'x11:deeppink4' => [139,10,80],
670             'x11:lightsalmon4' => [139,87,66],'x11:lightcyan4' => [122,139,139],
671             'x11:snow1' => [255,250,250],'x11:snow' => [255,250,250],
672             'x11:brown4' => [139,35,35],'x11:darkseagreen2' => [180,238,180],
673             'x11:lightsteelblue2' => [188,210,238],'x11:rosybrown' => [188,143,143],
674             'x11:maroon1' => [255,52,179],'x11:slategrey' => [112,128,144],
675             'x11:orange' => [255,165,0],'x11:orange1' => [255,165,0],
676             'x11:orangered3' => [205,55,0],'x11:plum3' => [205,150,205],
677             'x11:turquoise3' => [0,197,205],'x11:pink4' => [139,99,108],
678             'x11:violet' => [238,130,238],'x11:grey96' => [245,245,245],
679             'x11:whitesmoke' => [245,245,245],'x11:lightgoldenrod1' => [255,236,139],
680             'x11:darkorange1' => [255,127,0],'x11:seashell2' => [238,229,222],
681             'x11:midnightblue' => [25,25,112],'x11:grey27' => [69,69,69],
682             'x11:mediumpurple2' => [159,121,238],'x11:bisque4' => [139,125,107],
683             'x11:black' => [0,0,0],'x11:grey0' => [0,0,0],
684             'x11:lavenderblush4' => [139,131,134],'x11:bisque1' => [255,228,196],
685             'x11:bisque' => [255,228,196],'x11:mediumaquamarine' => [102,205,170],
686             'x11:aquamarine3' => [102,205,170],'x11:goldenrod1' => [255,193,37],
687             'x11:green4' => [0,139,0],'x11:bisque3' => [205,183,158],
688             'x11:salmon' => [250,128,114],'x11:grey1' => [3,3,3],
689             'x11:purple3' => [125,38,205],'x11:khaki4' => [139,134,78],
690             'x11:grey' => [190,190,190],'x11:cadetblue4' => [83,134,139],
691             'x11:cadetblue1' => [152,245,255],'x11:hotpink3' => [205,96,144],
692             'x11:antiquewhite2' => [238,223,204],'x11:darkorange4' => [139,69,0],
693             'x11:cornsilk2' => [238,232,205],'x11:grey93' => [237,237,237],
694             'x11:thistle3' => [205,181,205],'x11:plum2' => [238,174,238],
695             'x11:burlywood2' => [238,197,145],'x11:skyblue4' => [74,112,139],
696             'x11:peachpuff2' => [238,203,173],'x11:grey62' => [158,158,158],
697             'x11:paleturquoise3' => [150,205,205],'x11:lightblue1' => [191,239,255],
698             'x11:mediumpurple' => [147,112,219],'x11:peachpuff3' => [205,175,149],
699             'x11:grey49' => [125,125,125],'x11:grey3' => [8,8,8],
700             'x11:steelblue1' => [99,184,255],'x11:grey73' => [186,186,186],
701             'x11:grey44' => [112,112,112],'x11:palevioletred4' => [139,71,93],
702             'x11:khaki2' => [238,230,133],'x11:gold3' => [205,173,0],
703             'x11:grey47' => [120,120,120],'x11:aliceblue' => [240,248,255],
704             'x11:grey58' => [148,148,148],'x11:darkslategrey4' => [82,139,139],
705             'x11:mediumorchid4' => [122,55,139],'x11:thistle1' => [255,225,255],
706             'x11:mistyrose4' => [139,125,123],'x11:orchid1' => [255,131,250],
707             'x11:hotpink2' => [238,106,167],'x11:azure' => [240,255,255],
708             'x11:azure1' => [240,255,255],'x11:darkred' => [139,0,0],
709             'x11:red4' => [139,0,0],'x11:chartreuse2' => [118,238,0],
710             'x11:slateblue1' => [131,111,255],'x11:grey15' => [38,38,38],
711             'x11:grey71' => [181,181,181],'x11:darkslategrey2' => [141,238,238],
712             'x11:snow3' => [205,201,201],'x11:bisque2' => [238,213,183],
713             'x11:darkslateblue' => [72,61,139],'x11:coral4' => [139,62,47],
714             'x11:grey69' => [176,176,176],'x11:burlywood4' => [139,115,85],
715             'x11:coral3' => [205,91,69],'x11:purple' => [160,32,240],
716             'x11:grey36' => [92,92,92],'x11:grey94' => [240,240,240],
717             'x11:palevioletred2' => [238,121,159],'x11:grey46' => [117,117,117],
718             'x11:palevioletred' => [219,112,147],'x11:mediumslateblue' => [123,104,238],
719             'x11:seagreen1' => [84,255,159],'x11:gainsboro' => [220,220,220],
720             'x11:yellow1' => [255,255,0],'x11:yellow' => [255,255,0],
721             'x11:limegreen' => [50,205,50],'x11:darkgrey' => [169,169,169],
722             'x11:darkmagenta' => [139,0,139],'x11:magenta4' => [139,0,139],
723             'x11:grey59' => [150,150,150],'x11:firebrick2' => [238,44,44],
724             'x11:coral' => [255,127,80],'x11:honeydew' => [240,255,240],
725             'x11:honeydew1' => [240,255,240],'x11:grey86' => [219,219,219],
726             'x11:grey13' => [33,33,33],'x11:purple1' => [155,48,255],
727             'x11:grey82' => [209,209,209],'x11:grey65' => [166,166,166],
728             'x11:grey97' => [247,247,247],'x11:azure4' => [131,139,139],
729             'x11:darkslategrey3' => [121,205,205],'x11:lightcyan3' => [180,205,205],
730             'x11:aquamarine2' => [118,238,198],'x11:grey92' => [235,235,235],
731             'x11:slateblue' => [106,90,205],'x11:darkcyan' => [0,139,139],
732             'x11:cyan4' => [0,139,139],'x11:chartreuse3' => [102,205,0],
733             'x11:moccasin' => [255,228,181],'x11:mediumvioletred' => [199,21,133],
734             'x11:tomato3' => [205,79,57],'x11:grey31' => [79,79,79],
735             'x11:sienna2' => [238,121,66],'x11:grey98' => [250,250,250],
736             'x11:gold4' => [139,117,0],'x11:slateblue3' => [105,89,205],
737             'x11:grey14' => [36,36,36],'x11:honeydew4' => [131,139,131],
738             'x11:grey61' => [156,156,156],'x11:violetred3' => [205,50,120],
739             'x11:grey39' => [99,99,99],'x11:aquamarine4' => [69,139,116],
740             'x11:darkgoldenrod4' => [139,101,8],'x11:mediumpurple1' => [171,130,255],
741             'x11:lightsalmon1' => [255,160,122],'x11:lightsalmon' => [255,160,122],
742             'x11:darkolivegreen3' => [162,205,90],'x11:grey10' => [26,26,26],
743             'x11:khaki3' => [205,198,115],'x11:navajowhite3' => [205,179,139],
744             'x11:lightpink1' => [255,174,185],'x11:grey81' => [207,207,207],
745             'x11:grey45' => [115,115,115],'x11:wheat3' => [205,186,150],
746             'x11:steelblue4' => [54,100,139],'x11:grey48' => [122,122,122],
747             'x11:olivedrab3' => [154,205,50],'x11:yellowgreen' => [154,205,50],
748             'x11:mediumturquoise' => [72,209,204],'x11:palegoldenrod' => [238,232,170],
749             'x11:ivory2' => [238,238,224],'x11:darkolivegreen1' => [202,255,112],
750             'x11:red1' => [255,0,0],'x11:red' => [255,0,0],
751             'x11:lemonchiffon4' => [139,137,112],'x11:lightseagreen' => [32,178,170],
752             'x11:seagreen4' => [46,139,87],'x11:seagreen' => [46,139,87],
753             'x11:ivory' => [255,255,240],'x11:ivory1' => [255,255,240],
754             'x11:linen' => [250,240,230],'x11:grey34' => [87,87,87],
755             'x11:thistle2' => [238,210,238],'x11:tomato' => [255,99,71],
756             'x11:tomato1' => [255,99,71],'x11:slategrey1' => [198,226,255],
757             'x11:orchid3' => [205,105,201],'x11:lightcyan2' => [209,238,238],
758             'x11:grey54' => [138,138,138],'x11:darkolivegreen' => [85,107,47],
759             'x11:lightsteelblue' => [176,196,222],'x11:grey33' => [84,84,84],
760             'x11:chocolate4' => [139,69,19],'x11:saddlebrown' => [139,69,19],
761             'x11:orange3' => [205,133,0],'x11:lightyellow' => [255,255,224],
762             'x11:lightyellow1' => [255,255,224],'x11:grey75' => [191,191,191],
763             'x11:khaki1' => [255,246,143],'x11:thistle' => [216,191,216],
764             'x11:grey79' => [201,201,201],'x11:plum1' => [255,187,255],
765             'x11:paleturquoise4' => [102,139,139],'x11:cyan1' => [0,255,255],
766             'x11:cyan' => [0,255,255],'x11:maroon3' => [205,41,144],
767             'x11:papayawhip' => [255,239,213],'x11:seagreen3' => [67,205,128],
768             'x11:lightgoldenrod4' => [139,129,76],'x11:lightskyblue1' => [176,226,255],
769             'x11:firebrick' => [178,34,34],'x11:grey30' => [77,77,77],
770             'x11:grey26' => [66,66,66],'x11:antiquewhite4' => [139,131,120],
771             'x11:navyblue' => [0,0,128],'x11:navy' => [0,0,128],
772             'x11:grey7' => [18,18,18],'x11:grey5' => [13,13,13],
773             'x11:grey29' => [74,74,74],'x11:turquoise1' => [0,245,255],
774             'x11:darkgoldenrod3' => [205,149,12],'x11:goldenrod4' => [139,105,20],
775             'x11:palevioletred3' => [205,104,137],'x11:lightslategrey' => [119,136,153],
776             'x11:snow2' => [238,233,233],'x11:grey24' => [61,61,61],
777             'x11:slategrey4' => [108,123,139],'x11:grey55' => [140,140,140],
778             'x11:seashell3' => [205,197,191],'x11:deeppink3' => [205,16,118],
779             'x11:lawngreen' => [124,252,0],'x11:darkorchid3' => [154,50,205],
780             'x11:lightcoral' => [240,128,128],'x11:palegreen' => [152,251,152],
781             'x11:grey56' => [143,143,143],'x11:grey23' => [59,59,59],
782             'x11:grey74' => [189,189,189],'x11:azure2' => [224,238,238],
783             'x11:darkseagreen3' => [155,205,155],'x11:grey20' => [51,51,51],
784             'x11:cadetblue2' => [142,229,238],'x11:grey84' => [214,214,214],
785             'x11:cornsilk4' => [139,136,120],'x11:grey38' => [97,97,97],
786             'x11:magenta1' => [255,0,255],'x11:magenta' => [255,0,255],
787             'x11:darkseagreen' => [143,188,143],'x11:aquamarine1' => [127,255,212],
788             'x11:aquamarine' => [127,255,212],'x11:lightblue3' => [154,192,205],
789             'x11:olivedrab2' => [179,238,58],'x11:grey40' => [102,102,102],
790             'x11:peachpuff4' => [139,119,101],'x11:paleturquoise1' => [187,255,255],
791             'x11:darkseagreen1' => [193,255,193],'x11:darkorange3' => [205,102,0],
792             'x11:brown3' => [205,51,51],'x11:grey51' => [130,130,130],
793             'x11:mediumpurple4' => [93,71,139],'x11:lightpink2' => [238,162,173],
794             'x11:cadetblue' => [95,158,160],'x11:lemonchiffon2' => [238,233,191],
795             'x11:green2' => [0,238,0],'x11:azure3' => [193,205,205],
796             'x11:turquoise' => [64,224,208],'x11:brown1' => [255,64,64],
797             'x11:lightsteelblue4' => [110,123,139],'x11:orange2' => [238,154,0],
798             'x11:antiquewhite' => [250,235,215],'x11:wheat2' => [238,216,174],
799             'x11:rosybrown2' => [238,180,180],'x11:lightsteelblue3' => [162,181,205],
800             'x11:grey78' => [199,199,199],'x11:grey21' => [54,54,54],
801             'x11:sandybrown' => [244,164,96],'x11:lavenderblush2' => [238,224,229],
802             'x11:steelblue' => [70,130,180],'x11:grey95' => [242,242,242],
803             'x11:indianred' => [205,92,92],'x11:skyblue1' => [135,206,255],
804             'x11:khaki' => [240,230,140],'x11:orchid4' => [139,71,137],
805             'x11:chocolate1' => [255,127,36],'x11:goldenrod3' => [205,155,29],
806             'x11:sienna4' => [139,71,38],'x11:lightblue' => [173,216,230],
807             'x11:grey88' => [224,224,224],'x11:palegreen4' => [84,139,84],
808             'x11:mediumorchid' => [186,85,211],'x11:blue' => [0,0,255],
809             'x11:blue1' => [0,0,255],'x11:dodgerblue3' => [24,116,205],
810             'x11:indianred1' => [255,106,106],'x11:cyan3' => [0,205,205],
811             'x11:tan1' => [255,165,79],'x11:darkorange' => [255,140,0],
812             'x11:skyblue2' => [126,192,238],'x11:coral1' => [255,114,86],
813             'x11:darkslategrey' => [47,79,79],'x11:burlywood' => [222,184,135],
814             'x11:sienna3' => [205,104,57],'x11:darkgreen' => [0,100,0],
815             'x11:mistyrose3' => [205,183,181],'x11:grey68' => [173,173,173],
816             'x11:grey53' => [135,135,135],'x11:lemonchiffon' => [255,250,205],
817             'x11:lemonchiffon1' => [255,250,205],'x11:palegreen1' => [154,255,154],
818             'x11:grey76' => [194,194,194],'x11:steelblue3' => [79,148,205],
819             'x11:grey11' => [28,28,28],'x11:oldlace' => [253,245,230],
820             'x11:mintcream' => [245,255,250],'x11:firebrick4' => [139,26,26],
821             'x11:lemonchiffon3' => [205,201,165],'x11:olivedrab' => [107,142,35],
822             'x11:honeydew2' => [224,238,224],'x11:deepskyblue2' => [0,178,238],
823             'x11:slateblue2' => [122,103,238],'x11:slategrey2' => [185,211,238],
824             'x11:seagreen2' => [78,238,148],'x11:salmon2' => [238,130,98],
825             'x11:ivory3' => [205,205,193],'x11:mediumorchid1' => [224,102,255],
826             'x11:tan2' => [238,154,73],'x11:springgreen' => [0,255,127],
827             'x11:springgreen1' => [0,255,127],'x11:seashell1' => [255,245,238],
828             'x11:seashell' => [255,245,238],'x11:skyblue3' => [108,166,205],
829             'x11:chartreuse4' => [69,139,0],'x11:burlywood3' => [205,170,125],
830             'x11:plum' => [221,160,221],'x11:ivory4' => [139,139,131],
831             'x11:darkviolet' => [148,0,211],'x11:lightblue2' => [178,223,238],
832             'x11:wheat' => [245,222,179],'x11:darkgoldenrod1' => [255,185,15],
833             'x11:cornflowerblue' => [100,149,237],'x11:purple2' => [145,44,238],
834             'x11:grey6' => [15,15,15],'x11:magenta2' => [238,0,238],
835             'x11:sienna1' => [255,130,71],'x11:darkgoldenrod' => [184,134,11],
836             'x11:forestgreen' => [34,139,34],'x11:navajowhite4' => [139,121,94],
837             'x11:royalblue4' => [39,64,139],'x11:wheat1' => [255,231,186],
838             'x11:lightskyblue4' => [96,123,139],'x11:grey18' => [46,46,46],
839             'x11:orangered4' => [139,37,0],'x11:salmon3' => [205,112,84],
840             'x11:white' => [255,255,255],'x11:grey100' => [255,255,255],
841             'x11:orange4' => [139,90,0],'x11:wheat4' => [139,126,102],
842             'x11:rosybrown1' => [255,193,193],'x11:grey8' => [20,20,20],
843             'x11:lightgoldenrod2' => [238,220,130],'x11:lightskyblue3' => [141,182,205],
844             'x11:violetred' => [208,32,144],'x11:blue4' => [0,0,139],
845             'x11:darkblue' => [0,0,139],'x11:lavenderblush3' => [205,193,197],
846             'x11:thistle4' => [139,123,139],'x11:hotpink1' => [255,110,180],
847             'x11:darkorange2' => [238,118,0],'x11:lightsalmon2' => [238,149,114],
848             'x11:orangered1' => [255,69,0],'x11:orangered' => [255,69,0],
849             'x11:burlywood1' => [255,211,155],'x11:lightgrey' => [211,211,211],
850             'x11:grey22' => [56,56,56],'x11:tomato4' => [139,54,38],
851             'x11:rosybrown3' => [205,155,155],'x11:gold2' => [238,201,0],
852             'x11:beige' => [245,245,220]
853             );
854              
855             # Returns a hash-ref: color-name => RGB triple
856             sub available_names {
857 1     1 1 10 return \%_colors;
858             }
859              
860             # Returns the RGB triple for a name, undef if name not found
861             # The name is normalized before lookup is attempted. Normalization consists
862             # of: lowercasing and elimination of whitespace. Also, "gray" is replaced
863             # with "grey".
864             # If the name is prefixed with a namespace (separated by colon ':'),
865             # only this namespace is searched. If no namespace is specified, then
866             # the lookup occurs first in the global namespace, then in the default
867             # namespace.
868              
869             sub name2rgb {
870 56     56 1 4965 my ( $name ) = @_;
871              
872 56         193 my ( $ns, $core, $norm ) = _normalize_name( $name );
873              
874             # If explicit namespace:
875 56 100       136 if( $ns ne '' ) {
876 18 100       56 if( exists $_colors{ $norm } ) {
877 16         17 return _fmt( @{ $_colors{ $norm } } );
  16         50  
878             } else {
879 2         17 return; # Do not search further if explicit namespace is given
880             }
881             }
882              
883             # No explicit namespace
884 38 100       98 if( exists $_colors{ $core } ) {
885 10         12 return _fmt( @{ $_colors{ $core } } ); # global namespace
  10         31  
886             }
887              
888             # No namespace, but ':' prefix: search global ONLY, but not default ns
889 28 100       63 if( $core ne $norm ) { return; }
  5         37  
890              
891 23         45 $norm = get_default_namespace() . ':' . $core;
892 23 100       68 if( exists $_colors{ $norm } ) {
893 21         21 return _fmt( @{ $_colors{ $norm } } ); # default namespace
  21         64  
894             }
895              
896             # Not found
897 2         10 return;
898             }
899              
900             # Takes a name and an RGB triple. Registers the triple for the given name.
901             # The name will be normalized (lowercased, whitespace eliminated, 'gray'
902             # replaced by 'grey') before assignment is made.
903             # If the name is not prefixed by a namespace, the color will be entered
904             # into the global namespace.
905             # Returns the old value for the name, if the name already exists.
906              
907             sub register_name {
908 8     8 1 3980 my ( $name, $r, $g, $b ) = @_;
909              
910 8         329 my ( $ns, $core, $norm ) = _normalize_name( $name );
911              
912             # If no explicit ns is given, lookup and replace for $core, which is
913             # guaranteed not preceeded by ':'. Otherwise, use fully qualified name.
914 8 100       29 my $crr = ( $ns eq '' ) ? $core : $norm;
915              
916 8 100       101 if( exists $_colors{ $crr } ) {
917 3         6 my $old = $_colors{ $crr };
918 3         8 $_colors{ $crr } = [ $r, $g, $b ];
919              
920 3         10 return _fmt( @$old );
921             }
922              
923 5         20 $_colors{ $crr } = [ $r, $g, $b ];
924              
925 5         17 return;
926             }
927              
928             sub _normalize_name {
929 64     64   92 my ( $name ) = @_;
930              
931 64         216 $name = lc( $name ); # Lowercase
932 64         217 $name =~ s/\s//g; # Eliminate whitespace
933 64         99 $name =~ s/gray/grey/; # gray -> grey
934              
935 64         111 my ( $ns, $core ) = ( '', $name );
936 64 100       360 if( $name =~ /:/ ) {
937 34         115 ( $ns, $core ) = split ':', $name;
938             }
939              
940 64         347 return ( $ns, $core, $name );
941             }
942              
943             # Sets the default namespace. Returns the previous value.
944             # Giving an empty string as argument makes the global namespace the default.
945             # Note that the global namespace is initially EMPTY!
946              
947             sub set_default_namespace {
948 1     1 1 8 my $old = $_default_namespace;
949 1         4 $_default_namespace = $_[0];
950 1         7 return $old;
951             }
952              
953             sub get_default_namespace {
954 25     25 1 16833 return $_default_namespace;
955             }
956              
957             } # end BEGIN (Names)
958              
959              
960              
961             1;
962             __END__