SVGPDF/Colour.pm

filter: Statementstmt 65.9% 60 / 91 Branchbran 35.2% 12 / 34 Conditioncond 22.2% 2 / 9 MC/DCmcdc 0.0% 0 / 6 Subroutinesub 53.3% 8 / 15 Podpod 0.0% 0 / 8 totaltotal 50.3% 82 / 163 CC 24 SCAR 27.1
CC
24
Cov
55%
_parse
33.0
parse
6.0
_check
4.9
CRAP 15.1
« SVGPDF/Tspan.pm ↑ Summary SVGPDF/Rect.pm »
1 #! perl
2  
3 3 ▶use v5.26;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use Object::Pad;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 class SVGPDF::Colour;
8  
9 3 field $colour :accessor :param = undef;
10 0 ▶field $red :accessor :param = undef;
Subroutine colour: not called CC=2 SCAR=14.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $colour = shift() 0 0 0
11 0 ▶field $green :accessor :param = undef;
Subroutine red: not called CC=2 SCAR=14.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $red = shift() 0 0 0
12 0 ▶field $blue :accessor :param = undef;
Subroutine green: not called CC=2 SCAR=14.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $green = shift() 0 0 0
13 0 ▶field $alpha :accessor :param = undef;
Subroutine blue: not called CC=2 SCAR=14.0
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $blue = shift() 0 0 0
14  
15 0 ▶my @colours = qw( red green blue alpha );
Subroutine alpha: not called CC=2 SCAR=11.6
Pod undocumented
Branch: 1 branch
BranchTrueFalseTotal
@_ and $alpha = shift() 0 0 0
16  
17 3 ▶use constant GREY => 128;
Subroutine BEGIN: called CC=1 SCAR=0.0
18  
19 # The official 147 SVG coulr names.
20 3 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 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
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 3 };
183  
184 317 ▶method _parse( $col ) {
Subroutine _parse: called CC=15 SCAR=35.0
185 317 $col =~ s/\s+//g;
186 317 $col = lc($col);
187 317 my ( $r, $g, $b, $a ) = ( GREY, GREY, GREY, GREY );
188 317 ▶ if ( $col =~ /^rgb\((.+?),(.+?),(.+?)\)$/ ) {
Branch: 5 branches
BranchTrueFalseTotal
if ($col =~ /^rgb\((.+?),(.+?),(.+?)\)$/u) { } 14 303 317
elsif ($col =~ /^rgba\((.+?),(.+?),(.+?),(.+?)\)$/u) { } 0 303 303
elsif ($col =~ /^\#([a-f0-9]+)$/u) { } 158 145 303
elsif (defined $colours{$col}) { } 145 0 145
elsif ($col =~ /^url\(/u) { } 0 0 0
189 14 ( $r, $g, $b, $a ) = ( $1, $2, $3, 0 );
190 }
191 elsif ( $col =~ /^rgba\((.+?),(.+?),(.+?),(.+?)\)$/ ) {
192 0 ( $r, $g, $b, $a ) = ( $1, $2, $3, $4 );
193 }
194 elsif ( $col =~ /^\#([a-f0-9]+)$/ ) {
195 158 ▶ if ( length($1) == 6 || length($1) == 8 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (length $1 == 6 or length $1 == 8) { } 158 0 158
elsif (length $1 == 3 or length $1 == 4) { } 0 0 0
Condition33%1/3
length $1 == 6 or length $1 == 8
l!l&&r!l&&!r
15800
MC/DC0%0/2
length $1 == 6 or length $1 == 8
length $1 == 6 length $1 == 8
Truth table33%1/3
A or B
A= length $1 == 6B= length $1 == 8
ABresult
1X1
011
000
196 # HEX: RRGGBB of RRGGBBAA.
197 158 $r = hex(substr( $1, 0, 2 ));
198 158 $g = hex(substr( $1, 2, 2 ));
199 158 $b = hex(substr( $1, 4, 2 ));
200 158 ▶ $a = hex(substr( $1, 6, 2 )) if length($1) == 8;
Branch: 1 branch
BranchTrueFalseTotal
if length $1 == 8 0 158 158
201 }
202 ▶ elsif ( length($1) == 3 || length($1) == 4 ) {
Condition0%0/3
length $1 == 3 or length $1 == 4
l!l&&r!l&&!r
000
MC/DC0%0/2
length $1 == 3 or length $1 == 4
length $1 == 3 length $1 == 4
Truth table0%0/3
A or B
A= length $1 == 3B= length $1 == 4
ABresult
1X1
011
000
203 # Shorthand HEX: RGB => RRGGBB.
204 0 $r = hex(substr( $1, 0, 1 ).substr( $1, 0, 1 ));
205 0 $g = hex(substr( $1, 1, 1 ).substr( $1, 1, 1 ));
206 0 $b = hex(substr( $1, 2, 1 ).substr( $1, 2, 1 ));
207 0 ▶ $a = hex(substr( $1, 3, 1 ).substr( $1, 3, 1 )) if length($1) == 4;
Branch: 1 branch
BranchTrueFalseTotal
if length $1 == 4 0 0 0
208 }
209 }
210 elsif ( defined $colours{$col} ) {
211 # Known. Parse HEX.
212 145 return $self->_parse($colours{$col});
213 }
214 elsif ( $col =~ /^url\(/ ) {
215 0 return $self->_parse($colours{"grey"});
216 }
217 else {
218 0 carp("Illegal colour \"$col\"");
219 }
220 172 my $i = 0;
221 172 for ( $r, $g, $b, $a ) {
222 688 ▶ if ( /^(\d+(?:\.\d+)?)\%$/ ) { # 98.27%
Branch: 2 branches
BranchTrueFalseTotal
if (/^(\d+(?:\.\d+)?)\%$/u) { } 6 682 688
elsif (/^\d+$/u) { } 682 0 682
223 6 $_ = $1 / 100 * 255;
224 }
225 elsif ( /^\d+$/ ) { # 124
226 # ok
227 }
228 else {
229 0 carp("Illegal colour value $colours[$i]: $_");
230 0 $_ = GREY;
231 }
232 688 $i++;
233 }
234 172 return ( $r, $g, $b, $a );
235 3 }
236  
237 178 ▶method _check() {
Subroutine _check: called CC=4 SCAR=15.9
238 178 my $i = 0;
239 178 for ( $red, $green, $blue, $alpha ) {
240 712 ▶ next if $_ >= 0 && $_ <= 255;
Branch: 1 branch
BranchTrueFalseTotal
if $_ >= 0 and $_ <= 255 712 0 712
Condition33%1/3
$_ >= 0 and $_ <= 255
!ll&&!rl&&r
00712
MC/DC0%0/2
$_ >= 0 and $_ <= 255
$_ >= 0 $_ <= 255
Truth table33%1/3
A and B
A= $_ >= 0B= $_ <= 255
ABresult
0X0
100
111
241 0 carp("Illegal colour value $colours[$i]: $_");
242 0 $_ = GREY,
243 }
244 3 }
245  
246 0 ▶method parse( $col = $colour ) {
Subroutine parse: not called CC=2 SCAR=17.9
Pod undocumented
247 0 ( $red, $green, $blue, $alpha ) = _parse($col);
248 0 $self->_check;
249 3 }
250  
251 178 ▶method rgb() {
Subroutine rgb: called CC=1 SCAR=0.0
Pod undocumented
252 178 sprintf( "#%02X%02X%02X", $red, $green, $blue );
253 3 }
254  
255 0 ▶method rgba() {
Subroutine rgba: not called CC=1 SCAR=6.9
Pod undocumented
256 0 sprintf( "#%02X%02X%02X%02X", $red, $green, $blue, $alpha );
257 3 }
258  
259 3 1;
« SVGPDF/Tspan.pm ↑ Summary SVGPDF/Rect.pm »