| 1 | # You may distribute under the terms of either the GNU General Public License | ||||||||||
| 2 | # or the Artistic License (the same terms as Perl itself) | ||||||||||
| 3 | # | ||||||||||
| 4 | # (C) Paul Evans, 2009-2014 -- leonerd@leonerd.org.uk | ||||||||||
| 5 | |||||||||||
| 6 | package Convert::Color::Library; | ||||||||||
| 7 | |||||||||||
| 8 | 4 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 4 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 4 | ▶ | use base qw( Convert::Color::RGB8 ); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 4 | __PACKAGE__->register_color_space( 'lib' ); | |||||||||
| 13 | |||||||||||
| 14 | 4 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 15 | |||||||||||
| 16 | 4 | our $VERSION = '0.05'; | |||||||||
| 17 | |||||||||||
| 18 | =head1 NAME | ||||||||||
| 19 | |||||||||||
| 20 | C<Convert::Color::Library> - named lookup of colors from C<Color::Library> | ||||||||||
| 21 | |||||||||||
| 22 | =head1 SYNOPSIS | ||||||||||
| 23 | |||||||||||
| 24 | Directly: | ||||||||||
| 25 | |||||||||||
| 26 | use Convert::Color::Library; | ||||||||||
| 27 | |||||||||||
| 28 | my $red = Convert::Color::Library->new( 'red' ); | ||||||||||
| 29 | |||||||||||
| 30 | # Only use the SVG dictionary | ||||||||||
| 31 | my $brown = Convert::Color::Library->new( 'svg/brown' ); | ||||||||||
| 32 | |||||||||||
| 33 | # Use either HTML or SVG dictionary | ||||||||||
| 34 | my $pink = Convert::Color::Library->new( 'html,svg/pink' ); | ||||||||||
| 35 | |||||||||||
| 36 | Via L<Convert::Color>: | ||||||||||
| 37 | |||||||||||
| 38 | use Convert::Color; | ||||||||||
| 39 | |||||||||||
| 40 | my $cyan = Convert::Color->new( 'lib:cyan' ); | ||||||||||
| 41 | |||||||||||
| 42 | my $darkcyan = Convert::Color->new( 'lib:windows/darkcyan' ); | ||||||||||
| 43 | |||||||||||
| 44 | =head1 DESCRIPTION | ||||||||||
| 45 | |||||||||||
| 46 | This subclass of L<Convert::Color::RGB8> provides lookup of color names | ||||||||||
| 47 | using Robert Krimen's L<Color::Library> module. It therefore provides | ||||||||||
| 48 | convenient access to named colours in many dictionaries, such as SVG, X11 and | ||||||||||
| 49 | HTML. | ||||||||||
| 50 | |||||||||||
| 51 | =cut | ||||||||||
| 52 | |||||||||||
| 53 | =head1 CONSTRUCTOR | ||||||||||
| 54 | |||||||||||
| 55 | =cut | ||||||||||
| 56 | |||||||||||
| 57 | =head2 $color = Convert::Color::Library->new( $name ) | ||||||||||
| 58 | |||||||||||
| 59 | Returns a new object to represent the named color. | ||||||||||
| 60 | |||||||||||
| 61 | If the name is of the form | ||||||||||
| 62 | |||||||||||
| 63 | dicts/name | ||||||||||
| 64 | |||||||||||
| 65 | Then C<dicts> is parsed as a comma-separated list of dictionary names to pass | ||||||||||
| 66 | to C<Color::Library>. | ||||||||||
| 67 | |||||||||||
| 68 | =cut | ||||||||||
| 69 | |||||||||||
| 70 | sub new | ||||||||||
| 71 | 22 | ▶ | { | ||||||||
|
Subroutine
new: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||
| 72 | 22 | my $class = shift; | |||||||||
| 73 | |||||||||||
| 74 | 22 | require Color::Library; | |||||||||
| 75 | |||||||||||
| 76 | 22 | ▶ | @_ == 1 or | ||||||||
|
Branch: 1 branch
| |||||||||||
| 77 | croak "usage: Convert::Color::Library->new( NAME )"; | ||||||||||
| 78 | |||||||||||
| 79 | 22 | my ( $name ) = @_; | |||||||||
| 80 | |||||||||||
| 81 | 22 | my $color; | |||||||||
| 82 | |||||||||||
| 83 | 22 | ▶ | if( $name =~ m{^(.*)/(.*)$} ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | 20 | ( my $dicts, $name ) = ( $1, $2 ); | |||||||||
| 85 | |||||||||||
| 86 | 20 | $color = Color::Library->color( [ split m/,/, $dicts ], $name ); | |||||||||
| 87 | } | ||||||||||
| 88 | else { | ||||||||||
| 89 | 2 | $color = Color::Library->color( $name ); | |||||||||
| 90 | } | ||||||||||
| 91 | |||||||||||
| 92 | 22 | ▶ | defined $color or croak "No such library color named '$name'"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | 22 | my $self = $class->SUPER::new( $color->rgb ); | |||||||||
| 94 | |||||||||||
| 95 | 22 | $self->[3] = $color->name; | |||||||||
| 96 | 22 | $self->[4] = $color->dictionary->name; | |||||||||
| 97 | |||||||||||
| 98 | 22 | return $self; | |||||||||
| 99 | } | ||||||||||
| 100 | |||||||||||
| 101 | =head1 METHODS | ||||||||||
| 102 | |||||||||||
| 103 | =cut | ||||||||||
| 104 | |||||||||||
| 105 | =head2 $name = $color->name | ||||||||||
| 106 | |||||||||||
| 107 | =head2 $dict = $color->dict | ||||||||||
| 108 | |||||||||||
| 109 | Returns the name of the color within its dictionary, and the name of the | ||||||||||
| 110 | dictionary itself. | ||||||||||
| 111 | |||||||||||
| 112 | =cut | ||||||||||
| 113 | |||||||||||
| 114 | 7 | ▶ | sub name { shift->[3] } | ||||||||
|
Subroutine
name: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 115 | 4 | ▶ | sub dict { shift->[4] } | ||||||||
|
Subroutine
dict: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 116 | |||||||||||
| 117 | =head1 TODO | ||||||||||
| 118 | |||||||||||
| 119 | =over 4 | ||||||||||
| 120 | |||||||||||
| 121 | =item * | ||||||||||
| 122 | |||||||||||
| 123 | Consider an API for getting the list of dictionary names and colour names. | ||||||||||
| 124 | That said, it's easy enough to do directly to C<Color::Library>, so maybe not | ||||||||||
| 125 | needed. | ||||||||||
| 126 | |||||||||||
| 127 | =item * | ||||||||||
| 128 | |||||||||||
| 129 | Expose other dictionaries (SVG? Windows?) as named spaces, like the HTML one. | ||||||||||
| 130 | |||||||||||
| 131 | =back | ||||||||||
| 132 | |||||||||||
| 133 | =head1 SEE ALSO | ||||||||||
| 134 | |||||||||||
| 135 | =over 4 | ||||||||||
| 136 | |||||||||||
| 137 | =item * | ||||||||||
| 138 | |||||||||||
| 139 | L<Convert::Color> - color space conversions | ||||||||||
| 140 | |||||||||||
| 141 | =item * | ||||||||||
| 142 | |||||||||||
| 143 | L<Color::Library> - An easy-to-use and comprehensive named-color library | ||||||||||
| 144 | |||||||||||
| 145 | =item * | ||||||||||
| 146 | |||||||||||
| 147 | L<Convert::Color::HTML> - color conversion using C<Color::Library::Dictionary::HTML> | ||||||||||
| 148 | |||||||||||
| 149 | =back | ||||||||||
| 150 | |||||||||||
| 151 | =head1 AUTHOR | ||||||||||
| 152 | |||||||||||
| 153 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||
| 154 | |||||||||||
| 155 | =cut | ||||||||||
| 156 | |||||||||||
| 157 | 4 | 0x55AA; | |||||||||