File Coverage

blib/lib/Color/Library.pm
Criterion Covered Total %
statement 44 50 88.0
branch 13 18 72.2
condition n/a
subroutine 8 8 100.0
pod 3 3 100.0
total 68 79 86.0


line stmt bran cond sub pod time code
1             package Color::Library;
2             {
3             $Color::Library::VERSION = '0.021';
4             }
5             # ABSTRACT: An easy-to-use and comprehensive named-color library
6              
7 2     2   22520 use warnings;
  2         5  
  2         77  
8 2     2   11 use strict;
  2         4  
  2         89  
9              
10 2     2   1940 use Module::Pluggable search_path => 'Color::Library::Dictionary', sub_name => '_load_dictionaries', require => 1;
  2         28308  
  2         17  
11 2     2   1681 use Color::Library::Dictionary;
  2         7  
  2         1211  
12             __PACKAGE__->_load_dictionaries;
13              
14             my %dictionary;
15             sub _register_dictionary {
16 44     44   71 my $self = shift;
17 44         62 my $dictionary = shift;
18 44         302 $dictionary{$dictionary->id} = $dictionary;
19             }
20              
21              
22             sub dictionary {
23 20     20 1 5559 my $self = shift;
24 20         46 return ($self->dictionaries(shift))[0];
25             }
26              
27              
28             sub dictionaries {
29 21     21 1 54 my $self = shift;
30 21 50       41 local @_ = keys %dictionary unless @_;
31 21         31 @_ = map { Color::Library::Dictionary::_parse_id $_ } @_;
  40         96  
32 21 50       52 if (wantarray) {
33 21         53 return map { $_->_singleton } @dictionary{@_};
  40         344  
34             }
35             else {
36 0         0 my %_dictionary;
37 0         0 @_dictionary{@_} = map { $_->_singleton } @dictionary{@_};
  0         0  
38 0         0 return \%_dictionary;
39             }
40             }
41              
42              
43             # FUTURE Make this better
44             my @dictionary_search_order = (qw/svg x11 html ie mozilla netscape windows vaccc nbs-iscc/,
45             map { "nbs-iscc-$_" } qw/a b f h m p r rc s sc tc/);
46              
47             sub color {
48 4     4 1 801 my $self = shift;
49              
50 4         5 my @colors;
51              
52             # Default dictionaries to search, in order
53 4         34 my @dictionaries = @dictionary_search_order;
54              
55             # Can also pass in a default array of dictionary ids to search
56 4 100       107 @dictionaries = @{ shift() } if ref $_[0] eq "ARRAY";
  2         7  
57              
58 4         7 my $query_;
59 4         7 for my $query (@_) {
60 6         11 $query_ = $query;
61 6         17 my @dictionaries = @dictionaries;
62              
63 6 100       19 if ($query =~ m/:/) {
64             # Looks like the query contains at least one dictionary id
65              
66 1         5 my ($dictionaries, $name) = split m/:/, $query, 2;
67 1 50       5 unless (defined $name) {
68 0         0 $name = $dictionaries;
69 0         0 undef $dictionaries
70             }
71 1 50       7 @dictionaries = split m/,/, $dictionaries if defined $dictionaries;
72 1         2 $query_ = $name;
73             }
74              
75 6         7 my $color;
76 6         8 for my $dictionary_id (@dictionaries) {
77 19 50       43 next unless my $dictionary = $self->dictionary($dictionary_id);
78 19 100       301 last if $color = $dictionary->color($query_);
79             }
80 6         37 push @colors, $color;
81             }
82              
83 4 100       24 return wantarray ? @colors : $colors[0];
84             }
85             *colors = \&color;
86             *colour = \&color;
87             *colours = \&color;
88              
89              
90             1;
91              
92             __END__