File Coverage

blib/lib/Color/Palette.pm
Criterion Covered Total %
statement 1 3 33.3
branch n/a
condition n/a
subroutine 1 1 100.0
pod n/a
total 2 4 50.0


line stmt bran cond sub pod time code
1             package Color::Palette;
2             {
3             $Color::Palette::VERSION = '0.100003';
4             }
5 1     1   2659 use Moose;
  0            
  0            
6             # ABSTRACT: a set of named colors
7              
8             use MooseX::Types::Moose qw(Str HashRef ArrayRef);
9             use Color::Palette::Types qw(RecursiveColorDict ColorDict Color);
10              
11              
12             has colors => (
13             is => 'bare',
14             isa => RecursiveColorDict,
15             coerce => 1,
16             required => 1,
17             reader => '_colors',
18             );
19              
20              
21             has _resolved_colors => (
22             is => 'ro',
23             isa => ColorDict,
24             lazy => 1,
25             traits => [ 'Hash' ],
26             coerce => 1,
27             required => 1,
28             builder => '_build_resolved_colors',
29             handles => {
30             has_color => 'exists',
31             _get_color => 'get',
32             color_names => 'keys',
33             },
34             );
35              
36             sub _build_resolved_colors {
37             my ($self) = @_;
38              
39             my $input = $self->_colors;
40              
41             my %output;
42              
43             for my $key (keys %$input) {
44             my $value = $input->{ $key };
45             next unless ref $value;
46              
47             $output{ $key } = $value;
48             }
49              
50             for my $key (keys %$input) {
51             my $value = $input->{ $key };
52             next if ref $value;
53              
54             my %seen;
55             my $curr = $key;
56             REDIR: while (1) {
57             Carp::confess "$key refers to missing color $curr"
58             unless exists $input->{$curr};
59              
60             if ($output{ $curr }) {
61             $output{ $key } = $output{ $curr };
62             last REDIR;
63             }
64              
65             $curr = $input->{ $curr };
66             Carp::confess "looping at $curr" if $seen{ $curr }++;
67             }
68             }
69              
70             return \%output;
71             }
72              
73              
74             sub color {
75             my ($self, $name) = @_;
76             confess("no color named $name") unless my $color = $self->_get_color($name);
77              
78             return $color;
79             }
80              
81              
82             sub as_css_hash {
83             my ($self) = @_;
84             my $output = {};
85             $output->{$_} = $self->color($_)->as_css_hex for $self->color_names;
86             return $output;
87             }
88              
89              
90             sub as_strict_css_hash {
91             my ($self) = @_;
92             my $hashref = $self->as_css_hash;
93             tie my %hash, 'Color::Palette::TiedStrictHash';
94             %hash = %$hashref;
95             return \%hash;
96             }
97              
98              
99             sub optimize_for {
100             my $self = shift;
101             # warn deprecated
102             $self->optimized_for(@_);
103             }
104              
105             sub optimized_for {
106             my ($self, $checker) = @_;
107              
108             my $required_colors = $checker->required_colors;
109              
110             my %new_palette;
111             for my $name (@{ $checker->required_colors }) {
112             $new_palette{ $name } = $self->color($name);
113             }
114              
115             (ref $self)->new({
116             colors => \%new_palette,
117             });
118             }
119              
120             {
121             package
122             Color::Palette::TiedStrictHash;
123             use Tie::Hash;
124             BEGIN { our @ISA = qw(Tie::StdHash) }
125             sub FETCH {
126             my ($self, $key) = @_;
127             die "no entry in palette hash for key $key"
128             unless $self->EXISTS($key);
129             return $self->SUPER::FETCH($key);
130             }
131             }
132              
133             1;
134              
135             __END__
136              
137             =pod
138              
139             =encoding UTF-8
140              
141             =head1 NAME
142              
143             Color::Palette - a set of named colors
144              
145             =head1 VERSION
146              
147             version 0.100003
148              
149             =head1 DESCRIPTION
150              
151             The libraries in the Color-Palette distribution are meant to make it easy to
152             build sets of named colors, and to write applications that can define and
153             validate the color names they required.
154              
155             For example, a color palette might contain the following data:
156              
157             highlights => #f0f000
158             background => #333
159             sidebarBackground => #88d
160             sidebarText => 'highlights'
161             sidebarBoder => 'sidebarText'
162              
163             Colors can be defined by a color specifier (a L<Graphics::Color> object,
164             a CSS-style hex triple, or an arrayref of RGB values) or by a name of another
165             color that appears in the palette. If colors are defined in terms of another
166             color that doesn't exist, an exception will be raised.
167              
168             Applications that wish to use color palettes can provide schemas that define
169             the names they expect to be present in a palette. These schemas are
170             L<Color::Palette::Schema> objects.
171              
172             A palette can be checked against a schema with the schema's C<check> method, or
173             may be reduced to the minimal set of colors needed to satisfy the schema with
174             the palette's C<optimized_for> method.
175              
176             =head1 ATTRIBUTES
177              
178             =head2 colors
179              
180             This attribute is a hashref. Keys are color names and values are either Color
181             objects or names of other colors. To get at the color object for a name
182             consult the C<L</color>> method.
183              
184             =head1 METHODS
185              
186             =head2 color
187              
188             my $color_obj = $palette->color('extremeHighlight');
189              
190             This method will return the Color object to be used for the given name.
191              
192             =head2 color_names
193              
194             my @names = $palette->color_names;
195              
196             This method returns a list of all color names the object knows about.
197              
198             =head2 as_css_hash
199              
200             my $triple_for = $palette->as_css_hash
201              
202             This method returns a hashref. Every color name known to the palette has an
203             entry, and the value is the CSS-safe hex string for the resolved color. For
204             example, the output for the color scheme in the L</DESCRIPTION> section would
205             be:
206              
207             {
208             highlights => '#f0f000',
209             background => '#333333',
210             sidebarBackground => #8888dd',
211             sidebarText => #f0f000',
212             sidebarBoder => #f0f000',
213             }
214              
215             =head2 as_strict_css_hash
216              
217             my $hashref = $palette->as_strict_css_hash;
218              
219             This method behaves just like C<L</as_css_hash>>, but the returned hashref is
220             tied so that trying to read values for keys that do not exist is fatal. The
221             hash may also become read-only in the future.
222              
223             =head2 optimized_for
224              
225             my $optimized_palette = $palette->optimized_for($schema);
226              
227             This method returns a new palette containing only the colors needed to fulfill
228             the requirements of the given schema. This is useful for reducing a large
229             palette to the small set that must be embedded in a document.
230              
231             C<optimize_for> redispatches to this method for historical reasons.
232              
233             =begin :private
234              
235             =attr _resolved_colors
236              
237             This attribute is just like C<colors>, but all values are Color objects.
238              
239             =end :private
240              
241             =head1 AUTHOR
242              
243             Ricardo SIGNES <rjbs@cpan.org>
244              
245             =head1 COPYRIGHT AND LICENSE
246              
247             This software is copyright (c) 2013 by Ricardo SIGNES.
248              
249             This is free software; you can redistribute it and/or modify it under
250             the same terms as the Perl 5 programming language system itself.
251              
252             =cut