blib/lib/SVG/TrafficLight.pm

filter: Statementstmt 100% 54 / 54 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 14 / 14 Podpod 66.6% 2 / 3 totaltotal 98.5% 70 / 71 CC 3 SCAR 2.2
CC
3
Cov
100%
BUILD
2.0
draw_a_lightset_at
2.0
BEGIN
1.0
CRAP 1.2
↑ Summary
1 =head1 NAME
2  
3 SVG::TrafficLight - Perl extension to produce SVG diagrams of traffic lights.
4  
5 =head1 DESCRIPTION
6  
7 Perl extension to produce SVG diagrams of traffic lights.
8  
9 =head1 SYNOPSIS
10  
11 use SVG::TrafficLight;
12  
13 my $tl = SVG::TrafficLight->new; # default image
14 print $some_file_handle $tl->xmlify;
15  
16 my $tl2 = SVG::TrafficLight->new({
17 sequence => [
18 { red => 1, amber => 1, green => 1 }, # all lights on
19 { red => 0, amber => 0, green => 0 }, # all lights off
20 ],
21 });
22  
23 =cut
24  
25 package SVG::TrafficLight;
26  
27 3 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
28 3 ▶use SVG;
Subroutine BEGIN: called CC=1 SCAR=0.0
29  
30 3 our $VERSION = '0.1.4';
31  
32 =head1 ATTRIBUTES AND METHODS
33  
34 =head2 radius()
35  
36 Returns the radius of the circles used to draw the traffic lights. The default
37 is 50, but this can be altered when creating the object.
38  
39 my $tl = SVG::TrafficLight->new({ radius => 1000 });
40  
41 =cut
42  
43 3 has radius => (
44 is => 'ro',
45 isa => 'Num',
46 default => 50,
47 );
48  
49 =head2 diameter
50  
51 Returns the diameter of the circles used to draw the traffic lights. This is
52 just twice the radius. The default is 100. Change it by setting a different
53 radius.
54  
55 =cut
56  
57 18 ▶sub diameter {
Subroutine diameter: called CC=1 SCAR=0.0
Pod documented
58 18 my $self = shift;
59  
60 18 return $self->radius * 2;
61 }
62  
63 =head2 padding
64  
65 Returns a value which is used to pad various shapes in the image.
66  
67 =over 4
68  
69 =item *
70  
71 The padding between the edge of the image and the traffic light block.
72  
73 =item *
74  
75 The padding between two traffic light blocks in the sequence.
76  
77 =item *
78  
79 The padding between the edge of a traffic light block and the lights
80 contained within it.
81  
82 =item *
83  
84 The padding between two vertically stacked traffic lights within a block.
85  
86 =back
87  
88 The default value is half the radius of a traffic light circle. This can
89 be set when creating the object;
90  
91 my $tl = SVG::TrafficLight->new({ padding => 100 });
92  
93 =cut
94  
95 3 has padding => (
96 is => 'ro',
97 isa => 'Num',
98 lazy_build => 1,
99 );
100  
101 1 ▶sub _build_padding {
Subroutine _build_padding: called CC=1 SCAR=0.0
102 1 return shift->radius * .5;
103 }
104  
105 =head2 light_width
106  
107 Returns the width of a traffic light. This is the diameter of a light plus
108 twice the padding (one padding for each side of the light).
109  
110 =cut
111  
112 3 has light_width => (
113 is => 'ro',
114 isa => 'Num',
115 lazy_build => 1,
116 );
117  
118 1 ▶sub _build_light_width {
Subroutine _build_light_width: called CC=1 SCAR=0.0
119 1 my $self = shift;
120  
121 # A light is a diameter plus two paddings
122 1 return $self->diameter + (2 * $self->padding);
123 }
124  
125 =head2 light_height
126  
127 Returns the height of a traffic light. This is the diameter of three lights
128 plus four times the padding (one at the top, one at the bottom and two between
129 lights in the block).
130  
131 =cut
132  
133 3 has light_height => (
134 is => 'ro',
135 isa => 'Num',
136 lazy_build => 1,
137 );
138  
139 1 ▶sub _build_light_height {
Subroutine _build_light_height: called CC=1 SCAR=0.0
140 1 my $self = shift;
141  
142 # Height is three diameters + four paddings
143 1 return (3 * $self->diameter) + (4 * $self->padding);
144 }
145  
146 =head2 width
147  
148 Returns the width of the SVG document.
149  
150 This is the width of a traffic light block multiplied by the number of blocks
151 in the sequence plus padding on the left and right and padding between each
152 pair of lights.
153  
154 =cut
155  
156 3 has width => (
157 is => 'ro',
158 isa => 'Num',
159 lazy_build => 1,
160 );
161  
162 1 ▶sub _build_width {
Subroutine _build_width: called CC=1 SCAR=0.0
163 1 my $self = shift;
164  
165 1 my $count_lights = scalar @{ $self->sequence };
166  
167 # One light is 2 * radius
168 # + 2 * padding
169 1 my $one_light = $self->light_width;
170  
171 # Multiply by the number of lights
172 1 my $lights = $count_lights * $one_light;
173  
174 # Add padding at the edges and between the lights
175 1 return ($count_lights + 1) * $self->padding + $lights;
176 }
177  
178 =head2 height
179  
180 Returns the height of the SVG document.
181  
182 This is the height of a traffic light block plus padding at the top and
183 bottom.
184  
185 =cut
186  
187 3 has height => (
188 is => 'ro',
189 isa => 'Num',
190 lazy_build => 1,
191 );
192  
193 1 ▶sub _build_height {
Subroutine _build_height: called CC=1 SCAR=0.0
194 1 my $self = shift;
195  
196 # Height of a light bank + two lots of padding
197 1 return $self->light_height + (2 * $self->padding);
198 }
199  
200 =head2 corner_radius
201  
202 Returns the radius of the circles used to curve the corners of a traffic
203 light block. The default is 20. This can be changed when creating the object.
204  
205 my $tl = SVG::TrafficLight->new({ corner_radius => 500 });
206  
207 =cut
208  
209 3 has corner_radius => (
210 is => 'ro',
211 isa => 'Num',
212 default => 20,
213 );
214  
215 =head2 svg
216  
217 This is the SVG object that used to create the SVG document. A standard
218 object is created for you. It's possible to pass in your own when
219 creating the object.
220  
221 my $tl = SVG::TrafficLight->new({
222 svg => SVG->new(width => $width, height => $height,
223 });
224  
225 =cut
226  
227 3 has svg => (
228 is => 'ro',
229 isa => 'SVG',
230 lazy_build => 1,
231 handles => [ qw(rect circle xmlify) ],
232 );
233  
234 1 ▶sub _build_svg {
Subroutine _build_svg: called CC=1 SCAR=0.0
235 1 my $self = shift;
236  
237 1 return SVG->new(
238 width => $self->width,
239 height => $self->height,
240 );
241 }
242  
243 =head2 colours
244  
245 This defines the colours used to draw the traffic lights. The value must be
246 a reference to a hash. The hash must contain three keys - C<red>, C<amber>,
247 and C<green>. The values are references to two-element arrays. The first
248 element in each array is the colour used when the light is off and the
249 second is the colour used when the light is on.
250  
251 The values of the colours can be anything that is recognised as a colour in
252 SVG. These are either colour names (e.g. 'red') or RGB definitions (e.g.
253 'rgb(255,0,0,)'.
254  
255 The default values can be overridden when creating the object.
256  
257 my $tl = SVG::TrafficLight->new({
258 colours => {
259 red => [ ... ],
260 amber => [ ... ],
261 green => [ ... ],
262 }.
263 });
264  
265 =cut
266  
267 has colours => (
268 is => 'ro',
269 isa => 'HashRef',
270 1 ▶ default => sub { {
Subroutine __ANON__: called CC=1 SCAR=0.0
271 1 red => ['rgb(63,0,0)', 'red'],
272 amber => ['rgb(59,29,0)', 'orange'],
273 green => ['rgb(0,63,0)', 'green'],
274 } },
275 3 );
276  
277 =head2 sequence
278  
279 Defines a sequence of traffic lights to display. This is an array reference.
280 Each element in the array is a hash reference which defines which of the
281 three lights are on or off.
282  
283 The default sequence demonstates the full standard British traffic light
284 sequence of green, amber, red, red and amber, green. This can be changed
285 when creating the object. For example, here is how to reproduct the green,
286 amber, red, green sequence that is used in many countries.
287  
288 my $tl = SVG::TrafficLight->new({
289 sequence => [
290 { red => 0, amber => 0, green => 1 },
291 { red => 0, amber => 1, green => 0 },
292 { red => 1, amber => 0, green => 0 },
293 { red => 0, amber => 0, green => 1 },
294 ],
295 });
296  
297 You don't need to reproduce sequences that are seen in the real world, The
298 following code, for example, gives a sequence consisting of two steps - one
299 where all lights are off followed by one where all lights are on.
300  
301 my $tl = SVG::TrafficLight->new({
302 sequence => [
303 { red => 0, amber => 0, green => 0 },
304 { red => 1, amber => 1, green => 1 },
305 ],
306 });
307  
308 =cut
309  
310 has sequence => (
311 is => 'ro',
312 isa => 'ArrayRef',
313 1 ▶ default => sub { [{
Subroutine __ANON__: called CC=1 SCAR=0.0
314 1 red => 0,
315 amber => 0,
316 green => 1,
317 }, {
318 red => 0,
319 amber => 1,
320 green => 0,
321 }, {
322 red => 1,
323 amber => 0,
324 green => 0,
325 }, {
326 red => 1,
327 amber => 1,
328 green => 0,
329 }, {
330 red => 0,
331 amber => 0,
332 green => 1,
333 }] },
334 3 );
335  
336 1 ▶sub BUILD {
Subroutine BUILD: called CC=2 SCAR=6.9
Pod undocumented
337 1 my $self = shift;
338  
339 1 for my $i (0 .. $#{$self->sequence}) {
340 5 my $light_set_x = ($i * ($self->light_width + $self->padding))
341 + $self->padding;
342  
343 5 $self->draw_a_lightset_at($light_set_x, $self->padding,
344 $self->sequence->[$i]);
345 }
346 }
347  
348 =head2 draw_a_lightset_at($x, $y, \%lights)
349  
350 Draws a set of three lights at a given position on the canvas.
351  
352 Takes three mandatory parameters:
353  
354 =over 4
355  
356 =item $x
357  
358 The x co-ordinate of the top-left corner of the light set.
359  
360 =item $y
361  
362 The co-ordinate of the top left corner of the light set.
363  
364 =item \%lights
365  
366 A reference to a hash indicating which lights should be turned on or off.
367 The keys in the has should be the names of the lights in the light set (red,
368 amber and green) and the associated values should be a 1 (to indicate that the
369 light is on) or a 0 (to indicate that the light is off).
370  
371 =back
372  
373 =cut
374  
375 5 ▶sub draw_a_lightset_at {
Subroutine draw_a_lightset_at: called CC=2 SCAR=6.9
Pod documented
376 5 my $self = shift;
377 5 my ($x, $y, $lights) = @_;
378  
379 5 $self->rect(
380 x => $x,
381 y => $y,
382 width => $self->light_width,
383 height => $self->light_height,
384 fill => 'black',
385 rx => $self->corner_radius,
386 ry => $self->corner_radius,
387 );
388  
389 5 my $light = 0;
390 5 for my $l (qw[red amber green]) {
391 15 my $fill = $self->colours->{$l}[$lights->{$l}];
392  
393 15 $self->circle(
394 cx => $x + $self->padding + $self->radius,
395 cy => $y + $self->padding + $self->radius
396 + $light * ($self->diameter + $self->padding),
397 r => $self->radius,
398 fill => $fill,
399 );
400 15 ++$light;
401 }
402 }
403  
404 3 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
405 3 __PACKAGE__->meta->make_immutable;
406  
407 =head1 AUTHOR
408  
409 Dave Cross E<lt>dave@perlhacks.comE<gt>
410  
411 =head1 COPYRIGHT
412  
413 Copyright (c) 2017 Magnum Solutions Ltd. All rights reserved.
414  
415 This module is free software; you can redistribute it and/or
416 modify it under the same terms as Perl itself.
417  
418 =head1 SEE ALSO
419  
420 L<SVG>
421  
422 =cut
423  
424 3 1;
↑ Summary