Clicker/Drawing/ColorAllocator.pm

filter: Statementstmt 94.7% 54 / 57 Branchbran 100% 2 / 2 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 91.6% 11 / 12 Podpod 100% 2 / 2 totaltotal 94.5% 69 / 73 CC 2 SCAR 2.1
CC
2
Cov
95%
__ANON__
2.0
reset
2.0
BEGIN
1.0
CRAP 1.2
« Clicker/Renderer.pm ↑ Summary Clicker/Decoration/Glass.pm »
1 package Chart::Clicker::Drawing::ColorAllocator;
2 9 $Chart::Clicker::Drawing::ColorAllocator::VERSION = '2.90';
3 9 ▶use Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
4  
5 # ABSTRACT: Color picker
6  
7 9 ▶use Graphics::Color::RGB;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 9 ▶use Color::Scheme;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10  
11  
12 has 'colors' => (
13 traits => [ 'Array' ],
14 is => 'rw',
15 isa => 'ArrayRef',
16 8 ▶ default => sub { [] },
Subroutine __ANON__: called CC=1 SCAR=0.0
17 9 handles => {
18 'add_to_colors' => 'push',
19 'clear_colors' => 'clear',
20 'color_count' => 'count',
21 'get_color' => 'get'
22 }
23 );
24  
25  
26 9 has 'position' => ( is => 'rw', isa => 'Int', default => -1 );
27  
28  
29 9 has 'color_scheme' => (
30 is => 'rw',
31 isa => 'Color::Scheme',
32 lazy_build => 1,
33 );
34  
35  
36 9 has 'seed_hue' => (
37 is => 'rw',
38 isa => 'Int',
39 required => 1,
40 default => 270,
41 );
42  
43  
44 has hues => (
45 is => 'rw',
46 isa => 'ArrayRef',
47 required => 1,
48 lazy => 1,
49 1 ▶ default => sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
50 1 my $seed = shift->seed_hue;
51 1 [ map { ($seed + $_) % 360 } (0, 45, 75, 15, 60, 30) ]
52 },
53 9 );
54  
55  
56 has shade_order => (
57 is => 'rw',
58 isa => 'ArrayRef',
59 required => 1,
60 9 ▶ default => sub { [1, 3, 0, 2] },
Subroutine __ANON__: called CC=1 SCAR=0.0
61 9 );
62  
63 1 ▶sub _build_color_scheme {
Subroutine _build_color_scheme: called CC=1 SCAR=0.0
64 1 my $self = shift;
65 1 my $scheme = Color::Scheme->new;
66 1 $scheme->scheme('tetrade');
67 1 $scheme->web_safe(1);
68 1 $scheme->distance(1);
69 1 return $scheme;
70 }
71  
72  
73 98 ▶sub next {
Subroutine next: called CC=1 SCAR=0.0
74 98 my $self = shift;
75  
76 98 $self->position($self->position + 1);
77  
78 98 return $self->colors->[$self->position];
79 }
80  
81 # Before we attempt to get the next color, we'll instantiate it if we need it
82 # that way we don't waste a bunch of memory with useless colors.
83 98 ▶before 'next' => sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
84 98 my $self = shift;
85  
86 98 my $pos = $self->position;
87 98 ▶ if(!defined($self->colors->[$pos + 1])) {
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $self->colors->[$pos + 1]) 97 1 98
88 97 $self->add_to_colors($self->allocate_color);
89 }
90 9 };
91  
92  
93 97 ▶sub allocate_color {
Subroutine allocate_color: called CC=1 SCAR=0.0
Pod documented
94 97 my $self = shift;
95  
96 97 my $pos = $self->position + 1;
97 97 my $scheme = $self->color_scheme;
98  
99 97 my $hue_cnt = @{ $self->hues };
100 97 my $hue_pos = int($pos / 4) % $hue_cnt;
101 97 $scheme->from_hue($self->hues->[$hue_pos]);
102  
103 97 my $shade_pos = int($pos / ( $hue_cnt * 4)) % 4;
104 97 my $shade_idx = $self->shade_order->[$shade_pos];
105 97 my $color_idx = $pos % 4;
106  
107 97 my $color_hex = $scheme->colorset->[$color_idx]->[$shade_idx];
108 97 my ($r,$g,$b) = ( map{ hex } ($color_hex =~ /(..)(..)(..)/));
109 97 my $color = Graphics::Color::RGB->new(
110 red => $r / 255,
111 green => $g / 255,
112 blue => $b / 255,
113 alpha => 1,
114 )
115 }
116  
117  
118 0 ▶sub reset {
Subroutine reset: not called CC=1 SCAR=6.9
Pod documented
119 0 my $self = shift;
120  
121 0 $self->position(-1);
122 0 return 1;
123 }
124  
125 9 __PACKAGE__->meta->make_immutable;
126  
127 9 ▶no Moose;
Subroutine BEGIN: called CC=1 SCAR=0.0
128  
129 9 1;
130  
131 __END__
« Clicker/Renderer.pm ↑ Summary Clicker/Decoration/Glass.pm »