File Coverage

lib/ChordPro/lib/SVGPDF/Colour.pm
Criterion Covered Total %
statement 14 76 18.4
branch 0 34 0.0
condition 0 9 0.0
subroutine 5 15 33.3
pod 0 8 0.0
total 19 142 13.3


line stmt bran cond sub pod time code
1             #! perl
2              
3 10     10   174 use v5.26;
  10         65  
4 10     10   67 use Object::Pad;
  10         41  
  10         102  
5 10     10   1621 use utf8;
  10         27  
  10         89  
6              
7             class SVGPDF::Colour;
8              
9             field $colour :accessor :param = undef;
10 0 0   0 0   field $red :accessor :param = undef;
  0            
11 0 0   0 0   field $green :accessor :param = undef;
  0            
12 0 0   0 0   field $blue :accessor :param = undef;
  0            
13 0 0   0 0   field $alpha :accessor :param = undef;
  0            
14              
15 0 0   0 0   my @colours = qw( red green blue alpha );
  0            
16              
17 10     10   9369 use constant GREY => 128;
  10         27  
  10         7977  
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 10     10   99 use Carp;
  10         29  
  10         34359  
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 0     0     method _parse( $col ) {
  0            
  0            
  0            
185 0           $col =~ s/\s+//g;
186 0           $col = lc($col);
187 0           my ( $r, $g, $b, $a ) = ( GREY, GREY, GREY, GREY );
188 0 0         if ( $col =~ /^rgb\((.+?),(.+?),(.+?)\)$/ ) {
    0          
    0          
    0          
    0          
189 0           ( $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 0 0 0       if ( length($1) == 6 || length($1) == 8 ) {
    0 0        
196             # HEX: RRGGBB of RRGGBBAA.
197 0           $r = hex(substr( $1, 0, 2 ));
198 0           $g = hex(substr( $1, 2, 2 ));
199 0           $b = hex(substr( $1, 4, 2 ));
200 0 0         $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           $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 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 0           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 0           my $i = 0;
221 0           for ( $r, $g, $b, $a ) {
222 0 0         if ( /^(\d+(?:\.\d+)?)\%$/ ) { # 98.27%
    0          
223 0           $_ = $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 0           $i++;
233             }
234 0           return ( $r, $g, $b, $a );
235             }
236              
237 0     0     method _check() {
  0            
  0            
238 0           my $i = 0;
239 0           for ( $red, $green, $blue, $alpha ) {
240 0 0 0       next if $_ >= 0 && $_ <= 255;
241 0           carp("Illegal colour value $colours[$i]: $_");
242 0           $_ = GREY,
243             }
244             }
245              
246 0     0 0   method parse( $col = $colour ) {
  0            
  0            
  0            
247 0           ( $red, $green, $blue, $alpha ) = _parse($col);
248 0           $self->_check;
249             }
250              
251 0     0 0   method rgb() {
  0            
  0            
252 0           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;