| 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, 2014 -- leonerd@leonerd.org.uk | ||||||||||||||||||||
| 5 | |||||||||||||||||||||
| 6 | package Convert::Color::HTML; | ||||||||||||||||||||
| 7 | |||||||||||||||||||||
| 8 | 2 | ▶ | use strict; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 9 | 2 | ▶ | use warnings; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 2 | ▶ | use base qw( Convert::Color::Library ); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | |||||||||||||||||||||
| 12 | # Ensure that Color::Library::Dictionary::HTML is loaded | ||||||||||||||||||||
| 13 | 2 | ▶ | use Color::Library; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | |||||||||||||||||||||
| 15 | 2 | __PACKAGE__->register_color_space( 'html' ); | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 2 | our $VERSION = '0.05'; | |||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | =head1 NAME | ||||||||||||||||||||
| 20 | |||||||||||||||||||||
| 21 | C<Convert::Color::HTML> - color conversion using C<Color::Library::Dictionary::HTML> | ||||||||||||||||||||
| 22 | |||||||||||||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||||||||||||
| 24 | |||||||||||||||||||||
| 25 | Directly: | ||||||||||||||||||||
| 26 | |||||||||||||||||||||
| 27 | use Convert::Color::HTML; | ||||||||||||||||||||
| 28 | |||||||||||||||||||||
| 29 | my $red = Convert::Color::HTML->new( 'red' ); | ||||||||||||||||||||
| 30 | |||||||||||||||||||||
| 31 | my $blue = Convert::Color::HTML->new( '#0000FF' ); | ||||||||||||||||||||
| 32 | |||||||||||||||||||||
| 33 | Via L<Convert::Color>: | ||||||||||||||||||||
| 34 | |||||||||||||||||||||
| 35 | use Convert::Color; | ||||||||||||||||||||
| 36 | |||||||||||||||||||||
| 37 | my $cyan = Convert::Color->new( 'html:cyan' ); | ||||||||||||||||||||
| 38 | |||||||||||||||||||||
| 39 | Conversion from RGB: | ||||||||||||||||||||
| 40 | |||||||||||||||||||||
| 41 | my $green = Convert::Color::RGB->( 0, 1.0, 0 )->as_html; | ||||||||||||||||||||
| 42 | say "HTML colour name is " . $green->name; | ||||||||||||||||||||
| 43 | |||||||||||||||||||||
| 44 | =head1 DESCRIPTION | ||||||||||||||||||||
| 45 | |||||||||||||||||||||
| 46 | This subclass of L<Convert::Color::Library> provides a shortcut to performing | ||||||||||||||||||||
| 47 | library lookups specifically within the C<HTML> dictionary. Additionally it | ||||||||||||||||||||
| 48 | will parse C<#RRGGBB> color specifications. | ||||||||||||||||||||
| 49 | |||||||||||||||||||||
| 50 | =cut | ||||||||||||||||||||
| 51 | |||||||||||||||||||||
| 52 | =head1 CONSTRUCTOR | ||||||||||||||||||||
| 53 | |||||||||||||||||||||
| 54 | =cut | ||||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | =head2 $color = Convert::Color::HTML->new( $name ) | ||||||||||||||||||||
| 57 | |||||||||||||||||||||
| 58 | Returns a new object to represent the named color from the C<HTML> dictionary | ||||||||||||||||||||
| 59 | or plain RGB triplet. | ||||||||||||||||||||
| 60 | |||||||||||||||||||||
| 61 | =cut | ||||||||||||||||||||
| 62 | |||||||||||||||||||||
| 63 | sub new | ||||||||||||||||||||
| 64 | 20 | ▶ | { | ||||||||||||||||||
|
Subroutine
new: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||
| 65 | 20 | my $class = shift; | |||||||||||||||||||
| 66 | 20 | my ( $name ) = @_; | |||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | 20 | ▶ | if( $name =~ m/^#([0-9a-f]{6})$/i ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 69 | 2 | my $self = $class->Convert::Color::RGB8::new( $1 ); | |||||||||||||||||||
| 70 | 2 | $self->[3] = uc "#$1"; | |||||||||||||||||||
| 71 | 2 | return $self; | |||||||||||||||||||
| 72 | } | ||||||||||||||||||||
| 73 | |||||||||||||||||||||
| 74 | 18 | return $class->SUPER::new( "html/$name" ); | |||||||||||||||||||
| 75 | } | ||||||||||||||||||||
| 76 | |||||||||||||||||||||
| 77 | =head1 METHODS | ||||||||||||||||||||
| 78 | |||||||||||||||||||||
| 79 | =cut | ||||||||||||||||||||
| 80 | |||||||||||||||||||||
| 81 | =head2 $name = $color->name | ||||||||||||||||||||
| 82 | |||||||||||||||||||||
| 83 | Returns the name of the color instance; either the C<HTML> dictionary name, or | ||||||||||||||||||||
| 84 | the plain RGB triplet notation, as was passed to the constructor. | ||||||||||||||||||||
| 85 | |||||||||||||||||||||
| 86 | =cut | ||||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | # Don't register this as a palette space, because we can always represent any | ||||||||||||||||||||
| 89 | # RGB8 colour. Instead, provide the methods directly | ||||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | 2 | my %palette; | |||||||||||||||||||
| 92 | |||||||||||||||||||||
| 93 | sub new_from_rgb8 | ||||||||||||||||||||
| 94 | 2 | ▶ | { | ||||||||||||||||||
|
Subroutine
new_from_rgb8: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 95 | 2 | my $class = shift; | |||||||||||||||||||
| 96 | 2 | my ( $rgb8 ) = @_; | |||||||||||||||||||
| 97 | |||||||||||||||||||||
| 98 | 2 | my $hex = $rgb8->hex; | |||||||||||||||||||
| 99 | |||||||||||||||||||||
| 100 | 2 | ▶ | unless( keys %palette ) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 101 | %palette = map { | ||||||||||||||||||||
| 102 | 16 | my $color = $class->new( $_ ); | |||||||||||||||||||
| 103 | 16 | $color->hex => $color | |||||||||||||||||||
| 104 | } | ||||||||||||||||||||
| 105 | # RT100404 - omit the misspelled 'fuscia' | ||||||||||||||||||||
| 106 | 1 | grep { $_ ne "fuscia" } Color::Library::Dictionary::HTML->color_names; | |||||||||||||||||||
| 107 | } | ||||||||||||||||||||
| 108 | |||||||||||||||||||||
| 109 | 2 | ▶ | return $palette{$hex} || $class->new( "#$hex" ); | ||||||||||||||||||
|
Condition66%2/3
$palette{$hex} || $class->new("#$hex")
MC/DC0%0/2
$palette{$hex} || $class->new("#$hex")
$palette{$hex}
$class->new("#$hex")
Truth table66%2/3
A || B
A= $palette{$hex}B= $class->new("#$hex")
| |||||||||||||||||||||
| 110 | } | ||||||||||||||||||||
| 111 | |||||||||||||||||||||
| 112 | sub new_rgb | ||||||||||||||||||||
| 113 | 2 | ▶ | { | ||||||||||||||||||
|
Subroutine
new_rgb: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 114 | 2 | my $class = shift; | |||||||||||||||||||
| 115 | 2 | return $class->new_from_rgb8( Convert::Color::RGB->new( @_ )->as_rgb8 ); | |||||||||||||||||||
| 116 | } | ||||||||||||||||||||
| 117 | |||||||||||||||||||||
| 118 | =head1 AUTHOR | ||||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | Paul Evans <leonerd@leonerd.org.uk> | ||||||||||||||||||||
| 121 | |||||||||||||||||||||
| 122 | =cut | ||||||||||||||||||||
| 123 | |||||||||||||||||||||
| 124 | 2 | 0x55AA; | |||||||||||||||||||