line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Color::Rgb; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
# $Id: Rgb.pm,v 1.4 2002/10/23 20:30:46 sherzodr Exp $ |
4
|
|
|
|
|
|
|
|
5
|
|
|
|
|
|
|
require 5.003; |
6
|
1
|
|
|
1
|
|
5823
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
35
|
|
7
|
1
|
|
|
1
|
|
5
|
use Carp 'croak'; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
47
|
|
8
|
1
|
|
|
1
|
|
5
|
use Fcntl qw(:DEFAULT :flock); |
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
486
|
|
9
|
1
|
|
|
1
|
|
5
|
use vars qw($RGB_TXT $VERSION); |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
1089
|
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
########################################################################### |
12
|
|
|
|
|
|
|
################ Color::Rgb - simple rgb.txt parser ####################### |
13
|
|
|
|
|
|
|
########################################################################### |
14
|
|
|
|
|
|
|
# # |
15
|
|
|
|
|
|
|
# Copyright (c) 2002 Sherzod Ruzmetov. All rights reserved # |
16
|
|
|
|
|
|
|
# You can modify and redistribute the following library under the same # |
17
|
|
|
|
|
|
|
# terms as Perl itself. # |
18
|
|
|
|
|
|
|
# # |
19
|
|
|
|
|
|
|
# The library is written with usefulness in mind, but neither explicit # |
20
|
|
|
|
|
|
|
# nor implied guarantee to a particular purpose made. # |
21
|
|
|
|
|
|
|
########################################################################### |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
$RGB_TXT = '/usr/X11R6/lib/X11/rgb.txt'; |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
($VERSION) = '$Revision: 1.4 $' =~ m/Revision:\s*(\S+)/; |
26
|
|
|
|
|
|
|
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
|
31
|
|
|
|
|
|
|
# new(): constructor |
32
|
|
|
|
|
|
|
# Usage: CLASS->new(rgb_txt=>'/path/to/rgb.txt') |
33
|
|
|
|
|
|
|
# RETURN VALUE: Color::Rgb object |
34
|
|
|
|
|
|
|
sub new { |
35
|
1
|
|
|
1
|
0
|
103
|
my $class = shift; |
36
|
1
|
|
33
|
|
|
6
|
$class = ref($class) || $class; |
37
|
|
|
|
|
|
|
|
38
|
1
|
|
|
|
|
5
|
my $self = { |
39
|
|
|
|
|
|
|
rgb_txt => $RGB_TXT, |
40
|
|
|
|
|
|
|
_rgb_map=> {}, |
41
|
|
|
|
|
|
|
@_, |
42
|
|
|
|
|
|
|
}; |
43
|
|
|
|
|
|
|
|
44
|
1
|
50
|
|
|
|
33
|
unless (sysopen (RGB, $self->{rgb_txt}, O_RDONLY) ) { |
45
|
0
|
|
|
|
|
0
|
croak "$self->{rgb_txt}: $!"; |
46
|
|
|
|
|
|
|
} |
47
|
|
|
|
|
|
|
|
48
|
1
|
50
|
|
|
|
7
|
unless ( flock(RGB, LOCK_SH) ) { |
49
|
0
|
|
|
|
|
0
|
croak "Couldn't acquire LOCK_SH on $self->{rgb_txt}: $!"; |
50
|
|
|
|
|
|
|
} |
51
|
|
|
|
|
|
|
|
52
|
1
|
|
|
|
|
22
|
while ( ) { |
53
|
753
|
100
|
|
|
|
2077
|
/^(\n|!|\#)/ and next; # empty lines and comments |
54
|
752
|
|
|
|
|
670
|
chomp(); |
55
|
752
|
|
|
|
|
2728
|
my ($r, $g, $b, $name) = $_ =~ /^\s*(\d+)\s+(\d+)\s+(\d+)\s+(.+)$/; |
56
|
752
|
|
|
|
|
4046
|
$self->{_rgb_map}->{ lc($name) } = [$r, $g, $b]; |
57
|
|
|
|
|
|
|
} |
58
|
|
|
|
|
|
|
|
59
|
1
|
50
|
|
|
|
14
|
close (RGB) or croak "$self->{rgb_txt}: $!"; |
60
|
|
|
|
|
|
|
|
61
|
1
|
|
|
|
|
5
|
return bless $self => $class; |
62
|
|
|
|
|
|
|
} |
63
|
|
|
|
|
|
|
|
64
|
|
|
|
|
|
|
|
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
|
68
|
|
|
|
|
|
|
|
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
|
71
|
|
|
|
|
|
|
|
72
|
|
|
|
|
|
|
# rgb(): reruns RGB value for an name |
73
|
|
|
|
|
|
|
# Usage: CLASS->rgb('red' [, ',']) |
74
|
|
|
|
|
|
|
# RETURN VALUE either list or string |
75
|
|
|
|
|
|
|
sub rgb { |
76
|
7
|
|
|
7
|
0
|
79
|
my ($self, $name, $delim) = @_; |
77
|
|
|
|
|
|
|
|
78
|
7
|
50
|
|
|
|
11
|
unless ( $name ) { |
79
|
0
|
|
|
|
|
0
|
croak "Color::Rgb->rgb(): usage: rgb(\$name [,\$delim]"; |
80
|
|
|
|
|
|
|
} |
81
|
|
|
|
|
|
|
|
82
|
7
|
|
|
|
|
14
|
my $rgb = $self->{_rgb_map}->{lc($name) }; |
83
|
|
|
|
|
|
|
|
84
|
7
|
50
|
|
|
|
14
|
unless ( defined $rgb ) { |
85
|
0
|
|
|
|
|
0
|
croak "$name doesn't exist"; |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
|
88
|
7
|
|
|
|
|
7
|
my @rgb = @{ $rgb }; |
|
7
|
|
|
|
|
19
|
|
89
|
|
|
|
|
|
|
|
90
|
7
|
100
|
|
|
|
26
|
defined $delim and return join ($delim, @rgb); |
91
|
|
|
|
|
|
|
|
92
|
4
|
|
|
|
|
15
|
return @rgb; |
93
|
|
|
|
|
|
|
} |
94
|
|
|
|
|
|
|
|
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
sub name2rgb { |
97
|
1
|
|
|
1
|
0
|
1
|
my $self = shift; |
98
|
|
|
|
|
|
|
|
99
|
1
|
|
|
|
|
3
|
$self->rgb(@_); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
|
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
# hex(): returns a hex value for an name |
104
|
|
|
|
|
|
|
# Usage: CLASS->hex('red' [,'#']) |
105
|
|
|
|
|
|
|
# RETURN VALUE: hex string |
106
|
|
|
|
|
|
|
sub hex { |
107
|
3
|
|
|
3
|
0
|
6
|
my ($self, $name, $pound) = @_; |
108
|
|
|
|
|
|
|
|
109
|
3
|
50
|
|
|
|
5
|
unless ( $name ) { |
110
|
0
|
|
|
|
|
0
|
croak "Color::Rgb->hex(): usage: hex(\$name [,\$prefix]"; |
111
|
|
|
|
|
|
|
} |
112
|
|
|
|
|
|
|
|
113
|
|
|
|
|
|
|
# Using rgb() method to get the RGB list |
114
|
3
|
50
|
|
|
|
7
|
my ($r, $g, $b) = $self->rgb(lc($name)) or return; |
115
|
|
|
|
|
|
|
|
116
|
3
|
|
|
|
|
18
|
return sprintf("$pound%02lx%02lx%02lx", $r, $g, $b); |
117
|
|
|
|
|
|
|
} |
118
|
|
|
|
|
|
|
|
119
|
|
|
|
|
|
|
|
120
|
|
|
|
|
|
|
sub name2hex { |
121
|
1
|
|
|
1
|
0
|
2
|
my $self= shift; |
122
|
|
|
|
|
|
|
|
123
|
1
|
|
|
|
|
2
|
$self->hex(@_); |
124
|
|
|
|
|
|
|
} |
125
|
|
|
|
|
|
|
|
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
# hex2rgb(): takes a hex string, and returns an rgb list or string |
128
|
|
|
|
|
|
|
# depending if $delim was given or not |
129
|
|
|
|
|
|
|
# Usage: CLASS->hex2rgb('#000000' [,',']); |
130
|
|
|
|
|
|
|
# RETURN VALUE: list or string |
131
|
|
|
|
|
|
|
sub hex2rgb { |
132
|
1
|
|
|
1
|
0
|
3
|
my ($self, $hex, $delim) = @_; |
133
|
|
|
|
|
|
|
|
134
|
1
|
50
|
|
|
|
3
|
unless ( $hex ) { |
135
|
0
|
|
|
|
|
0
|
croak "Color::Rgb->hex2rgb(): Usage: hex2rgb(\$hex [,\$delim]"; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
|
139
|
1
|
|
|
|
|
5
|
$hex =~ s/^(\#|Ox)//; |
140
|
|
|
|
|
|
|
|
141
|
1
|
|
|
|
|
2
|
$_ = $hex; |
142
|
1
|
|
|
|
|
5
|
my ($r, $g, $b) = m/(\w{2})(\w{2})(\w{2})/; |
143
|
|
|
|
|
|
|
|
144
|
1
|
|
|
|
|
1
|
my @rgb = (); |
145
|
1
|
|
|
|
|
3
|
$rgb[0] = CORE::hex($r); |
146
|
1
|
|
|
|
|
2
|
$rgb[1] = CORE::hex($g); |
147
|
1
|
|
|
|
|
1
|
$rgb[2] = CORE::hex($b); |
148
|
|
|
|
|
|
|
|
149
|
1
|
50
|
|
|
|
7
|
defined $delim and return join ($delim, @rgb); |
150
|
|
|
|
|
|
|
|
151
|
0
|
|
|
|
|
0
|
return @rgb; |
152
|
|
|
|
|
|
|
} |
153
|
|
|
|
|
|
|
|
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
|
156
|
|
|
|
|
|
|
# rgb2hex(): opposite of hex2rgb(). |
157
|
|
|
|
|
|
|
# Usage: CLASS->rgb2hex($r, $g, $b [,'#']) |
158
|
|
|
|
|
|
|
# RETURN VALUE: hex string |
159
|
|
|
|
|
|
|
sub rgb2hex { |
160
|
4
|
|
|
4
|
0
|
6
|
my ($self, $r, $g, $b, $pound) = @_; |
161
|
|
|
|
|
|
|
|
162
|
4
|
50
|
|
|
|
9
|
unless ( defined $b ) { |
163
|
0
|
|
|
|
|
0
|
croak "Color::Rgb->rgb2hex(): Usage: rgb2hex(\$red, \$green, \$blue [,\$prefix]"; |
164
|
|
|
|
|
|
|
} |
165
|
|
|
|
|
|
|
|
166
|
4
|
|
|
|
|
18
|
return sprintf("$pound%02lx%02lx%02lx", $r, $g, $b); |
167
|
|
|
|
|
|
|
} |
168
|
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
|
170
|
|
|
|
|
|
|
|
171
|
|
|
|
|
|
|
# names(): returns a list of names |
172
|
|
|
|
|
|
|
# Usage: CLASS->names(['gray']) |
173
|
|
|
|
|
|
|
# RETURN VALUE: list |
174
|
|
|
|
|
|
|
sub names { |
175
|
2
|
|
|
2
|
0
|
58
|
my ($self, $pat) = @_; |
176
|
|
|
|
|
|
|
|
177
|
2
|
|
|
|
|
3
|
my @names = (); |
178
|
|
|
|
|
|
|
|
179
|
2
|
|
|
|
|
3
|
while ( my ($name, $rgb) = each %{$self->{_rgb_map}} ) { |
|
1506
|
|
|
|
|
2865
|
|
180
|
1504
|
100
|
|
|
|
2184
|
if ( defined $pat ) { |
181
|
752
|
100
|
|
|
|
1355
|
$name =~ m/$pat/ and push (@names, $name); |
182
|
752
|
|
|
|
|
750
|
next; |
183
|
|
|
|
|
|
|
} |
184
|
752
|
|
|
|
|
834
|
push @names, $name; |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
|
187
|
2
|
|
|
|
|
55
|
return @names; |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
|
191
|
|
|
|
|
|
|
1; |
192
|
|
|
|
|
|
|
|
193
|
|
|
|
|
|
|
########################################################################### |
194
|
|
|
|
|
|
|
################ Color::Rgb manual follows ################################ |
195
|
|
|
|
|
|
|
########################################################################### |
196
|
|
|
|
|
|
|
|
197
|
|
|
|
|
|
|
=pod |
198
|
|
|
|
|
|
|
|
199
|
|
|
|
|
|
|
=head1 NAME |
200
|
|
|
|
|
|
|
|
201
|
|
|
|
|
|
|
Color::Rgb - Simple rgb.txt parsing class |
202
|
|
|
|
|
|
|
|
203
|
|
|
|
|
|
|
=head1 REVISION |
204
|
|
|
|
|
|
|
|
205
|
|
|
|
|
|
|
$Revision: 1.4 $ |
206
|
|
|
|
|
|
|
|
207
|
|
|
|
|
|
|
=head1 SYNOPSIS |
208
|
|
|
|
|
|
|
|
209
|
|
|
|
|
|
|
use Color::Rgb; |
210
|
|
|
|
|
|
|
$rgb = new Color::Rgb(rgb_txt=>'/usr/X11R6/lib/X11/rgb.txt'); |
211
|
|
|
|
|
|
|
|
212
|
|
|
|
|
|
|
@rgb = $rgb->rgb('red'); # returns 255, 0, 0 |
213
|
|
|
|
|
|
|
$red = $rgb->rgb('red', ','); # returns the above rgb list as |
214
|
|
|
|
|
|
|
# comma separated string |
215
|
|
|
|
|
|
|
$red_hex=$rgb->hex('red'); # returns 'FF0000' |
216
|
|
|
|
|
|
|
$red_hex=$rgb->hex('red', '#'); # returns '#FF0000' |
217
|
|
|
|
|
|
|
|
218
|
|
|
|
|
|
|
$my_hex = $rgb->rgb2hex(255,0,0); # returns 'FF0000' |
219
|
|
|
|
|
|
|
$my_rgb = $rgb->hex2rgb('#FF0000'); # returns list of 255,0,0 |
220
|
|
|
|
|
|
|
|
221
|
|
|
|
|
|
|
=head1 DESCRIPTION |
222
|
|
|
|
|
|
|
|
223
|
|
|
|
|
|
|
Color::Rgb - simple rgb.txt parsing class. |
224
|
|
|
|
|
|
|
|
225
|
|
|
|
|
|
|
=head1 METHODS |
226
|
|
|
|
|
|
|
|
227
|
|
|
|
|
|
|
=over 4 |
228
|
|
|
|
|
|
|
|
229
|
|
|
|
|
|
|
=item * |
230
|
|
|
|
|
|
|
|
231
|
|
|
|
|
|
|
C$rgb_file])> - constructor. Returns a Color::Rgb object. |
232
|
|
|
|
|
|
|
Optionally accepts a path to the rgb.txt file. If you omit the file, it |
233
|
|
|
|
|
|
|
will use the path in the $Color::Rgb::RGB_TXT variable, which defaults to |
234
|
|
|
|
|
|
|
C<'/usr/X11R6/lib/X11/rgb.txt'>. It means, instead of using rgb_txt=>'' |
235
|
|
|
|
|
|
|
option, you could also set the value of the $Color::Rgb::RGB_TXT variable |
236
|
|
|
|
|
|
|
to the correct path before you call the L constructor (but definitely |
237
|
|
|
|
|
|
|
after you load the Color::Rgb class with C |
238
|
|
|
|
|
|
|
|
239
|
|
|
|
|
|
|
Note: If your system does not provide with any rgb.txt file, Color::Rgb |
240
|
|
|
|
|
|
|
distribution includes one you can use instead. |
241
|
|
|
|
|
|
|
|
242
|
|
|
|
|
|
|
=item * |
243
|
|
|
|
|
|
|
|
244
|
|
|
|
|
|
|
C - returns list of numeric Red, Green and Blue |
245
|
|
|
|
|
|
|
values for a $name delimited (optionally) by a $delimiter . $name is |
246
|
|
|
|
|
|
|
name of the color in the English language (Ex., 'black', 'red', 'purple' etc.). |
247
|
|
|
|
|
|
|
|
248
|
|
|
|
|
|
|
Examples: |
249
|
|
|
|
|
|
|
|
250
|
|
|
|
|
|
|
my ($r, $g, $b) = $rgb->rgb('blue'); # returns list: 00, 00, 255 |
251
|
|
|
|
|
|
|
my $string = $rgb->rgb('blue', ','); # returns string: '00,00,255' |
252
|
|
|
|
|
|
|
|
253
|
|
|
|
|
|
|
If name does not exist in the rgb.txt file it will return undef. |
254
|
|
|
|
|
|
|
|
255
|
|
|
|
|
|
|
=item * |
256
|
|
|
|
|
|
|
|
257
|
|
|
|
|
|
|
C - similar to L method, but returns |
258
|
|
|
|
|
|
|
hexadecimal string representing red, green and blue colors, prefixed |
259
|
|
|
|
|
|
|
(optionally) with $prefix. If $name does not exist in the rgb.txt file |
260
|
|
|
|
|
|
|
it will return undef. |
261
|
|
|
|
|
|
|
|
262
|
|
|
|
|
|
|
=item * |
263
|
|
|
|
|
|
|
|
264
|
|
|
|
|
|
|
C - alias to C |
265
|
|
|
|
|
|
|
|
266
|
|
|
|
|
|
|
=item * |
267
|
|
|
|
|
|
|
|
268
|
|
|
|
|
|
|
C - alias to C |
269
|
|
|
|
|
|
|
|
270
|
|
|
|
|
|
|
=item * |
271
|
|
|
|
|
|
|
|
272
|
|
|
|
|
|
|
C - converts rgb value to hexadecimal string. |
273
|
|
|
|
|
|
|
This method has nothing to do with the rgb.txt file, so none of the arguments |
274
|
|
|
|
|
|
|
need to exist in the file. |
275
|
|
|
|
|
|
|
|
276
|
|
|
|
|
|
|
Examples: |
277
|
|
|
|
|
|
|
|
278
|
|
|
|
|
|
|
@rgb = (128, 128, 128); # RGB representation of grey |
279
|
|
|
|
|
|
|
$hex_grey = $rgb->rgb2hex(@rgb); # returns string 'C0C0C0' |
280
|
|
|
|
|
|
|
$hex_grey = $rgb->rgb2hex(@rgb, '#'); # returns string '#C0C0C0' |
281
|
|
|
|
|
|
|
|
282
|
|
|
|
|
|
|
=item * |
283
|
|
|
|
|
|
|
|
284
|
|
|
|
|
|
|
C - the opposite of L: takes a |
285
|
|
|
|
|
|
|
hexadecimal representation of a color and returns a numeric list of Red, |
286
|
|
|
|
|
|
|
Green and Blue. If optional $delim delimiter is present, it returns the |
287
|
|
|
|
|
|
|
string of RGB colors delimited by the $delimiter. Characters like '#' and |
288
|
|
|
|
|
|
|
'0x' in the beginning of the hexadecimal value will be ignored. Examples: |
289
|
|
|
|
|
|
|
|
290
|
|
|
|
|
|
|
$hex = '#00FF00'; # represents blue |
291
|
|
|
|
|
|
|
|
292
|
|
|
|
|
|
|
@rgb = $rgb->hex2rgb($hex); #returns list of 0, 255, 0 |
293
|
|
|
|
|
|
|
$rgb_string = $rgb->hex2rgb($hex,','); #returns string '0,255,0' |
294
|
|
|
|
|
|
|
|
295
|
|
|
|
|
|
|
Note: L expects valid hexadecimal representation of a color in |
296
|
|
|
|
|
|
|
6 character long string. If not, it might not work properly. |
297
|
|
|
|
|
|
|
|
298
|
|
|
|
|
|
|
=item * |
299
|
|
|
|
|
|
|
|
300
|
|
|
|
|
|
|
C - returns a list of all the names in the rgb.txt file. |
301
|
|
|
|
|
|
|
If $pattern is given as the first argument, it will return only the names |
302
|
|
|
|
|
|
|
matching the pattern. Example: |
303
|
|
|
|
|
|
|
|
304
|
|
|
|
|
|
|
@colors = $rgb->names; # returns all the names |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
@gray_colors = $rgb->names('gray'); # returns list of all the names |
307
|
|
|
|
|
|
|
# matching the word 'gray' |
308
|
|
|
|
|
|
|
=back |
309
|
|
|
|
|
|
|
|
310
|
|
|
|
|
|
|
=head1 CREDITS |
311
|
|
|
|
|
|
|
|
312
|
|
|
|
|
|
|
Following people contributed to this library with their patches and/or |
313
|
|
|
|
|
|
|
bug reports. (list is in chronological order) |
314
|
|
|
|
|
|
|
|
315
|
|
|
|
|
|
|
=over 4 |
316
|
|
|
|
|
|
|
|
317
|
|
|
|
|
|
|
=item * |
318
|
|
|
|
|
|
|
|
319
|
|
|
|
|
|
|
Marc-Olivier BERNARD notified of the warnings that the library |
320
|
|
|
|
|
|
|
produced while "warnings" pragma enabled and improper parsed rgb values |
321
|
|
|
|
|
|
|
that contain single "0". This bug was fixed in 1.2 |
322
|
|
|
|
|
|
|
|
323
|
|
|
|
|
|
|
=item * |
324
|
|
|
|
|
|
|
|
325
|
|
|
|
|
|
|
Martin Herrmann noticed a bug in rgb2hex() method which was |
326
|
|
|
|
|
|
|
failing if the blue value was a single "0". This problem is fixed in 1.3 |
327
|
|
|
|
|
|
|
|
328
|
|
|
|
|
|
|
=back |
329
|
|
|
|
|
|
|
|
330
|
|
|
|
|
|
|
=head1 COPYRIGHT |
331
|
|
|
|
|
|
|
|
332
|
|
|
|
|
|
|
Color::Rgb is a free software and can be modified and distributed under the same terms |
333
|
|
|
|
|
|
|
as Perl itself. |
334
|
|
|
|
|
|
|
|
335
|
|
|
|
|
|
|
=head1 AUTHOR |
336
|
|
|
|
|
|
|
|
337
|
|
|
|
|
|
|
Color::Rgb is maintained by Sherzod B. Ruzmetov . |
338
|
|
|
|
|
|
|
|
339
|
|
|
|
|
|
|
=head1 SEE ALSO |
340
|
|
|
|
|
|
|
|
341
|
|
|
|
|
|
|
L |
342
|
|
|
|
|
|
|
|
343
|
|
|
|
|
|
|
=cut |