| 1 | package Color::Library; | ||||||||||
| 2 | { | ||||||||||
| 3 | 2 | $Color::Library::VERSION = '0.021'; | |||||||||
| 4 | } | ||||||||||
| 5 | # ABSTRACT: An easy-to-use and comprehensive named-color library | ||||||||||
| 6 | |||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 2 | ▶ | use Module::Pluggable search_path => 'Color::Library::Dictionary', sub_name => '_load_dictionaries', require => 1; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | 2 | ▶ | use Color::Library::Dictionary; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 12 | 2 | __PACKAGE__->_load_dictionaries; | |||||||||
| 13 | |||||||||||
| 14 | 2 | my %dictionary; | |||||||||
| 15 | 44 | ▶ | sub _register_dictionary { | ||||||||
|
Subroutine
_register_dictionary: called CC=1 SCAR=0.0
| |||||||||||
| 16 | 44 | my $self = shift; | |||||||||
| 17 | 44 | my $dictionary = shift; | |||||||||
| 18 | 44 | $dictionary{$dictionary->id} = $dictionary; | |||||||||
| 19 | } | ||||||||||
| 20 | |||||||||||
| 21 | |||||||||||
| 22 | 20 | ▶ | sub dictionary { | ||||||||
|
Subroutine
dictionary: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 23 | 20 | my $self = shift; | |||||||||
| 24 | 20 | return ($self->dictionaries(shift))[0]; | |||||||||
| 25 | } | ||||||||||
| 26 | |||||||||||
| 27 | |||||||||||
| 28 | 21 | ▶ | sub dictionaries { | ||||||||
|
Subroutine
dictionaries: called CC=3 SCAR=12.7
Pod
documented
| |||||||||||
| 29 | 21 | my $self = shift; | |||||||||
| 30 | 21 | ▶ | local @_ = keys %dictionary unless @_; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 31 | 21 | @_ = map { Color::Library::Dictionary::_parse_id $_ } @_; | |||||||||
| 32 | 21 | ▶ | if (wantarray) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 33 | 21 | return map { $_->_singleton } @dictionary{@_}; | |||||||||
| 34 | } | ||||||||||
| 35 | else { | ||||||||||
| 36 | 0 | my %_dictionary; | |||||||||
| 37 | 0 | @_dictionary{@_} = map { $_->_singleton } @dictionary{@_}; | |||||||||
| 38 | 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 | 2 | map { "nbs-iscc-$_" } qw/a b f h m p r rc s sc tc/); | |||||||||
| 46 | |||||||||||
| 47 | 4 | ▶ | sub color { | ||||||||
|
Subroutine
color: called CC=10 SCAR=23.3
Pod
documented
| |||||||||||
| 48 | 4 | my $self = shift; | |||||||||
| 49 | |||||||||||
| 50 | 4 | my @colors; | |||||||||
| 51 | |||||||||||
| 52 | # Default dictionaries to search, in order | ||||||||||
| 53 | 4 | my @dictionaries = @dictionary_search_order; | |||||||||
| 54 | |||||||||||
| 55 | # Can also pass in a default array of dictionary ids to search | ||||||||||
| 56 | 4 | ▶ | @dictionaries = @{ shift() } if ref $_[0] eq "ARRAY"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | |||||||||||
| 58 | 4 | my $query_; | |||||||||
| 59 | 4 | for my $query (@_) { | |||||||||
| 60 | 6 | $query_ = $query; | |||||||||
| 61 | 6 | my @dictionaries = @dictionaries; | |||||||||
| 62 | |||||||||||
| 63 | 6 | ▶ | if ($query =~ m/:/) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 64 | # Looks like the query contains at least one dictionary id | ||||||||||
| 65 | |||||||||||
| 66 | 1 | my ($dictionaries, $name) = split m/:/, $query, 2; | |||||||||
| 67 | 1 | ▶ | unless (defined $name) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 0 | $name = $dictionaries; | |||||||||
| 69 | 0 | undef $dictionaries | |||||||||
| 70 | } | ||||||||||
| 71 | 1 | ▶ | @dictionaries = split m/,/, $dictionaries if defined $dictionaries; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | 1 | $query_ = $name; | |||||||||
| 73 | } | ||||||||||
| 74 | |||||||||||
| 75 | 6 | my $color; | |||||||||
| 76 | 6 | for my $dictionary_id (@dictionaries) { | |||||||||
| 77 | 19 | ▶ | next unless my $dictionary = $self->dictionary($dictionary_id); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 78 | 19 | ▶ | last if $color = $dictionary->color($query_); | ||||||||
|
Branch: 1 branch
| |||||||||||
| 79 | } | ||||||||||
| 80 | 6 | push @colors, $color; | |||||||||
| 81 | } | ||||||||||
| 82 | |||||||||||
| 83 | 4 | ▶ | return wantarray ? @colors : $colors[0]; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | } | ||||||||||
| 85 | 2 | *colors = \&color; | |||||||||
| 86 | 2 | *colour = \&color; | |||||||||
| 87 | 2 | *colours = \&color; | |||||||||
| 88 | |||||||||||
| 89 | |||||||||||
| 90 | 2 | 1; | |||||||||
| 91 | |||||||||||
| 92 | __END__ | ||||||||||