blib/lib/Color/Spectrum/Multi.pm

filter: Statementstmt 96.4% 27 / 28 Branchbran 92.8% 13 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 4 / 4 Podpod 100% 1 / 1 totaltotal 95.7% 45 / 47 CC 8 SCAR 18.1
CC
8
Cov
95%
generate
8.0
BEGIN
1.0
BEGIN
1.0
CRAP 6.1
↑ Summary
1 package Color::Spectrum::Multi;
2  
3 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use base qw(Color::Spectrum);
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7  
8 =head1 NAME
9  
10 Color::Spectrum::Multi - simple L<Color::Spectrum> wrapper to handle fading
11 between multiple colours.
12  
13 =head1 VERSION
14  
15 Version 0.02
16  
17 =cut
18  
19 3 our $VERSION = '0.02';
20  
21  
22 =head1 SYNOPSIS
23  
24 A simple wrapper around L<Color::Spectrum>, 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<Color::Spectrum>, 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<Color::Spectrum> 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 4 ▶sub generate {
Subroutine generate: called CC=8 SCAR=20.8
Pod documented
57 4 ▶ my $self = shift if ref($_[0]) eq __PACKAGE__;
Branch: 1 branch
BranchTrueFalseTotal
if ref $_[0] eq __PACKAGE__ 2 2 4
58
59 # If we have two or less colours, just allow Color::Spectrum to do its
60 # thing:
61 4 ▶ if (@_ <= 2) {
Branch: 1 branch
BranchTrueFalseTotal
if (@_ <= 2) 0 4 4
62 0 return Color::Spectrum::generate(@_);
63 }
64  
65 4 my ($steps, @points) = @_;
66 4 my @colours;
67 4 my $steps_used = 0;
68 # take the first colour waypoint off:
69 4 my $startpoint = shift @points;
70
71 # How many steps do we get between each waypoint?
72 4 my $substeps = int($steps / scalar @points);
73 4 ▶ while(my $endpoint = shift @points) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $endpoint = shift @points) 6 4 10
74 6 ▶ if (@points == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (@points == 0) 4 2 6
75 # there's no more points left... make sure we don't fall short
76 # on the number of steps:
77 4 ▶ if (($steps_used + $substeps) != $steps) {
Branch: 1 branch
BranchTrueFalseTotal
if ($steps_used + $substeps != $steps) 2 2 4
78 2 $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 ▶ my @colour_set =
Branch: 1 branch
BranchTrueFalseTotal
$steps_used ? : 2 4 6
86 Color::Spectrum::generate(
87 $steps_used ? $substeps+1 : $substeps, $startpoint,$endpoint
88 );
89 6 ▶ push @colours, $steps_used ? @colour_set[1..$substeps] : @colour_set;
Branch: 1 branch
BranchTrueFalseTotal
$steps_used ? : 2 4 6
90  
91 # next fade will start from last colour of this fade:
92 6 $startpoint = $endpoint;
93
94 6 $steps_used += $substeps;
95 }
96 4 return @colours;
97 }
98  
99 =back
100  
101 =head1 AUTHOR
102  
103 David Precious, C<< <davidp at preshweb.co.uk> >>
104  
105 =head1 BUGS
106  
107 Please report any bugs or feature requests to C<bug-color-spectrum-multi at rt.cpan.org>, or through
108 the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Color-Spectrum-Multi>. 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<http://rt.cpan.org/NoAuth/Bugs.html?Dist=Color-Spectrum-Multi>
128  
129 =item * AnnoCPAN: Annotated CPAN documentation
130  
131 L<http://annocpan.org/dist/Color-Spectrum-Multi>
132  
133 =item * CPAN Ratings
134  
135 L<http://cpanratings.perl.org/d/Color-Spectrum-Multi>
136  
137 =item * Search CPAN
138  
139 L<http://search.cpan.org/dist/Color-Spectrum-Multi/>
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 3 1; # End of Color::Spectrum::Multi
↑ Summary