line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Color::Library::Color; |
2
|
|
|
|
|
|
|
|
3
|
2
|
|
|
2
|
|
11
|
use strict; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
62
|
|
4
|
2
|
|
|
2
|
|
10
|
use warnings; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
59
|
|
5
|
|
|
|
|
|
|
|
6
|
2
|
|
|
2
|
|
9
|
use base qw/Class::Accessor::Fast/; |
|
2
|
|
|
|
|
3
|
|
|
2
|
|
|
|
|
350438
|
|
7
|
|
|
|
|
|
|
|
8
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors(qw/_id _name _title _dictionary /); |
9
|
|
|
|
|
|
|
__PACKAGE__->mk_accessors(qw/_rgb _html _value _hex/); |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
use overload |
12
|
2
|
|
|
|
|
24
|
'""' => \&html, |
13
|
|
|
|
|
|
|
fallback => 1, |
14
|
2
|
|
|
2
|
|
12111
|
; |
|
2
|
|
|
|
|
1288
|
|
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub rgb; |
17
|
|
|
|
|
|
|
sub rgb2hex; |
18
|
|
|
|
|
|
|
sub rgb2value; |
19
|
|
|
|
|
|
|
sub value2rgb($); |
20
|
|
|
|
|
|
|
sub parse_rgb_color; |
21
|
|
|
|
|
|
|
sub integer2rgb($); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 NAME |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
Color::Library::Color - Color entry for a Color::Library color dictionary |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
=head1 METHODS |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
=over 4 |
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
=item $id = $color->id |
32
|
|
|
|
|
|
|
|
33
|
|
|
|
|
|
|
Returns the id of the color |
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
A color id is in the format of , e.g. |
36
|
|
|
|
|
|
|
|
37
|
|
|
|
|
|
|
svg:aliceblue |
38
|
|
|
|
|
|
|
x11:bisque2 |
39
|
|
|
|
|
|
|
nbs-iscc-f:chromeyellow.66 |
40
|
|
|
|
|
|
|
vaccc:darkspringyellow |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=item $name = $color->name |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
Returns the name of the color, e.g. |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
aliceblue |
47
|
|
|
|
|
|
|
bisque2 |
48
|
|
|
|
|
|
|
chromeyellow |
49
|
|
|
|
|
|
|
darkspringyellow |
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
=item $title = $color->title |
52
|
|
|
|
|
|
|
|
53
|
|
|
|
|
|
|
Returns the title of the color, e.g. |
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
aliceblue |
56
|
|
|
|
|
|
|
bisque2 |
57
|
|
|
|
|
|
|
chrome yellow |
58
|
|
|
|
|
|
|
Dark Spring-Yellow |
59
|
|
|
|
|
|
|
|
60
|
|
|
|
|
|
|
=item $dictionary = $color->dictionary |
61
|
|
|
|
|
|
|
|
62
|
|
|
|
|
|
|
Returns the Color::Library::Dictionary object that the color belongs to |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
=item $hex = $color->hex |
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
Returns the hex value of the color, e.g. |
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
ff08ff |
69
|
|
|
|
|
|
|
eed5b7 |
70
|
|
|
|
|
|
|
eaa221 |
71
|
|
|
|
|
|
|
669900 |
72
|
|
|
|
|
|
|
|
73
|
|
|
|
|
|
|
Note that $hex does NOT include the leading #, for that use $color->html, $color->css, or $color->svg |
74
|
|
|
|
|
|
|
|
75
|
|
|
|
|
|
|
=item $html = $color->html |
76
|
|
|
|
|
|
|
|
77
|
|
|
|
|
|
|
=item $css = $color->css |
78
|
|
|
|
|
|
|
|
79
|
|
|
|
|
|
|
=item $svg = $color->svg |
80
|
|
|
|
|
|
|
|
81
|
|
|
|
|
|
|
Returns the hex value of the color with a leading #, suitable for use in HTML, CSS, or SVG documents, e.g. |
82
|
|
|
|
|
|
|
|
83
|
|
|
|
|
|
|
#ff08ff |
84
|
|
|
|
|
|
|
#eed5b7 |
85
|
|
|
|
|
|
|
#eaa221 |
86
|
|
|
|
|
|
|
#669900 |
87
|
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
=cut |
89
|
|
|
|
|
|
|
|
90
|
|
|
|
|
|
|
=item $value = $color->value |
91
|
|
|
|
|
|
|
|
92
|
|
|
|
|
|
|
Returns the numeric value of the color, e.g. |
93
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
15792383 |
95
|
|
|
|
|
|
|
15652279 |
96
|
|
|
|
|
|
|
15376929 |
97
|
|
|
|
|
|
|
6723840 |
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
=cut |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
for my $method (qw/id name title dictionary html value hex/) { |
102
|
2
|
|
|
2
|
|
299
|
no strict 'refs'; |
|
2
|
|
|
|
|
5
|
|
|
2
|
|
|
|
|
2380
|
|
103
|
|
|
|
|
|
|
my $accessor = "_$method"; |
104
|
89797
|
|
|
89797
|
|
246717
|
*$method = sub { return $_[0]->$accessor }; |
105
|
|
|
|
|
|
|
} |
106
|
|
|
|
|
|
|
*css = \&html; |
107
|
|
|
|
|
|
|
*svg = \&html; |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=item ($r, $g, $b) = $color->rgb |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
Returns r, g, and b values of the color as a 3 element list (list context), e.g. |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
(240, 248, 255) |
114
|
|
|
|
|
|
|
|
115
|
|
|
|
|
|
|
=item $rgb = $color->rgb |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
Returns r, g, and b values of the color in a 3 element array (scalar context), e.g. |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
[ 240, 248, 255 ] |
120
|
|
|
|
|
|
|
|
121
|
|
|
|
|
|
|
=cut |
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
sub rgb { |
124
|
2
|
50
|
|
2
|
1
|
774
|
return wantarray ? @{ $_[0]->_rgb } : [ @{ $_[0]->_rgb } ] |
|
0
|
|
|
|
|
0
|
|
|
2
|
|
|
|
|
18
|
|
125
|
|
|
|
|
|
|
} |
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
=item $color = Color::Library::Color->new( id => $id, name => $name, title => $title, value => $value ) |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
=item $color = Color::Library::Color->new( { id => $id, name => $name, title => $title, value => $value } ) |
130
|
|
|
|
|
|
|
|
131
|
|
|
|
|
|
|
=item $color = Color::Library::Color->new( [[ $id, $name, $title, $rgb, $hex, $value ]] ) |
132
|
|
|
|
|
|
|
|
133
|
|
|
|
|
|
|
Returns a new Color::Library::Color object representing the specified color |
134
|
|
|
|
|
|
|
|
135
|
|
|
|
|
|
|
You probably don't want/need to call this yourself |
136
|
|
|
|
|
|
|
|
137
|
|
|
|
|
|
|
=cut |
138
|
|
|
|
|
|
|
|
139
|
|
|
|
|
|
|
# FUTURE Note that $value may be a numeric value, a hex value, or a 3 element r-g-b array |
140
|
|
|
|
|
|
|
|
141
|
|
|
|
|
|
|
sub new { |
142
|
12801
|
|
|
12801
|
1
|
44242
|
my $self = bless {}, shift; |
143
|
12801
|
50
|
|
|
|
28736
|
if (ref $_[0] eq "ARRAY") { |
144
|
12801
|
|
|
|
|
13093
|
my ($id, $name, $title, $rgb, $hex, $value) = @{ shift() }; |
|
12801
|
|
|
|
|
33382
|
|
145
|
12801
|
|
|
|
|
33005
|
$self->_id($id); |
146
|
12801
|
|
|
|
|
83826
|
$self->_name($name); |
147
|
12801
|
|
|
|
|
75877
|
$self->_title($title); |
148
|
12801
|
|
|
|
|
73498
|
$self->_rgb($rgb); |
149
|
12801
|
|
|
|
|
66961
|
$self->_hex($hex); |
150
|
12801
|
|
|
|
|
77055
|
$self->_html("#" . $hex); |
151
|
12801
|
|
|
|
|
70675
|
$self->_value($value); |
152
|
12801
|
|
|
|
|
72199
|
$self->_dictionary(shift); |
153
|
|
|
|
|
|
|
} |
154
|
|
|
|
|
|
|
else { |
155
|
0
|
0
|
|
|
|
0
|
local %_ = ref $_[0] eq "HASH" ? %{ $_[0] } : @_; |
|
0
|
|
|
|
|
0
|
|
156
|
0
|
|
|
|
|
0
|
$self->_id($_{id}); |
157
|
0
|
|
|
|
|
0
|
$self->_name($_{name}); |
158
|
0
|
|
|
|
|
0
|
$self->_title($_{title}); |
159
|
0
|
|
|
|
|
0
|
$self->_dictionary($_{dictionary}); |
160
|
|
|
|
|
|
|
|
161
|
0
|
0
|
|
|
|
0
|
my ($r, $g, $b) = parse_rgb_color(ref $_{value} eq "ARRAY" ? @{ $_{value} } : $_{value}); |
|
0
|
|
|
|
|
0
|
|
162
|
|
|
|
|
|
|
|
163
|
0
|
|
|
|
|
0
|
my $rgb = $self->_rgb([ $r, $g, $b ]); |
164
|
0
|
|
|
|
|
0
|
my $hex = $self->_hex(rgb2hex $rgb); |
165
|
0
|
|
|
|
|
0
|
$self->_html("#" . $hex); |
166
|
0
|
|
|
|
|
0
|
$self->_value(rgb2value $rgb); |
167
|
|
|
|
|
|
|
} |
168
|
12801
|
|
|
|
|
93120
|
return $self; |
169
|
|
|
|
|
|
|
} |
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
=back |
172
|
|
|
|
|
|
|
|
173
|
|
|
|
|
|
|
=head2 FUNCTIONS |
174
|
|
|
|
|
|
|
|
175
|
|
|
|
|
|
|
=over 4 |
176
|
|
|
|
|
|
|
|
177
|
|
|
|
|
|
|
=item $hex = Color::Library::Color::rgb2hex( $rgb ) |
178
|
|
|
|
|
|
|
|
179
|
|
|
|
|
|
|
=item $hex = Color::Library::Color::rgb2hex( $r, $g, $b ) |
180
|
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
Converts an rgb value to its hex representation |
182
|
|
|
|
|
|
|
|
183
|
|
|
|
|
|
|
=cut |
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
sub rgb2hex { |
186
|
0
|
0
|
|
0
|
1
|
|
return ref $_[0] eq "ARRAY" ? |
187
|
|
|
|
|
|
|
sprintf("%02lx%02lx%02lx", $_[0][0], $_[0][1], $_[0][2]) : |
188
|
|
|
|
|
|
|
sprintf("%02lx%02lx%02lx", $_[0], $_[1], $_[2]); |
189
|
|
|
|
|
|
|
} |
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
=item $value = Color::Library::Color::rgb2value( $rgb ) |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
=item $value = Color::Library::Color::rgb2value( $r, $g, $b ) |
194
|
|
|
|
|
|
|
|
195
|
|
|
|
|
|
|
Converts an rgb value to its numeric representation |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=cut |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
sub rgb2value { |
200
|
0
|
0
|
|
0
|
1
|
|
my ($r, $g, $b) = ref $_[0] eq "ARRAY" ? @{ $_[0] } : @_; |
|
0
|
|
|
|
|
|
|
201
|
0
|
|
|
|
|
|
return $b + ($g << 8) + ($r << 16); |
202
|
|
|
|
|
|
|
} |
203
|
|
|
|
|
|
|
|
204
|
|
|
|
|
|
|
=item $rgb = Color::Library::Color::value2rgb( $value ) |
205
|
|
|
|
|
|
|
|
206
|
|
|
|
|
|
|
=item ($r, $g, $b) = Color::Library::Color::value2rgb( $value ) |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
Converts a numeric color value to its rgb representation |
209
|
|
|
|
|
|
|
|
210
|
|
|
|
|
|
|
=cut |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
sub value2rgb($) { |
213
|
0
|
|
|
0
|
1
|
|
my $value = shift; |
214
|
0
|
|
|
|
|
|
my ($r, $g, $b); |
215
|
0
|
|
|
|
|
|
$b = ($value & 0x0000ff); |
216
|
|
|
|
|
|
|
|
217
|
0
|
|
|
|
|
|
$g = ($value & 0x00ff00) >> 8; |
218
|
0
|
|
|
|
|
|
$r = ($value & 0xff0000) >> 16; |
219
|
0
|
0
|
|
|
|
|
return wantarray ? ($r, $g, $b) : [ $r, $g, $b ]; |
220
|
|
|
|
|
|
|
} |
221
|
|
|
|
|
|
|
|
222
|
|
|
|
|
|
|
=item ($r, $g, $b) = Color::Library::Color::parse_rgb_color( $hex ) |
223
|
|
|
|
|
|
|
|
224
|
|
|
|
|
|
|
=item ($r, $g, $b) = Color::Library::Color::parse_rgb_color( $value ) |
225
|
|
|
|
|
|
|
|
226
|
|
|
|
|
|
|
Makes a best effort to convert a hex or numeric color value to its rgb representation |
227
|
|
|
|
|
|
|
|
228
|
|
|
|
|
|
|
=cut |
229
|
|
|
|
|
|
|
|
230
|
|
|
|
|
|
|
# Partly taken from Imager/Color.pm |
231
|
|
|
|
|
|
|
sub parse_rgb_color { |
232
|
0
|
0
|
0
|
0
|
1
|
|
return (@_) if @_ == 3 && ! grep /[^\d.+eE-]/, @_; |
233
|
0
|
0
|
|
|
|
|
if ($_[0] =~ /^\#?([\da-f][\da-f])([\da-f][\da-f])([\da-f][\da-f])$/i) { |
234
|
0
|
|
|
|
|
|
return (hex($1), hex($2), hex($3)); |
235
|
|
|
|
|
|
|
} |
236
|
0
|
0
|
|
|
|
|
if ($_[0] =~ /^\#?([\da-f])([\da-f])([\da-f])$/i) { |
237
|
0
|
|
|
|
|
|
return (hex($1) * 17, hex($2) * 17, hex($3) * 17); |
238
|
|
|
|
|
|
|
} |
239
|
0
|
0
|
0
|
|
|
|
return value2rgb $_[0] if 1 == @_ && $_[0] =~ m/^\d+$/; |
240
|
|
|
|
|
|
|
} |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
1; |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
__END__ |