Debian Codegraph @

lib/Color/Library.pm

raw
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 use warnings;
8 use strict;
9
10 use Module::Pluggable search_path => 'Color::Library::Dictionary', sub_name => '_load_dictionaries', require => 1;
11 use Color::Library::Dictionary;
12 __PACKAGE__->_load_dictionaries;
13
14 my %dictionary;
15 sub _register_dictionary {
16 my $self = shift;
17 my $dictionary = shift;
18 $dictionary{$dictionary->id} = $dictionary;
19 }
20
21
22 sub dictionary {
23 my $self = shift;
24 return ($self->dictionaries(shift))[0];
25 }
26
27
28 sub dictionaries {
29 my $self = shift;
30 local @_ = keys %dictionary unless @_;
31 @_ = map { Color::Library::Dictionary::_parse_id $_ } @_;
32 if (wantarray) {
33 return map { $_->_singleton } @dictionary{@_};
34 }
35 else {
36 my %_dictionary;
37 @_dictionary{@_} = map { $_->_singleton } @dictionary{@_};
38 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 my $self = shift;
49
50 my @colors;
51
52 # Default dictionaries to search, in order
53 my @dictionaries = @dictionary_search_order;
54
55 # Can also pass in a default array of dictionary ids to search
56 @dictionaries = @{ shift() } if ref $_[0] eq "ARRAY";
57
58 my $query_;
59 for my $query (@_) {
60 $query_ = $query;
61 my @dictionaries = @dictionaries;
62
63 if ($query =~ m/:/) {
64 # Looks like the query contains at least one dictionary id
65
66 my ($dictionaries, $name) = split m/:/, $query, 2;
67 unless (defined $name) {
68 $name = $dictionaries;
69 undef $dictionaries
70 }
71 @dictionaries = split m/,/, $dictionaries if defined $dictionaries;
72 $query_ = $name;
73 }
74
75 my $color;
76 for my $dictionary_id (@dictionaries) {
77 next unless my $dictionary = $self->dictionary($dictionary_id);
78 last if $color = $dictionary->color($query_);
79 }
80 push @colors, $color;
81 }
82
83 return wantarray ? @colors : $colors[0];
84 }
85 *colors = \&color;
86 *colour = \&color;
87 *colours = \&color;
88
89
90 1;
91
92 __END__
93 =pod
94
95 =head1 NAME
96
97 Color::Library - An easy-to-use and comprehensive named-color library
98
99 =head1 VERSION
100
101 version 0.021
102
103 =head1 SYNOPSIS
104
105 use Color::Library;
106
107 # Search for a sea blue color
108 my $seablue = Color::Library->color("seablue");
109
110 # Search for a grey73 in the 'svg' and 'x11' dictionaries only
111 my $grey73 = Color::Library->colour([qw/svg x11/] => "grey73");
112
113 # Find a bunch of colors at the same time
114 my ($red, $green, $blue) = Color::Library->colors(qw/red green blue/);
115
116 # Fetch the named color "aliceblue" from the SVG dictionary
117 my $color = Color::Library->SVG->color("aliceblue");
118
119 # Prints out "aliceblue is #ff08ff"
120 print $color->name, "is ", $color, "\n";
121
122 # Get a list of names in the svg dictionary
123 my @names = Color::Library->SVG->names;
124
125 # Get a list of colors in the x11 dictionary
126 my @colors = Color::Library->dictionary('x11')->colors;
127
128 =head1 DESCRIPTION
129
130 Color::Library is an easy-to-use and comprehensive named-color dictionary. Currently provides coverage for www (svg, html, css) colors, x11 colors, and more.
131
132 =head1 DICTIONARIES
133
134 The following dictionaries are available in this distribution:
135
136 Color::Library::Dictionary::SVG - Colors from the SVG specification
137 Color::Library::Dictionary::X11 - Colors for the X11 Window System (rgb.txt)
138 Color::Library::Dictionary::HTML - Colors from the HTML 4.0 specification
139 Color::Library::Dictionary::IE - Colors recognized by Internet Explorer
140 Color::Library::Dictionary::Mozilla - Colors recognized by Mozilla (ColorNames.txt)
141 Color::Library::Dictionary::Netscape - Colors recognized by Netscape
142 Color::Library::Dictionary::Windows - Colors from the Windows system palette
143 Color::Library::Dictionary::VACCC - VisiBone Anglo-Centric Color Code
144 Color::Library::Dictionary::NBS_ISCC - Centroids of the NBS/ISCC catalog
145 Color::Library::Dictionary::NBS_ISCC::A - Dye Colors
146 Color::Library::Dictionary::NBS_ISCC::B - Colour Terminology in Biology
147 Color::Library::Dictionary::NBS_ISCC::F - Colors; (for) Ready-Mixed Paints
148 Color::Library::Dictionary::NBS_ISCC::H - Horticultural Colour Charts
149 Color::Library::Dictionary::NBS_ISCC::M - Dictionary of Color
150 Color::Library::Dictionary::NBS_ISCC::P - Plochere Color System
151 Color::Library::Dictionary::NBS_ISCC::R - Color Standards and Color Nomenclature
152 Color::Library::Dictionary::NBS_ISCC::RC - Rock-Color Chart
153 Color::Library::Dictionary::NBS_ISCC::S - Postage-Stamp Color Names
154 Color::Library::Dictionary::NBS_ISCC::SC - Soil Color Charts
155 Color::Library::Dictionary::NBS_ISCC::TC - Standard Color Card of America
156
157 You can see a list of colors in any of these by reading their perldoc. For example:
158
159 perldoc Color::Library::Dictionary::VACCC
160
161 If you have any suggestions for more color dictionaries to integrate, contact me.
162
163 =head1 METHODS
164
165 =over 4
166
167 =item $dictionary = Color::Library->dictionary( <dictionary_id> )
168
169 Returns a Color::Library::Dictionary object corresponding to <dictionary_id>
170
171 =item @dictionaries = Color::Library->dictionaries
172
173 =item @dictionaries = Color::Library->dictionaries( <dictionary_id>, <dictionary_id>, ... )
174
175 =item $dictionaries = Color::Library->dictionaries( <dictionary_id>, <dictionary_id>, ... )
176
177 In list context, returns a list of Color::Library::Dictionary objects (for each <dictionary_id> passed in
178
179 In scalar context, returns a hash of Color::Library::Dictionary objects mapping a dictionary id to a dictionary
180
181 When called without arguments, the method will return all dictionaries
182
183 =item $color = Color::Library->color( <query> )
184
185 Returns a Color::Library::Color object found via <query>
186
187 A query can be any of the following:
188
189 =over 4
190
191 =item color name
192
193 A color name is like C<blue> or C<bleached-almond>
194
195 =item color title
196
197 A color title is like C<Dark Green-Teal>
198
199 =item color id
200
201 A color id is in the form of <dictionary_id>:<color_name>, for example: C<x11:azure1>
202
203 =back
204
205 =item color( <query>, <query>, ... )
206
207 In list context, returns a list of Color::Library::Color objects corresponding to each <query>
208
209 In scalar context, just returns the first <query>
210
211 =item color( <dictionaries>, <query>, ... )
212
213 If an array reference is passed as the first argument, then this indicates that the array is a list of dictionary ids to search
214 through (in order):
215
216 # Search in the svg and x11 dictionaries for a match
217 my $blue = Color::Library->color([qw/svg x11/], "blue");
218
219 # Will not find "aquamarine1" in the svg dictionary, so it will try the x11 dictionary
220 my $aquamarine1 = Color::Library->color([qw/svg x11/], "aquamarine1");
221
222 =item $color = Color::Library->colors
223
224 =item $color = Color::Library->colour
225
226 =item $color = Color::Library->colours
227
228 All are aliases for the above color method
229
230 =back
231
232 =head1 ABOUT
233
234 This package was inspired by Graphics::ColorNames, and covers much of the same ground. However, I found the Graphics::ColorNames interface difficult to use. I also wanted to list colors directly in the perldoc, which this package does.
235
236 =head1 SEE ALSO
237
238 L<Graphics::ColorNames>
239
240 =head1 AUTHOR
241
242 Robert Krimen <[email protected]>
243
244 =head1 COPYRIGHT AND LICENSE
245
246 This software is copyright (c) 2011 by Robert Krimen.
247
248 This is free software; you can redistribute it and/or modify it under
249 the same terms as the Perl 5 programming language system itself.
250
251 =cut
252