blib/lib/HTML/Rainbow.pm

filter: Statementstmt 100% 51 / 51 Branchbran 100% 28 / 28 Conditioncond 100% 5 / 5 MC/DCmcdc 100% 3 / 3 Subroutinesub 100% 7 / 7 Podpod 100% 2 / 2 totaltotal 100% 96 / 96 CC 21 SCAR 22.8
CC
21
Cov
100%
new
14.0
rainbow
8.0
BEGIN
1.0
CRAP 9.8
↑ Summary
1 # HTML::Rainbow.pm
2 #
3 # Copyright (c) 2005-2009 David Landgren
4 # All rights reserved
5  
6 package HTML::Rainbow;
7  
8 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use Exporter;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use Tie::Cycle::Sinewave;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 3 ▶use vars qw/$VERSION @PRIMES @ISA @EXPORT_OK/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 3 $VERSION = '0.06';
15 3 @ISA = ('Exporter');
16 3 @PRIMES = qw(17 19 23 29 31 37 41 43 47 53 59 61 67 71 73 79);
17  
18 =head1 NAME
19  
20 HTML::Rainbow - Put colour into your HTML
21  
22 =head1 VERSION
23  
24 This document describes version 0.06 of HTML::Rainbow, released
25 2009-10-04.
26  
27 =head1 SYNOPSIS
28  
29 use HTML::Rainbow 'rainbow';
30 print rainbow('hello, world');
31  
32 =head1 DESCRIPTION
33  
34 C<HTML::Rainbow> will take plain text string (or array of strings)
35 and mark it up with C<< <font> >> tags (or C<< <span> >> tags
36 if you're feeling particularly orthodox), and produce text that
37 drifts endlessly from one colour to the next.
38  
39 The intensity of the red, green and blue channels follow mutually
40 prime sinusoidal periods.
41  
42 =for html
43 <p>This comes in handy when you have the burning desire
44 to say</p>
45 <blockquote><font color="#737373">P</font><font
46 color="#7c797c">e</font><font color="#858085">r</font><font
47 color="#8e878e">l</font> <font color="#978d97">i</font><font
48 color="#9f949f">s</font> <font color="#a89aa8">a</font> <font
49 color="#afa0af">l</font><font color="#b7a7b7">a</font><font
50 color="#beacbe">n</font><font color="#c4b2c4">g</font><font
51 color="#c9b7c9">u</font><font color="#cebcce">a</font><font
52 color="#d2c1d2">g</font><font color="#d6c5d6">e</font> <font
53 color="#d8c9d8">o</font><font color="#dacdda">p</font><font
54 color="#dbd0db">t</font><font color="#dbd3db">i</font><font
55 color="#dbd5db">m</font><font color="#dad8d6">i</font><font
56 color="#d9d9cc">z</font><font color="#d7dabf">e</font><font
57 color="#d6dbad">d</font> <font color="#d4db99">f</font><font
58 color="#d1db83">o</font><font color="#cfda6d">r</font> <font
59 color="#ccd656">s</font><font color="#c8d141">c</font><font
60 color="#c5ca2e">a</font><font color="#c1c21f">n</font><font
61 color="#bdb813">n</font><font color="#b9ad0c">i</font><font
62 color="#b5a00a">n</font><font color="#b0930c">g</font> <font
63 color="#ab860d">a</font><font color="#a6780e">r</font><font
64 color="#a16a0f">b</font><font color="#9c5c10">i</font><font
65 color="#964f12">t</font><font color="#914213">r</font><font
66 color="#8b3615">a</font><font color="#852b17">r</font><font
67 color="#802119">y</font> <font color="#7a191b">t</font><font
68 color="#74131d">e</font><font color="#6e0e1f">x</font><font
69 color="#680b22">t</font> <font color="#630a24">f</font><font
70 color="#5d0a27">i</font><font color="#572129">l</font><font
71 color="#52522c">e</font><font color="#4c8d2f">s</font><font
72 color="#47c032">,</font> <font color="#41da35">e</font><font
73 color="#3cd338">x</font><font color="#37d13b">t</font><font
74 color="#33ce3f">r</font><font color="#2ecb42">a</font><font
75 color="#2ac845">c</font><font color="#26c449">t</font><font
76 color="#22c04c">i</font><font color="#1ebc50">n</font><font
77 color="#1bb854">g</font> <font color="#18b457">i</font><font
78 color="#15af5b">n</font><font color="#13aa5f">f</font><font
79 color="#10a562">o</font><font color="#0ea066">r</font><font
80 color="#0d9b6a">m</font><font color="#0b956e">a</font><font
81 color="#0b9072">t</font><font color="#0a8a75">i</font><font
82 color="#0a8479">o</font><font color="#0a7e7d">n</font> <font
83 color="#0a7981">f</font><font color="#0a7385">r</font><font
84 color="#0b6d88">o</font><font color="#0c678c">m</font> <font
85 color="#0d6190">t</font><font color="#0e5c93">h</font><font
86 color="#0f5697">o</font><font color="#11509b">s</font><font
87 color="#134b9e">e</font> <font color="#1546a2">t</font><font
88 color="#1740a5">e</font><font color="#193ba8">x</font><font
89 color="#1b36ab">t</font> <font color="#1e32af">f</font><font
90 color="#202db2">i</font><font color="#2329b5">l</font><font
91 color="#2625b8">e</font><font color="#2921ba">s</font><font
92 color="#2d1ebd">,</font> <font color="#301ac0">a</font><font
93 color="#3317c2">n</font><font color="#3714c5">d</font> <font
94 color="#3b12c7">p</font><font color="#3f10c9">r</font><font
95 color="#420ecb">i</font><font color="#460ccd">n</font><font
96 color="#4a0bcf">t</font><font color="#4f0ad1">i</font><font
97 color="#530ad3">n</font><font color="#570ad4">g</font> <font
98 color="#5b0ad5">r</font><font color="#600ad7">e</font><font
99 color="#640cd8">p</font><font color="#680ed9">o</font><font
100 color="#6d12d9">r</font><font color="#7115da">t</font><font
101 color="#761adb">s</font> <font color="#7a1fdb">b</font><font
102 color="#7e25db">a</font><font color="#832cdb">s</font><font
103 color="#8733db">e</font><font color="#8c3bda">d</font> <font
104 color="#9043d7">o</font><font color="#944bd2">n</font> <font
105 color="#9854cc">t</font><font color="#9c5dc4">h</font><font
106 color="#a066ba">a</font><font color="#a470af">t</font> <font
107 color="#a879a3">i</font><font color="#ac8296">n</font><font
108 color="#b08b89">f</font><font color="#b3947b">o</font><font
109 color="#b79d6d">r</font><font color="#baa55f">m</font><font
110 color="#bdad51">a</font><font color="#c0b544">t</font><font
111 color="#c3bc38">i</font><font color="#c6c22d">o</font><font
112 color="#c8c823">n</font><font color="#cbcd1b">.</font> <font
113 color="#cdd114">I</font><font color="#cfd50f">t</font><font
114 color="#d1d80b">'</font><font color="#d3da0a">s</font> <font
115 color="#d5db0a">a</font><font color="#d6db0c">l</font><font
116 color="#d8db10">s</font><font color="#d9da16">o</font> <font
117 color="#dad91d">a</font> <font color="#dad726">g</font><font
118 color="#dbd530">o</font><font color="#dbd33c">o</font><font
119 color="#dbd048">d</font> <font color="#dbcd55">l</font><font
120 color="#dbc963">a</font><font color="#dac571">n</font><font
121 color="#d6c17f">g</font><font color="#d0bc8d">u</font><font
122 color="#c9b79a">a</font><font color="#c1b2a7">g</font><font
123 color="#b7acb2">e</font> <font color="#aba6bd">f</font><font
124 color="#9fa0c6">o</font><font color="#929ace">r</font> <font
125 color="#8494d4">m</font><font color="#778dd8">a</font><font
126 color="#6986db">n</font><font color="#5b80db">y</font> <font
127 color="#4d79da">s</font><font color="#4172d8">y</font><font
128 color="#356bd6">s</font><font color="#2a65d2">t</font><font
129 color="#205ece">e</font><font color="#1857c9">m</font> <font
130 color="#1251c4">m</font><font color="#0d4abe">a</font><font
131 color="#0b44b7">n</font><font color="#0a3eb0">a</font><font
132 color="#0a38a8">g</font><font color="#2233a0">e</font><font
133 color="#532e97">m</font><font color="#8f298e">e</font><font
134 color="#c12485">n</font><font color="#da207c">t</font> <font
135 color="#d31c73">t</font><font color="#ad1869">a</font><font
136 color="#741560">s</font><font color="#3b1257">k</font><font
137 color="#140f4e">s</font><font color="#0a0d46">.</font> <font
138 color="#220c3e">T</font><font color="#330b36">h</font><font
139 color="#460a2e">e</font> <font color="#5b0a28">l</font><font
140 color="#720a21">a</font><font color="#890a1c">n</font><font
141 color="#9e0a17">g</font><font color="#b20b13">u</font><font
142 color="#c20c0f">a</font><font color="#cf0c0d">g</font><font
143 color="#d80d0b">e</font> <font color="#db0e0a">i</font><font
144 color="#da100a">s</font> <font color="#d9110a">i</font><font
145 color="#d7120f">n</font><font color="#d51419">t</font><font
146 color="#d21626">e</font><font color="#d01838">n</font><font
147 color="#cc1a4c">d</font><font color="#c81c62">e</font><font
148 color="#c41e78">d</font> <font color="#c0208f">t</font><font
149 color="#bb23a4">o</font> <font color="#b625b6">b</font><font
150 color="#b128c6">e</font> <font color="#ab2ad2">p</font><font
151 color="#a52dd9">r</font><font color="#9f30db">a</font><font
152 color="#9933d9">c</font><font color="#9336d8">t</font><font
153 color="#8c39d6">i</font><font color="#853dd4">c</font><font
154 color="#7f40d2">a</font><font color="#7843cf">l</font> <font
155 color="#7147cc">(</font><font color="#6a4ac9">e</font><font
156 color="#644ec5">a</font><font color="#5d51c2">s</font><font
157 color="#5655be">y</font> <font color="#5059b9">t</font><font
158 color="#4a5cb5">o</font> <font color="#4360b0">u</font><font
159 color="#3d64ac">s</font><font color="#3868a7">e</font><font
160 color="#326ca2">,</font> <font color="#2d6f9c">e</font><font
161 color="#287397">f</font><font color="#237791">f</font><font
162 color="#1f7b8c">i</font><font color="#1b7f86">c</font><font
163 color="#178280">i</font><font color="#14867a">e</font><font
164 color="#118a75">n</font><font color="#0f8e6f">t</font><font
165 color="#0d9169">,</font> <font color="#0c9563">c</font><font
166 color="#0a985d">o</font><font color="#0a9c58">m</font><font
167 color="#0a9f52">p</font><font color="#0aa34d">l</font><font
168 color="#0ba647">e</font><font color="#0fa942">t</font><font
169 color="#15ad3d">e</font><font color="#1cb038">)</font> <font
170 color="#24b333">r</font><font color="#2eb62f">a</font><font
171 color="#39b92a">t</font><font color="#46bc26">h</font><font
172 color="#53be22">e</font><font color="#60c11f">r</font> <font
173 color="#6ec31b">t</font><font color="#7cc618">h</font><font
174 color="#8ac815">a</font><font color="#98ca13">n</font> <font
175 color="#a4cc10">b</font><font color="#b0ce0f">e</font><font
176 color="#bbd00d">a</font><font color="#c5d20c">u</font><font
177 color="#cdd30b">t</font><font color="#d3d50a">i</font><font
178 color="#d8d60a">f</font><font color="#dad70a">u</font><font
179 color="#dbd80a">l</font> <font color="#dbd90b">(</font><font
180 color="#dada0d">t</font><font color="#d9da10">i</font><font
181 color="#d7db13">n</font><font color="#d5db17">y</font><font
182 color="#d3db1c">,</font> <font color="#d1db22">e</font><font
183 color="#cedb28">l</font><font color="#cbd82f">e</font><font
184 color="#c7d037">g</font><font color="#c4c43e">a</font><font
185 color="#c0b447">n</font><font color="#bca14f">t</font><font
186 color="#b88b58">,</font> <font color="#b37561">m</font><font
187 color="#af5e6a">i</font><font color="#aa4874">n</font><font
188 color="#a5357d">i</font><font color="#9f2486">m</font><font
189 color="#9a178f">a</font><font color="#950e98">l</font><font
190 color="#8f0aa1">)</font><font color="#890ba9">.</font></blockquote>
191  
192 Win friends, and influence enemies, on your favourite
193 HTML bulletin board.
194  
195 =head1 METHODS
196  
197 =over 8
198  
199 =item B<new>
200  
201 Creates a new C<HTML::Rainbow> object. A set of key/value parameters
202 can be supplied to control the finer details of the object's
203 behaviour.
204  
205 The colour-space of HTML is defined by a tuple of red, green and
206 blue components. Each component can vary between 0 and 255. Setting
207 all components to 0 produces black, and setting them all to 255
208 produces white. The parameters for C<new()> allow you to control
209 the behaviour of the components, either individually or as a whole.
210  
211 Each value may be specifed as a number from 0 to 255, or as a
212 percentage (such as C<50%>). Percentages are rounded to the nearest
213 integer, and values out of range are clipped to the nearest bound.
214  
215 =over 4
216  
217 =item min
218  
219 Sets the minimum value for all three components. For example, a
220 value of 0 (zero) may result in white being produced. This may
221 produce invisible text if the background colour is also white.
222 Hence, one may wish to use a value between 20 to 40 if this is the
223 case.
224  
225 =item max
226  
227 Sets the maximum value for all three components. Setting it to C<100%> or
228 255 may result in black being produced. A similar warning concerning a
229 background colour of black applies here.
230  
231 =item min_red, min_green, min_blue
232  
233 Sets the minimum value for the specified colour component.
234  
235 =item max_red, max_green, max_blue
236  
237 Sets the maximum value for the specified colour component.
238  
239 =item red, green, blue
240  
241 Sets the value of the specified colour component to a fixed value.
242 For example, the following call to new()...
243  
244 my $r = HTML::Rainbow->new(
245 red => 0,
246 green => 0,
247 min_blue => 10,
248 max_blue => 240,
249 );
250  
251 ... will result in a rainbow generator that moves through various
252 shades of blue.
253  
254 =item period_list
255  
256 Set the periods available to choose from. At each peak and trough
257 of the sine wave followed by each colour component, a new period
258 length is chosen at random. This is to ensure that the sequence
259 of colours does not repeat itself too rapidly. Prime numbers
260 are well suited, and the value of period should be at least 10 (ten) or
261 more for best results. A list of periods, from 17 to 79, is used by
262 default. Very long texts will benefit from longer periods. The
263 parameter is a reference to an array.
264  
265 my $r = HTML::Rainbow->new(
266 min => 0,
267 max => '80%',
268 period_list => [qw[ 19 37 53 71 89 107 131 151 173 193 ]],
269 );
270  
271 =item use_span
272  
273 Use the HTML C<< <span> >> element instead of the C<< <font> >>
274 element for specifying the colour. The result uses 6 more characters
275 per marked up character.
276  
277 =back
278  
279 The most specific parameter wins. If both, for example, a C<red>
280 and a C<red_min> parameter are found, the C<red> parameter wins.
281 If a C<red_min> and a C<min> parameter is found, the C<red_min>
282 parameter wins.
283  
284 =cut
285  
286 11 ▶sub new {
Subroutine new: called CC=14 SCAR=26.4
Pod documented
287 11 my $class = shift;
288 11 my %args = @_;
289  
290 # sanity checks for %args
291 11 for my $attr(
292 qw(min max),
293 33 map {( $_, "${_}_min", "${_}_max" )} qw( red green blue )
294 ) {
295 121 ▶ next unless exists $args{$attr};
Branch: 1 branch
BranchTrueFalseTotal
unless exists $args{$attr} 114 7 121
296 # reduce round-off errors
297 # perl -le 'print 1/2+1/32+1/64+1/512+1/1024+1/4096'
298 7 my $scale = 2 + 1/2 + 1/32 + 1/64 + 1/512 + 1/1024 + 1/4096;
299 # 2.550048828125 approx 2.55
300 # 43% => 43 / 100 * 255 => 43 * 2.55 => 110
301 7 $args{$attr} =~ s{^(\d+(?:\.\d+)?)\s*%$}{sprintf('%d',sprintf('%0.2f',$1) * $scale)}e;
302 7 ▶ $args{$attr} = 0 if $args{$attr} < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $args{$attr} < 0 1 6 7
303 7 ▶ $args{$attr} = 255 if $args{$attr} > 255;
Branch: 1 branch
BranchTrueFalseTotal
if $args{$attr} > 255 1 6 7
304 }
305  
306 my $self = {
307 period => $args{period_list} ? $args{period_list} : \@PRIMES,
308 11 ▶ use_span => $args{use_span } || 0,
Branch: 1 branch
BranchTrueFalseTotal
$args{'period_list'} ? : 1 10 11
Condition100%2/2
$args{'use_span'} || 0
l!l
110
MC/DC100%1/1
$args{'use_span'} || 0
$args{'use_span'}
Truth table100%2/2
A
A= $args{'use_span'}
Aresult
11
00
309 };
310  
311 17 ▶ my $change_period = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
312 17 my $s = shift;
313 17 $s->period( $self->{period}[rand @{$self->{period}}] );
314 11 };
315  
316 tie $self->{$_}, 'Tie::Cycle::Sinewave', {
317 min => defined $args{$_}
318 ? $args{$_}
319 : defined $args{"${_}_min"}
320 ? $args{"${_}_min"}
321 : defined $args{min}
322 ? $args{min}
323 : 16,
324 max => defined $args{$_}
325 ? $args{$_}
326 : defined $args{"${_}_max"}
327 ? $args{"${_}_max"}
328 : defined $args{max}
329 ? $args{max}
330 : 240,
331 33 period => $self->{period}[ rand @{$self->{period}} ],
332 at_min => $change_period,
333 at_max => $change_period,
334 11 ▶ } for qw( red green blue );
Branch: 6 branches
BranchTrueFalseTotal
defined $args{$_} ? : 1 32 33
defined $args{"${_}_min"} ? : 1 31 32
defined $args{'min'} ? : 4 27 31
defined $args{$_} ? : 1 32 33
defined $args{"${_}_max"} ? : 1 31 32
defined $args{'max'} ? : 4 27 31
335  
336 11 bless $self, $class;
337 }
338  
339 =item B<rainbow>
340  
341 Converts each passed parameter to rainbowed markup, and returns
342 a single scalar with the resulting marked up text.
343  
344 print $r->rainbow( 'somewhere over the rainbow, bluebirds fly' );
345  
346 You can avoid using an intermediate variable by chaining the
347 C<rainbow> method on from the C<new> method:
348  
349 print HTML::Rainbow->new(
350 max => 127,
351 min => 0,
352 period_list => [qw[ 11 29 47 71 97 113 149 173 ]],
353 )->rainbow( $text );
354  
355 =cut
356  
357 3 push @EXPORT_OK, 'rainbow';
358 {
359 3 my $ctx;
360 10 ▶ sub rainbow {
Subroutine rainbow: called CC=8 SCAR=20.8
Pod documented
361 10 my $self = shift;
362 10 my $out = '';
363 10 ▶ if (!($self and UNIVERSAL::isa($self,'HTML::Rainbow'))) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($self and UNIVERSAL::isa($self, "HTML::Rainbow")) 5 5 10
Condition100%3/3
$self and UNIVERSAL::isa($self, "HTML::Rainbow")
!ll&&!rl&&r
235
MC/DC100%2/2
$self and UNIVERSAL::isa($self, "HTML::Rainbow")
$self UNIVERSAL::isa($self, "HTML::Rainbow")
Truth table100%3/3
A and B
A= $selfB= UNIVERSAL::isa($self, "HTML::Rainbow")
ABresult
0X0
100
111
364 # called as a function, not a method
365 5 unshift @_, $self;
366 5 ▶ $self = $ctx ? $ctx : $ctx = HTML::Rainbow->new;
Branch: 1 branch
BranchTrueFalseTotal
$ctx ? : 4 1 5
367 }
368 10 for my $str( grep defined, @_ ) {
369 8 for my $ch( split //, $str ) {
370 42 ▶ if( $ch =~ /^\s$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($ch =~ /^\s$/) { } 2 40 42
371 2 $out .= $ch;
372 }
373 else {
374 my $triple = sprintf( "#%02x%02x%02x",
375 $self->{red}, $self->{green}, $self->{blue}
376 40 );
377 40 ▶ if( $self->{use_span} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'use_span'}) { } 7 33 40
378 7 $out .= qq{<span style="color:$triple">$ch</span>};
379 }
380 else {
381 33 $out .= qq{<font color="$triple">$ch</font>};
382 }
383 }
384 }
385 }
386 $out
387 10 }
388 }
389  
390 =back
391  
392 =head1 DIAGNOSTICS
393  
394 None.
395  
396 =head1 SEE ALSO
397  
398 =over 8
399  
400 =item L<Tie::Cycle::Sinewave>
401  
402 The individual red, green and blue colour components follow
403 sinewaves produced by this module.
404  
405 =item L<HTML::Parser>
406  
407 If you want to modify an existing HTML page, you'll probably have
408 to parse it in order to extract the text. The C<eg> directory
409 contains some examples to show how this may be done.
410  
411 =back
412  
413 =head1 EXAMPLE
414  
415 The following example produces a valid HTML page.
416  
417 use strict;
418 use warnings;
419  
420 use CGI ':standard';
421 use HTML::Rainbow;
422  
423 print header(),
424 start_html(),
425 HTML::Rainbow->new->rainbow('hello, world'),
426 end_html();
427  
428 =head1 BUGS
429  
430 None known.
431  
432 Please report all bugs at
433 L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=HTML-Rainbow|rt.cpan.org>
434  
435 Make sure you include the output from the following two commands:
436  
437 perl -MHTML::Rainbow -le 'print $HTML::Rainbow::VERSION'
438 perl -V
439  
440 =head1 ACKNOWLEDGEMENTS
441  
442 This module is dedicated to John Lang, someone I used to work
443 with back in the early days of the web. I found him one day
444 painstakingly writing HTML in a text editor and reviewing the
445 results in Netscape. He was trying to do something like this,
446 to post to a bulletin board, so I wrote some very ugly Perl
447 to help him out. Ten years later, I finally got around to
448 cleaning it up.
449  
450 =head1 AUTHOR
451  
452 David Landgren, copyright (C) 2005-2009. All rights reserved.
453  
454 http://www.landgren.net/perl/
455  
456 If you (find a) use this module, I'd love to hear about it.
457 If you want to be informed of updates, send me a note. You
458 know my first name, you know my domain. Can you guess my
459 e-mail address?
460  
461 =head1 LICENSE
462  
463 This library is free software; you can redistribute it and/or modify
464 it under the same terms as Perl itself.
465  
466 =cut
467  
468 3 'The Lusty Decadent Delights of Imperial Pompeii';
469 __END__
↑ Summary