File Coverage

blib/lib/SVGPDF/Colour.pm
Criterion Covered Total %
statement 45 76 59.2
branch 12 34 35.2
condition 2 9 22.2
subroutine 8 15 53.3
pod 0 8 0.0
total 67 142 47.1


line stmt bran cond sub pod time code
1             #! perl
2              
3 3     3   169691 use v5.26;
  3         12  
4 3     3   1517 use Object::Pad;
  3         21559  
  3         20  
5 3     3   1326 use utf8;
  3         549  
  3         18  
6              
7             class SVGPDF::Colour;
8              
9             field $colour :accessor :param = undef;
10 0 0   0 0 0 field $red :accessor :param = undef;
  0         0  
11 0 0   0 0 0 field $green :accessor :param = undef;
  0         0  
12 0 0   0 0 0 field $blue :accessor :param = undef;
  0         0  
13 0 0   0 0 0 field $alpha :accessor :param = undef;
  0         0  
14              
15 0 0   0 0 0 my @colours = qw( red green blue alpha );
  0         0  
16              
17 3     3   2519 use constant GREY => 128;
  3         7  
  3         2127  
18              
19             # The official 147 SVG coulr names.
20             my %colours =
21             ( aliceblue => "#F0F8FF",
22             antiquewhite => "#FAEBD7",
23             aqua => "#00FFFF",
24             aquamarine => "#7FFFD4",
25             azure => "#F0FFFF",
26             beige => "#F5F5DC",
27             bisque => "#FFE4C4",
28             black => "#000000",
29             blanchedalmond => "#FFEBCD",
30             blue => "#0000FF",
31             blueviolet => "#8A2BE2",
32             brown => "#A52A2A",
33             burlywood => "#DEB887",
34             cadetblue => "#5F9EA0",
35             chartreuse => "#7FFF00",
36             chocolate => "#D2691E",
37             coral => "#FF7F50",
38             cornflowerblue => "#6495ED",
39             cornsilk => "#FFF8DC",
40             crimson => "#DC143C",
41             cyan => "#00FFFF",
42             darkblue => "#00008B",
43             darkcyan => "#008B8B",
44             darkgoldenrod => "#B8860B",
45             darkgray => "#A9A9A9",
46             darkgreen => "#006400",
47             darkgrey => "#A9A9A9",
48             darkkhaki => "#BDB76B",
49             darkmagenta => "#8B008B",
50             darkolivegreen => "#556B2F",
51             darkorange => "#FF8C00",
52             darkorchid => "#9932CC",
53             darkred => "#8B0000",
54             darksalmon => "#E9967A",
55             darkseagreen => "#8FBC8F",
56             darkslateblue => "#483D8B",
57             darkslategray => "#2F4F4F",
58             darkslategrey => "#2F4F4F",
59             darkturquoise => "#00CED1",
60             darkviolet => "#9400D3",
61             deeppink => "#FF1493",
62             deepskyblue => "#00BFFF",
63             dimgray => "#696969",
64             dimgrey => "#696969",
65             dodgerblue => "#1E90FF",
66             firebrick => "#B22222",
67             floralwhite => "#FFFAF0",
68             forestgreen => "#228B22",
69             fuchsia => "#FF00FF",
70             gainsboro => "#DCDCDC",
71             ghostwhite => "#F8F8FF",
72             gold => "#FFD700",
73             goldenrod => "#DAA520",
74             gray => "#808080",
75             green => "#008000",
76             greenyellow => "#ADFF2F",
77             grey => "#808080",
78             honeydew => "#F0FFF0",
79             hotpink => "#FF69B4",
80             indianred => "#CD5C5C",
81             indigo => "#4B0082",
82             ivory => "#FFFFF0",
83             khaki => "#F0E68C",
84             lavender => "#E6E6FA",
85             lavenderblush => "#FFF0F5",
86             lawngreen => "#7CFC00",
87             lemonchiffon => "#FFFACD",
88             lightblue => "#ADD8E6",
89             lightcoral => "#F08080",
90             lightcyan => "#E0FFFF",
91             lightgoldenrodyellow => "#FAFAD2",
92             lightgray => "#D3D3D3",
93             lightgreen => "#90EE90",
94             lightgrey => "#D3D3D3",
95             lightpink => "#FFB6C1",
96             lightsalmon => "#FFA07A",
97             lightseagreen => "#20B2AA",
98             lightskyblue => "#87CEFA",
99             lightslategray => "#778899",
100             lightslategrey => "#778899",
101             lightsteelblue => "#B0C4DE",
102             lightyellow => "#FFFFE0",
103             lime => "#00FF00",
104             limegreen => "#32CD32",
105             linen => "#FAF0E6",
106             magenta => "#FF00FF",
107             maroon => "#800000",
108             mediumaquamarine => "#66CDAA",
109             mediumblue => "#0000CD",
110             mediumorchid => "#BA55D3",
111             mediumpurple => "#9370DB",
112             mediumseagreen => "#3CB371",
113             mediumslateblue => "#7B68EE",
114             mediumspringgreen => "#00FA9A",
115             mediumturquoise => "#48D1CC",
116             mediumvioletred => "#C71585",
117             midnightblue => "#191970",
118             mintcream => "#F5FFFA",
119             mistyrose => "#FFE4E1",
120             moccasin => "#FFE4B5",
121             navajowhite => "#FFDEAD",
122             navy => "#000080",
123             oldlace => "#FDF5E6",
124             olive => "#808000",
125             olivedrab => "#6B8E23",
126             orange => "#FFA500",
127             orangered => "#FF4500",
128             orchid => "#DA70D6",
129             palegoldenrod => "#EEE8AA",
130             palegreen => "#98FB98",
131             paleturquoise => "#AFEEEE",
132             palevioletred => "#DB7093",
133             papayawhip => "#FFEFD5",
134             peachpuff => "#FFDAB9",
135             peru => "#CD853F",
136             pink => "#FFC0CB",
137             plum => "#DDA0DD",
138             powderblue => "#B0E0E6",
139             purple => "#800080",
140             red => "#FF0000",
141             rosybrown => "#BC8F8F",
142             royalblue => "#4169E1",
143             saddlebrown => "#8B4513",
144             salmon => "#FA8072",
145             sandybrown => "#F4A460",
146             seagreen => "#2E8B57",
147             seashell => "#FFF5EE",
148             sienna => "#A0522D",
149             silver => "#C0C0C0",
150             skyblue => "#87CEEB",
151             slateblue => "#6A5ACD",
152             slategray => "#708090",
153             slategrey => "#708090",
154             snow => "#FFFAFA",
155             springgreen => "#00FF7F",
156             steelblue => "#4682B4",
157             tan => "#D2B48C",
158             teal => "#008080",
159             thistle => "#D8BFD8",
160             tomato => "#FF6347",
161             turquoise => "#40E0D0",
162             violet => "#EE82EE",
163             wheat => "#F5DEB3",
164             white => "#FFFFFF",
165             whitesmoke => "#F5F5F5",
166             yellow => "#FFFF00",
167             yellowgreen => "#9ACD32",
168             );
169              
170 3     3   21 use Carp;
  3         41  
  3         9948  
171              
172             ADJUST {
173             my ( $r, $g, $b, $a );
174             if ( defined $colour ) {
175             ( $r, $g, $b, $a ) = $self->_parse($colour);
176             }
177             $red //= $r // 0;
178             $green //= $g // 0;
179             $blue //= $b // 0;
180             $alpha //= $a // 0;
181             $self->_check();
182             };
183              
184 265     265   379 method _parse( $col ) {
  265         514  
  265         469  
  265         341  
185 265         736 $col =~ s/\s+//g;
186 265         595 $col = lc($col);
187 265         565 my ( $r, $g, $b, $a ) = ( GREY, GREY, GREY, GREY );
188 265 100       1430 if ( $col =~ /^rgb\((.+?),(.+?),(.+?)\)$/ ) {
    50          
    100          
    50          
    0          
189 10         54 ( $r, $g, $b, $a ) = ( $1, $2, $3, 0 );
190             }
191             elsif ( $col =~ /^rgba\((.+?),(.+?),(.+?),(.+?)\)$/ ) {
192 0         0 ( $r, $g, $b, $a ) = ( $1, $2, $3, $4 );
193             }
194             elsif ( $col =~ /^\#([a-f0-9]+)$/ ) {
195 134 50 33     553 if ( length($1) == 6 || length($1) == 8 ) {
    0 0        
196             # HEX: RRGGBB of RRGGBBAA.
197 134         381 $r = hex(substr( $1, 0, 2 ));
198 134         254 $g = hex(substr( $1, 2, 2 ));
199 134         244 $b = hex(substr( $1, 4, 2 ));
200 134 50       316 $a = hex(substr( $1, 6, 2 )) if length($1) == 8;
201             }
202             elsif ( length($1) == 3 || length($1) == 4 ) {
203             # Shorthand HEX: RGB => RRGGBB.
204 0         0 $r = hex(substr( $1, 0, 1 ).substr( $1, 0, 1 ));
205 0         0 $g = hex(substr( $1, 1, 1 ).substr( $1, 1, 1 ));
206 0         0 $b = hex(substr( $1, 2, 1 ).substr( $1, 2, 1 ));
207 0 0       0 $a = hex(substr( $1, 3, 1 ).substr( $1, 3, 1 )) if length($1) == 4;
208             }
209             }
210             elsif ( defined $colours{$col} ) {
211             # Known. Parse HEX.
212 121         352 return $self->_parse($colours{$col});
213             }
214             elsif ( $col =~ /^url\(/ ) {
215 0         0 return $self->_parse($colours{"grey"});
216             }
217             else {
218 0         0 carp("Illegal colour \"$col\"");
219             }
220 144         271 my $i = 0;
221 144         401 for ( $r, $g, $b, $a ) {
222 576 100       2110 if ( /^(\d+(?:\.\d+)?)\%$/ ) { # 98.27%
    50          
223 6         31 $_ = $1 / 100 * 255;
224             }
225             elsif ( /^\d+$/ ) { # 124
226             # ok
227             }
228             else {
229 0         0 carp("Illegal colour value $colours[$i]: $_");
230 0         0 $_ = GREY;
231             }
232 576         1022 $i++;
233             }
234 144         827 return ( $r, $g, $b, $a );
235             }
236              
237 150     150   282 method _check() {
  150         358  
  150         202  
238 150         226 my $i = 0;
239 150         368 for ( $red, $green, $blue, $alpha ) {
240 600 50 33     2668 next if $_ >= 0 && $_ <= 255;
241 0         0 carp("Illegal colour value $colours[$i]: $_");
242 0         0 $_ = GREY,
243             }
244             }
245              
246 0     0 0 0 method parse( $col = $colour ) {
  0         0  
  0         0  
  0         0  
247 0         0 ( $red, $green, $blue, $alpha ) = _parse($col);
248 0         0 $self->_check;
249             }
250              
251 150     150 0 235 method rgb() {
  150         335  
  150         239  
252 150         984 sprintf( "#%02X%02X%02X", $red, $green, $blue );
253             }
254              
255 0     0 0   method rgba() {
  0            
  0            
256 0           sprintf( "#%02X%02X%02X%02X", $red, $green, $blue, $alpha );
257             }
258              
259             1;