File Coverage

lib/Data/CloudWeights.pm
Criterion Covered Total %
statement 132 133 99.2
branch 45 68 66.1
condition 10 20 50.0
subroutine 16 16 100.0
pod 2 2 100.0
total 205 239 85.7


line stmt bran cond sub pod time code
1             package Data::CloudWeights;
2              
3 1     1   171362 use 5.01;
  1         4  
  1         34  
4 1     1   527 use namespace::autoclean;
  1         22976  
  1         7  
5 1     1   73 use version; our $VERSION = qv( sprintf '0.14.%d', q$Rev: 1 $ =~ /\d+/gmx );
  1         1  
  1         14  
6              
7 1     1   945 use Moo;
  1         11917  
  1         8  
8 1     1   2541 use POSIX;
  1         7587  
  1         8  
9 1     1   4108 use Type::Utils qw( as enum message subtype where );
  1         31430  
  1         9  
10 1     1   1202 use Types::Standard qw( ArrayRef HashRef Int Maybe Num Str );
  1         35606  
  1         15  
11              
12             my $COLOUR = subtype 'Colour', as Str,
13             where { not $_ or $_ =~ m{ \A \# [0-9A-Fa-f]+ \z }mx };
14             my $SORT_ORDER = enum 'Sort_Order' => [ qw( asc desc ) ];
15             my $SORT_TYPE = enum 'Sort_Type' => [ qw( alpha numeric ) ];
16              
17             # Public attributes
18             has 'cold_colour' => is => 'ro', isa => Maybe[$COLOUR],
19             documentation => 'Blue', default => '#0000FF';
20              
21             has 'hot_colour' => is => 'ro', isa => Maybe[$COLOUR],
22             documentation => 'Red', default => '#FF0000';
23              
24             has 'colour_pallet' => is => 'lazy', isa => ArrayRef[$COLOUR],
25             documentation => 'Alternative to colour calculation';
26              
27             has 'decimal_places' => is => 'rw', isa => Int, default => 3,
28             documentation => 'Defaults for ems';
29              
30             has 'limit' => is => 'rw', isa => Int, default => 0,
31             documentation => 'Max size of returned list. Zero no limit';
32              
33             has 'max_count' => is => 'rw', isa => Int, default => 0,
34             documentation => 'Current max value across all tags cloud';
35              
36             has 'max_size' => is => 'rw', isa => Num, default => 3.0,
37             documentation => 'Output size no more than';
38              
39             has 'min_count' => is => 'rw', isa => Int, default => -1,
40             documentation => 'Current min';
41              
42             has 'min_size' => is => 'rw', isa => Num, default => 1.0,
43             documentation => 'Output size no less than';
44              
45             has 'sort_field' => is => 'rw', isa => Maybe[Str], default => 'tag',
46             documentation => 'Output sorted by this field';
47              
48             has 'sort_order' => is => 'rw', isa => $SORT_ORDER,
49             documentation => 'Sort order - asc or desc', default => 'asc';
50              
51             has 'sort_type' => is => 'rw', isa => $SORT_TYPE,
52             documentation => 'Sort type - alpha or numeric',
53             default => 'alpha';
54              
55             has 'total_count' => is => 'rw', isa => Int, default => 0,
56             documentation => 'Current total for all tags in the cloud';
57              
58             # Private attributes
59             has '_index' => is => 'ro', isa => HashRef, default => sub { {} };
60             has '_sorts' => is => 'ro', isa => HashRef, default => sub { {
61             alpha => {
62             asc => sub { my $x = shift; sub { $_[ 0 ]->{ $x } cmp $_[ 1 ]->{ $x } }
63             },
64             desc => sub { my $x = shift; sub { $_[ 1 ]->{ $x } cmp $_[ 0 ]->{ $x } }
65             },
66             },
67             numeric => {
68             asc => sub { my $x = shift; sub { $_[ 0 ]->{ $x } <=> $_[ 1 ]->{ $x } }
69             },
70             desc => sub { my $x = shift; sub { $_[ 1 ]->{ $x } <=> $_[ 0 ]->{ $x } }
71             },
72             } } };
73             has '_tags' => is => 'ro', isa => ArrayRef, default => sub { [] };
74              
75             # Public methods
76             sub add { # Include the passed args in this cloud's formation
77 6     6 1 1536 my ($self, $tag, $count, $value) = @_;
78              
79 6 100       22 $tag or return; # Mandatory arg used as a key in tag ref index
80              
81             # Mask out null strings and negative numbers from the passed count value
82 5 100       13 $count = defined $count ? abs $count : 0;
83              
84             # Add this count to the total for this cloud
85 5         98 $self->total_count( $self->total_count + $count );
86              
87 5 100       634 if (not exists $self->_index->{ $tag }) {
88             # Create a new tag reference and add to both list and index
89 3         10 my $tag_ref = { count => $count, tag => $tag, value => $value };
90              
91 3         4 push @{ $self->_tags }, $self->_index->{ $tag } = $tag_ref;
  3         16  
92             }
93             else {
94 2         7 my $index = $self->_index->{ $tag };
95              
96             # Calls with the same tag are cumulative
97 2         4 $count += $index->{count}; $index->{count} = $count;
  2         2  
98              
99 2 50       7 if (defined $value) {
100 2         4 my $tag_value = $index->{value};
101              
102             # Make an array if there are two or more calls to add the same tag
103 2 100 66     13 $tag_value and ref $tag_value ne 'ARRAY'
104             and $index->{value} = [ $tag_value ];
105              
106             # Push passed value in each call onto the values array.
107 2 100       5 if ($tag_value) { push @{ $index->{value} }, $value }
  1         3  
  1         3  
108 1         1 else { $index->{value} = $value }
109             }
110             }
111              
112             # Update this cloud's max and min values
113 5 100       97 $count > $self->max_count and $self->max_count( $count );
114 5 100       172 $self->min_count == -1 and $self->min_count( $count );
115 5 50       154 $count < $self->min_count and $self->min_count( $count );
116              
117             # Return the current cumulative count for this tag
118 5         37 return $count;
119             }
120              
121             sub formation { # Calculate the result set for this cloud
122 7     7 1 2154 my $self = shift;
123 7         112 my $prec = 10**$self->decimal_places;
124 7         639 my $bands = scalar @{ $self->colour_pallet } - 1;
  7         91  
125 7   50     167 my $range = (abs $self->max_count - $self->min_count) || 1;
126 7         1260 my $step = ($self->max_size - $self->min_size) / $range;
127 7         1109 my $compare = $self->_get_sort_method;
128 7         15 my $ntags = @{ $self->_tags };
  7         16  
129 7         9 my $out = [];
130              
131 7 100       21 $ntags == 0 and return []; # No calls to add were made
132              
133 6 100       11 if ($ntags == 1) { # One call to add was made
134 1   33     21 return [ { colour => $self->hot_colour || pop @{ $self->colour_pallet },
135             count => $self->_tags->[ 0 ]->{count},
136             percent => 100,
137             size => $self->max_size,
138             tag => $self->_tags->[ 0 ]->{tag},
139             value => $self->_tags->[ 0 ]->{value} } ];
140             }
141              
142 5         9 for (sort { $compare->( $a, $b ) } @{ $self->_tags }) {
  8         16  
  5         18  
143 11         555 my $count = $_->{count};
144 11         183 my $base = $count - $self->min_count;
145 11         56 my $index = int 0.5 + ($base * $bands / $range);
146 11         165 my $percent = 100 * $count / $self->total_count;
147 11         211 my $size = $self->min_size + $step * $base;
148              
149             # Push the return array with a hash ref for each key value pair
150             # passed to the add method
151 11         52 push @{ $out }, { colour => $self->colour_pallet->[ $index ],
  11         166  
152             count => $count,
153             percent => (int 0.5 + $prec * $percent) / $prec,
154             size => (int 0.5 + $prec * $size ) / $prec,
155             tag => $_->{tag},
156             value => $_->{value} };
157              
158 11 100 66     249 $self->limit and @{ $out } == $self->limit and last;
  1         19  
159             }
160              
161 5         55 return $out;
162             }
163              
164             # Private methods
165             sub _build_colour_pallet {
166 1     1   414 my $self = shift;
167              
168             # Unsetting hot or cold colour strings in the constructor will cause
169             # the default pallet to be used instead
170 1 50 33     18 $self->cold_colour and $self->hot_colour
171             and return [ _generate( 12, $self->cold_colour, $self->hot_colour ) ];
172              
173 0         0 return [ '#CC33FF', '#663399', '#3300CC', '#99CCFF',
174             '#00FFFF', '#66FFCC', '#66CC99', '#006600',
175             '#CCFF66', '#FFFF33', '#FF6600', '#FF0000' ];
176             }
177              
178             sub _generate { # Robbed from Color::Spectrum since it's abandoned
179 1   33 1   3 my ($cnt, $col1, $col2) = @_; $col2 ||= $col1; my @murtceps;
  1         4  
  1         1  
180              
181 1 50       3 push @murtceps, uc $col1; my $pound = $col1 =~ /^\#/ ? '#' : q();
  1         25  
182              
183 1         3 $col1 =~s/^\#//; $col2 =~s/^\#//;
  1         3  
184              
185 1 50       2 my $clockwise = 0; $cnt < 0 and $clockwise++; $cnt = int( abs( $cnt ) );
  1         4  
  1         2  
186              
187 1 0       3 $cnt <= 1 and return ( wantarray() ? @murtceps : \@murtceps );
    50          
188 1 0       7 $cnt == 2 and return ( wantarray()
    50          
189             ? ( "${pound}${col1}", "${pound}${col2}" )
190             : [ "${pound}${col1}", "${pound}${col2}" ] );
191              
192             # The RGB values need to be on the decimal scale,
193             # so we divide em by 255 enpassant.
194 3         12 my ( $h1, $s1, $i1 )
195 1         7 = _rgb2hsi( map { hex() / 255 } unpack( 'a2a2a2', $col1 ) );
196 3         7 my ( $h2, $s2, $i2 )
197 1         5 = _rgb2hsi( map { hex() / 255 } unpack( 'a2a2a2', $col2 ) );
198              
199 1         2 $cnt--; my $sd = ( $s2 - $s1 ) / $cnt; my $id = ( $i2 - $i1 ) / $cnt;
  1         2  
  1         2  
200              
201 1         1 my $hd = $h2 - $h1;
202              
203 1 0       8 $hd = ( uc( $col1 ) eq uc( $col2 ) )
    50          
    50          
204             ? ( $clockwise ? -1 : 1 ) / $cnt
205             : ( ( $hd < 0 ? 1 : 0 ) + $hd - $clockwise) / $cnt;
206              
207 1         4 while (--$cnt) {
208 10         9 $s1 += $sd; $i1 += $id; $h1 += $hd;
  10         10  
  10         7  
209 10 50       16 $h1 > 1 and $h1 -= 1; $h1 < 0 and $h1 += 1;
  10 100       17  
210 30         68 push @murtceps, sprintf "${pound}%02X%02X%02X",
211 10         17 map { int( $_ * 255 + 0.5 ) } _hsi2rgb( $h1, $s1, $i1 );
212             }
213              
214 1         4 push @murtceps, uc "${pound}${col2}";
215              
216 1 50       49 return wantarray() ? @murtceps : \@murtceps;
217             }
218              
219             sub _get_sort_method { # Add called multiple times, determine the sorting method
220 7     7   9 my $self = shift;
221              
222             # No sorting if sort field is false
223 7 100   1   105 my $field = $self->sort_field or return sub { return 0 };
  1         3  
224              
225 6 50       749 ref $field and return $field; # User supplied subroutine
226              
227 6         101 my $orderby = $self->_sorts->{ lc $self->sort_type }
228             ->{ lc $self->sort_order }->( $field );
229              
230             # Protect against wrong sort type for the data
231             return $field ne 'tag'
232 2   66 2   3 ? sub { return $orderby->( @_ ) || $_[ 0 ]->{tag} cmp $_[ 1 ]->{tag} }
233 6 100       22 : $orderby;
234             }
235              
236             sub _rgb2hsi {
237 2     2   3 my ( $r, $g, $b ) = @_; my ( $h, $s, $i ) = ( 0, 0, 0 );
  2         2  
238              
239 2 50       4 $i = ( $r + $g + $b ) / 3; $i == 0 and return ( $h, $s, $i );
  2         6  
240              
241 2         5 my $x = $r - 0.5 * ( $g + $b ); my $y = 0.866025403 * ( $g - $b );
  2         3  
242              
243 2 50       13 $s = ( $x ** 2 + $y ** 2 ) ** 0.5; $s == 0 and return ( $h, $s, $i );
  2         4  
244              
245 2         25 $h = POSIX::atan2( $y , $x ) / ( 2 * 3.1415926535 );
246              
247 2         185 return ( $h, $s, $i );
248             }
249              
250             sub _hsi2rgb {
251 10     10   11 my ( $h, $s, $i ) = @_; my ( $r, $g, $b ) = ( 0, 0, 0 );
  10         11  
252              
253             # Degenerate cases. If !intensity it's black, if !saturation it's grey
254 10 50       15 $i == 0 and return ( $r, $g, $b ); $s == 0 and return ( $i, $i, $i );
  10 50       15  
255              
256 10         10 $h = $h * 2 * 3.1415926535;
257              
258 10         23 my $x = $s * cos( $h ); my $y = $s * sin( $h );
  10         18  
259              
260 10         8 $r = $i + ( 2 / 3 * $x );
261 10         14 $g = $i - ( $x / 3 ) + ( $y / 2 / 0.866025403 );
262 10         11 $b = $i - ( $x / 3 ) - ( $y / 2 / 0.866025403 );
263             # Limit 0<=x<=1 ## YUCK but we go outta range without it.
264 10 50       11 ( $r, $b, $g ) = map { $_ < 0 ? 0 : $_ > 1 ? 1 : $_ } ( $r, $b, $g );
  30 100       63  
265              
266 10         16 return ( $r, $g, $b );
267             }
268              
269             1;
270              
271             __END__