Library/Dictionary.pm

filter: Statementstmt 87.0% 87 / 100 Branchbran 65.6% 21 / 32 Conditioncond 50.0% 3 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 83.3% 15 / 18 Podpod 100% 7 / 7 totaltotal 79.6% 133 / 167 CC 20 SCAR 18.4
CC
20
Cov
80%
color
16.1
_compile
4.0
colors
3.0
CRAP 6.3
« Library/Color.pm ↑ Summary Library.pm »
1 package Color::Library::Dictionary;
2  
3 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 2 ▶use Color::Library;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Color::Library::Color;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 2 ▶use base qw/Class::Data::Inheritable/;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 2 __PACKAGE__->mk_classdata($_) for qw/_self _compiled _color_list _index/;
12  
13 =head1 NAME
14  
15 Color::Library::Dictionary - Color dictionary for Color::Library
16  
17 =cut
18  
19 44 ▶sub _register_dictionary {
Subroutine _register_dictionary: called CC=2 SCAR=6.9
20 44 my $module = my $class = shift;
21 44 my @module = split m/::/, $module;
22  
23 44 my @parent_module = @module;
24 44 my $name = pop @parent_module;
25 44 ▶ @parent_module = qw/Color Library/ if @parent_module == 3; # Color::Library::Dictionary
Branch: 1 branch
BranchTrueFalseTotal
if @parent_module == 3 22 22 44
26 44 my $parent_module = join "::", @parent_module;
27 {
28 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
29 44 ▶ *{"$parent_module\::$name"} = sub {
Subroutine __ANON__: called CC=1 SCAR=0.0
30 45 return $module->_singleton;
31 44 };
32 }
33 44 Color::Library->_register_dictionary($module);
34 }
35  
36 90 ▶sub _parse_id($) {
Subroutine _parse_id: called CC=1 SCAR=0.0
37 90 my $id = shift;
38 90 $id =~ s/::|_|\s+|\//-/g;
39 90 $id = lc $id;
40 90 $id =~ s/^color-library-dictionary-//g;
41 90 return $id;
42 }
43  
44 85 ▶sub _singleton {
Subroutine _singleton: called CC=2 SCAR=6.9
45 85 my $class = shift;
46 85 my $self;
47 85 ▶ return $self if $self = $class->_self;
Branch: 1 branch
BranchTrueFalseTotal
if $self = $class->_self 64 21 85
48 21 $class->_self($self = bless {}, $class);
49 21 return $self;
50 }
51  
52 21 ▶sub _compile {
Subroutine _compile: called CC=4 SCAR=14.0
53 21 my $self = shift;
54 21 ▶ my $class = ref $self || $self;
Condition33%1/3
ref $self || $self
l!l&&r!l&&!r
2100
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table33%1/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
55  
56 21 ▶ return if $self->_compiled;
Branch: 1 branch
BranchTrueFalseTotal
if $self->_compiled 0 21 21
57  
58 21 my $color_list = $self->_load_color_list;
59  
60 21 my $index = {};
61 21 my @color_list;
62 21 my $indice = 0;
63 21 for my $color (@$color_list) {
64 12801 push @color_list, $color = Color::Library::Color->new($color, $self);
65 12801 $index->{id}->{$color->id} = $color;
66 12801 $index->{name}->{$color->name} = $color;
67 12801 $index->{title}->{$color->title} = $color;
68 12801 $index->{hex}->{$color->hex} = $color;
69 12801 $index->{value}->{$color->value} = $color;
70 }
71 21 $self->_index($index);
72 21 $self->_color_list(\@color_list);
73 21 $self->_compiled(1);
74 }
75  
76 =head1 METHODS
77  
78 =over 4
79  
80 =item @colors = $dictionary->colors
81  
82 Returns the list of Color::Library::Color objects contained by $dictionary
83  
84 Will return a list in list context, and a list reference in scalar context
85  
86 =cut
87  
88 86 ▶sub colors {
Subroutine colors: called CC=3 SCAR=11.0
Pod documented
89 86 my $self = shift;
90 86 ▶ $self->_compile unless $self->_compiled;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_compiled 21 65 86
91 86 my @colors = @{ $self->_color_list };
92 86 ▶ return wantarray ? @colors : \@colors;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 86 0 86
93 }
94  
95 =item @names = $dictionary->names
96  
97 =item @names = $dictionary->color_names
98  
99 Returns the list of color names contained by $dictionary
100  
101 Will return a list in list context, and a list reference in scalar context
102  
103 =cut
104  
105 43 ▶sub names {
Subroutine names: called CC=2 SCAR=7.0
Pod documented
106 43 my $self = shift;
107 43 my @names = map { $_->name } $self->colors;
108 43 ▶ return wantarray ? @names : \@names;
Branch: 1 branch
BranchTrueFalseTotal
wantarray ? : 43 0 43
109 }
110 2 *color_names = \&names;
111  
112 =item $color = $dictionary->color( <query> )
113  
114 Returns a Color::Library::Color object of $dictionary found via <query>
115  
116 A query can be any of the following:
117  
118 =over 4
119  
120 =item color name
121  
122 A color name is like C<blue> or C<bleached-almond>
123  
124 =item color title
125  
126 A color title is like C<Dark Green-Teal>
127  
128 =item color id
129  
130 A color id is in the form of <dictionary_id>:<color_name>, for example: C<x11:azure1>
131  
132 =back
133  
134 =cut
135  
136 20 ▶sub color {
Subroutine color: called CC=11 SCAR=27.8
Pod documented
137 20 my $self = shift;
138 20 my $query = shift;
139  
140 20 ▶ return unless defined $query;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $query 0 20 20
141  
142 20 ▶ unless (ref $query) {
Branch: 1 branch
BranchTrueFalseTotal
unless (ref $query) 20 0 20
143 20 $query =~ s/^\s*//;
144 20 $query =~ s/\s*$//;
145 }
146  
147 20 ▶ $self->_compile unless $self->_compiled;
Branch: 1 branch
BranchTrueFalseTotal
unless $self->_compiled 0 20 20
148  
149 20 my $color;
150  
151 20 ▶ if (ref $query eq "ARRAY") {
Branch: 3 branches
BranchTrueFalseTotal
if (ref $query eq 'ARRAY') { } 0 20 20
elsif ($query =~ /^\#?([\da-f][\da-f])([\da-f][\da-f])([\da-f][\da-f])/i) { } 0 20 20
elsif ($query =~ /^\#([\da-f])([\da-f])([\da-f])$/i) { } 0 20 20
152 0 $query = Color::Library::Color::rgb2hex $query;
153 0 return $color = $self->_index->{hex}->{$query};
154 }
155 elsif ($query =~ /^\#?([\da-f][\da-f])([\da-f][\da-f])([\da-f][\da-f])/i) {
156 0 return (hex($1), hex($2), hex($3), 255);
157 0 $query = lc($1 . $2 . $3);
158 0 return $color = $self->_index->{hex}->{$query};
159 }
160 elsif ($query =~ /^\#([\da-f])([\da-f])([\da-f])$/i) {
161 0 $query = lc($1 . $1 . $2 . $2 . $3 . $3);
162 0 return $color = $self->_index->{hex}->{$query};
163 }
164  
165 20 ▶ return $color if $color = $self->_index->{title}->{$query};
Branch: 1 branch
BranchTrueFalseTotal
if $color = $self->_index->{"title"}{$query} 5 15 20
166  
167 15 $query = lc $query;
168  
169 15 ▶ return $color if $color = $self->_index->{id}->{$query};
Branch: 1 branch
BranchTrueFalseTotal
if $color = $self->_index->{"id"}{$query} 0 15 15
170  
171 15 $query =~ s/[^\w]//g;
172  
173 15 ▶ return $color if $color = $self->_index->{name}->{$query};
Branch: 1 branch
BranchTrueFalseTotal
if $color = $self->_index->{"name"}{$query} 1 14 15
174  
175 14 ▶ return $color if $color = $self->_index->{value}->{$query};
Branch: 1 branch
BranchTrueFalseTotal
if $color = $self->_index->{"value"}{$query} 0 14 14
176  
177 14 return;
178 }
179  
180 =item $id = $dictionary->id
181  
182 =item $name = $dictionary->name
183  
184 Returns the id (name) of $dictionary, e.g.
185  
186 svg
187 x11
188 vaccc
189 nbs-iscc-f
190  
191 =cut
192  
193 50 ▶sub id {
Subroutine id: called CC=2 SCAR=7.1
Pod documented
194 50 my $self = shift;
195 50 ▶ return _parse_id(ref $self || $self);
Condition66%2/3
ref $self || $self
l!l&&r!l&&!r
6440
MC/DC0%0/2
ref $self || $self
ref $self $self
Truth table66%2/3
A || B
A= ref $selfB= $self
ABresult
1X1
011
000
196 }
197 2 *name = \&id;
198  
199 =item $title = $dictionary->title
200  
201 Returns the title of $dictionary, e.g.
202  
203 SVG
204 X11
205 VACCC
206 NBS/ISCC F
207  
208 =cut
209  
210 0 ▶sub title {
Subroutine title: not called CC=1 SCAR=6.9
Pod documented
211 0 my $self = shift;
212 0 return $self->_description->{title};
213 }
214  
215 =item $subtitle = $dictionary->subtitle
216  
217 Returns the subtitle of $dictionary, if any
218  
219 =cut
220  
221 0 ▶sub subtitle {
Subroutine subtitle: not called CC=1 SCAR=6.9
Pod documented
222 0 my $self = shift;
223 0 return $self->_description->{subtitle};
224 }
225  
226 =item $description = $dictionary->description
227  
228 Returns the description of $dictionary, if any
229  
230 =cut
231  
232 0 ▶sub description {
Subroutine description: not called CC=1 SCAR=6.9
Pod documented
233 0 my $self = shift;
234 0 return $self->_description->{description};
235 }
236  
237  
238 2 1;
« Library/Color.pm ↑ Summary Library.pm »