File Coverage

blib/lib/Color/Brewer.pm
Criterion Covered Total %
statement 37 38 97.3
branch 5 6 83.3
condition 1 3 33.3
subroutine 10 10 100.0
pod 2 2 100.0
total 55 59 93.2


line stmt bran cond sub pod time code
1             package Color::Brewer;
2              
3 2     2   26605 use strict;
  2         3  
  2         48  
4 2     2   6 use warnings;
  2         2  
  2         36  
5 2     2   5 use utf8;
  2         7  
  2         6  
6              
7 2     2   1129 use JSON;
  2         18944  
  2         5  
8 2     2   973 use File::ShareDir;
  2         8246  
  2         85  
9 2     2   876 use Params::Validate qw(:all);
  2         12412  
  2         276  
10 2     2   1384 use Path::Tiny;
  2         15097  
  2         542  
11              
12             our $VERSION = 0.001;
13              
14             =encoding utf-8
15              
16             =head1 NAME
17              
18             Color::Brewer - Color schemes from Cynthis Brewer's ColorBrewer L
19              
20             =head1 SYNOPSYS
21              
22             use Color::Brewer;
23              
24             my @color_scheme = Color::Brewer::named_color_scheme(name => 'RdBu', number_of_data_classes => 4);
25             my @color_schemes = Color::Brewer::color_schemes(data_nature => 'sequential', number_of_data_classes => 5);
26              
27             =head1 DESCRIPTION
28              
29             Provides color schemes for maps designed by Cynthia Brewer. The color schemes are also suitable for data visualizations.
30              
31             Choosing the best color scheme is easy. Just visit Cynthia Brewer's ColorBrewer L.
32              
33             Also adding a citation in your map, chart, ... would be nice. Something like:
34            
35             Colors from colorbrewer2.org, by Cynthia A. Brewer, Geography, Pennsylvania State University
36              
37             This is suggested in L
38              
39             =cut
40              
41             my $colors;
42              
43             =head1 METHODS
44              
45             =cut
46              
47             sub _color_brewer {
48 4 100   4   8 if ( !defined $colors ) {
49 1         4 $colors = from_json( path( File::ShareDir::dist_file( 'Color-Brewer', 'colorbrewer.json' ) )->slurp_utf8() );
50             }
51 4         1627 return $colors;
52             }
53              
54             =head2 named_color_scheme
55              
56             Get a named color scheme
57              
58             =head3 Parameters
59              
60             =over
61              
62             =item * name:
63              
64             Name of the color scheme: RdBu
65              
66             =item * number_of_data_classes:
67              
68             Number of data classes. Valid range goes from 3 to 12 depending on the scheme.
69              
70             =back
71              
72             =head3 Returns
73              
74             Array with the color scheme or an empty list if there is no such scheme
75              
76             =cut
77              
78             sub named_color_scheme {
79 1     1 1 262 my %params = validate( @_,
80             { name => { type => SCALAR },
81             number_of_data_classes => { type => SCALAR }
82             }
83             );
84 1         5 my ( $name, $number_of_data_classes ) = @params{qw(name number_of_data_classes)};
85              
86 1         2 my $colors = _color_brewer();
87 1 50 33     20 if ( defined $colors->{$name} && defined $colors->{$name}{$number_of_data_classes} ) {
88 1         1 return @{ $colors->{$name}{$number_of_data_classes} };
  1         5  
89             }
90              
91 0         0 return ();
92             }
93              
94             =head2 color_schemes
95              
96             Get the color schemes available
97              
98             =head3 Parameters
99              
100             =over
101              
102             =item * data_nature:
103              
104             Nature of the data: qualitative, sequential or diverging
105              
106             =item * number_of_data_classes:
107              
108             Number of data classes. Valid range goes from 3 to 12 depending on the scheme.
109              
110             =back
111              
112             =head3 Returns
113              
114             Array with all the color schemes matching the options. Every color scheme is an array
115             which elements are html rgb colors like: "rgb(252,141,89)"
116              
117             Array can be empty if none of the color schemes match the options.
118              
119             =cut
120              
121             sub color_schemes {
122 3     3 1 1172 my %params = validate( @_,
123             { data_nature => { type => SCALAR },
124             number_of_data_classes => { type => SCALAR }
125             }
126             );
127              
128 3         12 my ( $data_nature, $number_of_data_classes ) = @params{qw(data_nature number_of_data_classes)};
129 3         5 $data_nature = substr( $data_nature, 0, 3 );
130              
131 8         10 return map { $_->{$number_of_data_classes} }
132 3 100       3 grep { $_->{type} =~ /$data_nature/ && defined $_->{$number_of_data_classes} } values %{ _color_brewer() };
  105         230  
  3         6  
133             }
134              
135             =head1 CREDIT
136              
137             Color Brewer website and color schemes are copyrighted by Cynthia Brewer. With this module is bundled a json file with the color schemes.
138              
139             This product includes color specifications and designs developed by Cynthia Brewer (http://colorbrewer2.org/).
140              
141             Apache-Style Software License for ColorBrewer software and ColorBrewer Color Schemes
142              
143             Copyright (c) 2002 Cynthia Brewer, Mark Harrower, and The Pennsylvania State University.
144              
145             Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License.
146             You may obtain a copy of the License at
147              
148             http://www.apache.org/licenses/LICENSE-2.0
149              
150             Unless required by applicable law or agreed to in writing, software distributed
151             under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
152             CONDITIONS OF ANY KIND, either express or implied. See the License for the
153             specific language governing permissions and limitations under the License.
154              
155             =head1 SEE ALSO
156              
157             L Perl implementation of Color Schemes 2
158              
159             L Set of named colors
160              
161             L Named-color library
162              
163             L Simple creation and manipulation of colors
164              
165             L Conversion between color spaces
166              
167             =head1 AUTHOR
168              
169             Pablo RodrĂ­guez González
170              
171             =head1 LICENSE
172              
173             This work is licensed under the Apache License, Version 2.0.
174              
175             =cut
176              
177             1;