File Coverage

blib/lib/Color/Spectrum/Multi.pm
Criterion Covered Total %
statement 25 26 96.1
branch 11 12 91.6
condition n/a
subroutine 4 4 100.0
pod 1 1 100.0
total 41 43 95.3


line stmt bran cond sub pod time code
1             package Color::Spectrum::Multi;
2              
3 2     2   40830 use warnings;
  2         6  
  2         87  
4 2     2   13 use strict;
  2         4  
  2         431  
5 2     2   14 use base qw(Color::Spectrum);
  2         9  
  2         3486  
6              
7              
8             =head1 NAME
9              
10             Color::Spectrum::Multi - simple L wrapper to handle fading
11             between multiple colours.
12              
13             =head1 VERSION
14              
15             Version 0.02
16              
17             =cut
18              
19             our $VERSION = '0.02';
20              
21              
22             =head1 SYNOPSIS
23              
24             A simple wrapper around L, to allow generating a range of colours
25             fading between multiple colours (e.g. a red -> yellow -> green fade) easy.
26              
27             Usage is much the same as L, except you can supply as many
28             colours as you wish.
29              
30             # Procedural interface:
31             use Color::Spectrum::Multi qw(generate);
32             my @color = generate(10,'#FF0000','#00FF00', '#0000FF');
33              
34             # OO interface:
35             use Color::Spectrum::Multi;
36             my $spectrum = Color::Spectrum::Multi->new();
37             my @color = $spectrum->generate(10,'#FF0000','#00FF00', '#0000FF');
38              
39             =head1 DESCRIPTION
40              
41             L provides an easy way to fade between two colours in a given
42             number of steps. This module is a simple wrapper around Color::Spectrum, making
43             it easy to fade between an arbitrary number of colours.
44              
45             =head1 METHODS
46              
47             =over
48              
49             =item generate
50              
51             Given the desired number of steps and two or more colours, returns a series of
52             colours.
53              
54             =cut
55              
56             sub generate {
57 4 100   4 1 7901 my $self = shift if ref($_[0]) eq __PACKAGE__;
58            
59             # If we have two or less colours, just allow Color::Spectrum to do its
60             # thing:
61 4 50       15 if (@_ <= 2) {
62 0         0 return Color::Spectrum::generate(@_);
63             }
64              
65 4         13 my ($steps, @points) = @_;
66 4         6 my @colours;
67 4         7 my $steps_used = 0;
68             # take the first colour waypoint off:
69 4         7 my $startpoint = shift @points;
70            
71             # How many steps do we get between each waypoint?
72 4         14 my $substeps = int($steps / scalar @points);
73 4         13 while(my $endpoint = shift @points) {
74 6 100       18 if (@points == 0) {
75             # there's no more points left... make sure we don't fall short
76             # on the number of steps:
77 4 100       13 if (($steps_used + $substeps) != $steps) {
78 2         4 $substeps = $steps - $steps_used;
79             }
80             }
81            
82             # Since we start from the last colour of the previous fade, if this
83             # isn't the first fade, we want to generate one extra colour, and drop
84             # the first (otherwise, we'd duplicate colours)
85 6 100       31 my @colour_set =
86             Color::Spectrum::generate(
87             $steps_used ? $substeps+1 : $substeps, $startpoint,$endpoint
88             );
89 6 100       1532 push @colours, $steps_used ? @colour_set[1..$substeps] : @colour_set;
90              
91             # next fade will start from last colour of this fade:
92 6         10 $startpoint = $endpoint;
93            
94 6         24 $steps_used += $substeps;
95             }
96 4         21 return @colours;
97             }
98              
99             =back
100              
101             =head1 AUTHOR
102              
103             David Precious, C<< >>
104              
105             =head1 BUGS
106              
107             Please report any bugs or feature requests to C, or through
108             the web interface at L. I will be notified, and then you'll
109             automatically be notified of progress on your bug as I make changes.
110              
111              
112              
113              
114             =head1 SUPPORT
115              
116             You can find documentation for this module with the perldoc command.
117              
118             perldoc Color::Spectrum::Multi
119              
120              
121             You can also look for information at:
122              
123             =over 4
124              
125             =item * RT: CPAN's request tracker
126              
127             L
128              
129             =item * AnnoCPAN: Annotated CPAN documentation
130              
131             L
132              
133             =item * CPAN Ratings
134              
135             L
136              
137             =item * Search CPAN
138              
139             L
140              
141             =back
142              
143              
144              
145             =head1 COPYRIGHT & LICENSE
146              
147             Copyright 2009 David Precious, all rights reserved.
148              
149             This program is free software; you can redistribute it and/or modify it
150             under the same terms as Perl itself.
151              
152              
153             =cut
154              
155             1; # End of Color::Spectrum::Multi