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