Dave Cross

NAME

SVG::TrafficLight - Perl extension to produce SVG diagrams of traffic lights.

DESCRIPTION

Perl extension to produce SVG diagrams of traffic lights.

SYNOPSIS

    use SVG::TrafficLight;

    my $tl = SVG::TrafficLight->new; # default image
    print $some_file_handle $tl->xmlify;

    my $tl2 = SVG::TrafficLight->new({
      sequence => [
        { red => 1, amber => 1, green => 1 }, # all lights on
        { red => 0, amber => 0, green => 0 }, # all lights off
      ],
    });

ATTRIBUTES AND METHODS

radius()

Returns the radius of the circles used to draw the traffic lights. The default is 50, but this can be altered when creating the object.

    my $tl = SVG::TrafficLight->new({ radius => 1000 });

diameter

Returns the diameter of the circles used to draw the traffic lights. This is just twice the radius. The default is 100. Change it by setting a different radius.

padding

Returns a value which is used to pad various shapes in the image.

  • The padding between the edge of the image and the traffic light block.

  • The padding between two traffic light blocks in the sequence.

  • The padding between the edge of a traffic light block and the lights contained within it.

  • The padding between two vertically stacked traffic lights within a block.

The default value is half the radius of a traffic light circle. This can be set when creating the object;

    my $tl = SVG::TrafficLight->new({ padding => 100 });

light_width

Returns the width of a traffic light. This is the diameter of a light plus twice the padding (one padding for each side of the light).

light_height

Returns the height of a traffic light. This is the diameter of three lights plus four times the padding (one at the top, one at the bottom and two between lights in the block).

width

Returns the width of the SVG document.

This is the width of a traffic light block multiplied by the number of blocks in the sequence plus padding on the left and right and padding between each pair of lights.

height

Returns the height of the SVG document.

This is the height of a traffic light block plus padding at the top and bottom.

corner_radius

Returns the radius of the circles used to curve the corners of a traffic light block. The default is 20. This can be changed when creating the object.

    my $tl = SVG::TrafficLight->new({ corner_radius => 500 });

svg

This is the SVG object that used to create the SVG document. A standard object is created for you. It's possible to pass in your own when creating the object.

    my $tl = SVG::TrafficLight->new({
      svg => SVG->new(width => $width, height => $height,
    });

colours

This defines the colours used to draw the traffic lights. The value must be a reference to a hash. The hash must contain three keys - red, amber, and green. The values are references to two-element arrays. The first element in each array is the colour used when the light is off and the second is the colour used when the light is on.

The values of the colours can be anything that is recognised as a colour in SVG. These are either colour names (e.g. 'red') or RGB definitions (e.g. 'rgb(255,0,0,)'.

The default values can be overridden when creating the object.

    my $tl = SVG::TrafficLight->new({
      colours => {
        red   => [ ... ],
        amber => [ ... ],
        green => [ ... ],
      }.
    });

sequence

Defines a sequence of traffic lights to display. This is an array reference. Each element in the array is a hash reference which defines which of the three lights are on or off.

The default sequence demonstates the full standard British traffic light sequence of green, amber, red, red and amber, green. This can be changed when creating the object. For example here is how to reproduct the green, amber, red, green sequence that is used in many countries.

    my $tl = SVG::TrafficLight->new({
      sequence => [
        { red => 0, amber => 0, green => 1 },
        { red => 0, amber => 1, green => 0 },
        { red => 1, amber => 0, green => 0 },
        { red => 0, amber => 0, green => 1 },
      ],
    });

You don't need to reproduce sequences that are seen in the real world, The following code, for example, gives a sequence consisting of two steps - one where all lights are off followed by one where all lights are on.

    my $tl = SVG::TrafficLight->new({
      sequence => [
        { red => 0, amber => 0, green => 0 },
        { red => 1, amber => 1, green => 1 },
      ],
    });

draw_a_lightset_at($x, $y, \%lights)

Draws a set of three lights at a given position on the canvas.

Takes three mandatory parameters:

$x

The x co-ordinate of the top-left corner of the light set.

$y

The co-ordinate of the top left corner of the light set.

\%lights

A reference to a hash indicating which lights should be turned on or off. The keys in the has should be the names of the lights in the light set (red, amber and green) and the associated values should be a 1 (to indicate that the light is on) or a 0 (to indicate that the light is off).

AUTHOR

Dave Cross <dave@perlhacks.com>

COPYRIGHT

Copyright (c) 2017 Magnum Solutions Ltd. All rights reserved.

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

SEE ALSO

SVG