File Coverage

blib/lib/WWW/CloudCreator.pm
Criterion Covered Total %
statement 9 72 12.5
branch 0 18 0.0
condition 0 11 0.0
subroutine 3 9 33.3
pod 6 6 100.0
total 18 116 15.5


line stmt bran cond sub pod time code
1             # $Id: $ $Revision: $ $Source: $ $Date: $
2              
3             package WWW::CloudCreator;
4              
5 1     1   21310 use strict;
  1         2  
  1         36  
6 1     1   4 use warnings;
  1         1  
  1         27  
7              
8 1     1   813 use POSIX qw(ceil floor);
  1         6428  
  1         6  
9              
10             our $VERSION = '1.1';
11              
12             sub new {
13 0     0 1   my ($class, %args) = @_;
14 0           my $self = bless{
15             'counts' => {},
16             'smallest' => 8,
17             'largest' => 16,
18             'cold' => '000',
19             'hot' => 'E00',
20             %args,
21             }, $class;
22 0           return $self;
23             }
24              
25             sub add {
26 0     0 1   my ( $self, $tag, $count ) = @_;
27 0 0 0       if (! $tag || ! $count) { return 0; }
  0            
28 0           $self->{counts}->{$tag} = $count;
29 0           return 1;
30             }
31              
32             sub gencloud {
33 0     0 1   my ($self) = @_;
34 0   0       my $smallest = $self->{'smallest'} || 8;
35 0   0       my $largest = $self->{'largest'} || 16;
36 0   0       my $cold = $self->{'cold'} || '000';
37 0   0       my $hot = $self->{'hot'} || '000';
38 0           my $counts = $self->{'counts'};
39 0           my @tags = sort { $counts->{$b} <=> $counts->{$a} } keys %{$counts};
  0            
  0            
40 0           my $ntags = scalar @tags;
41 0 0         if ($ntags == 0) {
    0          
42 0           return q{};
43             } elsif ($ntags == 1) {
44 0           my $tag = $tags[0];
45 0           return [ $tag, 1, 'font-size:' . $smallest . q{;} ];
46             }
47 0           my $min = $counts->{$tags[-1]};
48 0           my $max = $counts->{$tags[0]};
49 0           my $spread = $max - $min;
50 0           my ($fontspread, $fontstep);
51 0 0         if ($largest != $smallest) {
52 0           $fontspread = $largest - $smallest;
53 0 0         if ($spread > 0) {
54 0           $fontstep = $fontspread / $spread;
55             } else {
56 0           $fontstep = 0;
57             }
58             }
59 0           my (@hotarray, @coldarray, @coldval, @hotval, @colorspread, @colorstep);
60 0 0         if ($hot ne $cold) {
61 0           @hotarray = map { hex $_ } (split //xm, $hot);
  0            
62 0           @coldarray = map { hex $_ } (split //xm, $cold);
  0            
63 0           for my $i (0 .. 2) {
64 0           push @coldval, hexdec($coldarray[$i]);
65 0           push @hotval, hexdec($hotarray[$i]);
66 0           push @colorspread, ( hexdec($hotarray[$i]) - hexdec($coldarray[$i]) );
67 0 0         if ($spread > 0) {
68 0           push @colorstep, ( hexdec($hotarray[$i]) - hexdec($coldarray[$i]) ) / $spread;
69             } else {
70 0           push @colorstep, '0';
71             }
72             }
73             }
74 0           my (@out);
75 0           foreach my $tag ( sort @tags ) {
76 0           my $fraction = $counts->{$tag} - $min;
77 0           my $fontsize = $smallest + ( $fontstep * $fraction);
78 0           my (@style, $color);
79 0 0         if ($hot ne $cold) {
80 0           for my $i ( 0 .. 2 ) {
81 0           my $ihex = $coldarray[$i] + ($colorstep[$i] * $fraction);
82 0           my $decihex = dechex( $ihex );
83 0           $color .= $decihex;
84             }
85 0           } else { $color = $cold; }
86 0           push @style, 'color: #' . $color . q{;};
87 0 0         if ($largest != $smallest) {
88 0           push @style, 'font-size: ' . round($fontsize) . 'pt;';
89             }
90 0           push @out, [ $tag, $counts->{$tag}, join q{}, @style];
91             }
92 0           return @out;
93             }
94              
95 0     0 1   sub round { return int $_[0] + .5 * ($_[0] <=> 0); }
96              
97 0     0 1   sub dechex { return sprintf '%x', $_[0]; }
98              
99 0     0 1   sub hexdec { return hex $_[0]; }
100              
101             1;
102             __END__