Color.pm

filter: Statementstmt 76.7% 99 / 129 Branchbran 50.0% 23 / 46 Conditioncond 12.5% 2 / 16 MC/DCmcdc 0.0% 0 / 10 Subroutinesub 88.0% 22 / 25 Podpod 60.0% 6 / 10 totaltotal 64.4% 152 / 236 CC 32 SCAR 17.6
CC
32
Cov
65%
AUTOLOAD
29.7
_space2class
7.8
convert_to
6.1
CRAP 5.8
« Color/X11.pm ↑ Summary Color/HSV.pm »
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-2024 -- leonerd@leonerd.org.uk
5  
6 package Convert::Color 0.18;
7  
8 18 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 18 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 18 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
12  
13 18 ▶use List::UtilsBy qw( min_by );
Subroutine BEGIN: called CC=1 SCAR=0.0
14  
15 18 ▶use meta 0.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
16 18 ▶no warnings 'meta::experimental';
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 # Maximum number of entries in a ->closest_to cache
19 18 ▶use constant MAX_CACHE_SIZE => 1000;
Subroutine BEGIN: called CC=1 SCAR=0.0
20  
21 18 ▶use Module::Pluggable require => 0,
Subroutine BEGIN: called CC=1 SCAR=0.0
22 18 search_path => [ 'Convert::Color' ];
23 0 my @plugins = Convert::Color->plugins;
24  
25 =head1 NAME
26  
27 C<Convert::Color> - color space conversions and named lookups
28  
29 =head1 SYNOPSIS
30  
31 use Convert::Color;
32  
33 my $color = Convert::Color->new( 'hsv:76,0.43,0.89' );
34  
35 my ( $red, $green, $blue ) = $color->rgb;
36  
37 # GTK uses 16-bit values
38 my $gtk_col = Gtk2::Gdk::Color->new( $color->as_rgb16->rgb16 );
39  
40 # HTML uses #rrggbb in hex
41 my $html = '<td bgcolor="#' . $color->as_rgb8->hex . '">';
42  
43 =head1 DESCRIPTION
44  
45 This module provides conversions between commonly used ways to express colors.
46 It provides conversions between color spaces such as RGB and HSV, and it
47 provides ways to look up colors by a name.
48  
49 This class provides a base for subclasses which represent particular color
50 values in particular spaces. The base class provides methods to represent the
51 color in a few convenient forms, though subclasses may provide more specific
52 details for the space in question.
53  
54 For more detail, read the documentation on these classes; namely:
55  
56 =over 4
57  
58 =item *
59  
60 L<Convert::Color::RGB> - red/green/blue as floats between 0 and 1
61  
62 =item *
63  
64 L<Convert::Color::RGB8> - red/green/blue as 8-bit integers
65  
66 =item *
67  
68 L<Convert::Color::RGB16> - red/green/blue as 16-bit integers
69  
70 =item *
71  
72 L<Convert::Color::HSV> - hue/saturation/value
73  
74 =item *
75  
76 L<Convert::Color::HSL> - hue/saturation/lightness
77  
78 =item *
79  
80 L<Convert::Color::CMY> - cyan/magenta/yellow
81  
82 =item *
83  
84 L<Convert::Color::CMYK> - cyan/magenta/yellow/key (blackness)
85  
86 =back
87  
88 The following classes are subclasses of one of the above, which provide a way
89 to access predefined colors by names:
90  
91 =over 4
92  
93 =item *
94  
95 L<Convert::Color::VGA> - named lookup for the basic VGA colors
96  
97 =item *
98  
99 L<Convert::Color::X11> - named lookup of colors from X11's F<rgb.txt>
100  
101 =back
102  
103 =cut
104  
105 =head1 CONSTRUCTOR
106  
107 =cut
108  
109 0 my $_space2class_cache_initialised;
110 my %_space2class_cache; # {$space} = $class
111 18 my %_class2space_cache; # {$class} = $space
112  
113 # doc'ed later for readability...
114 sub register_color_space
115 105 ▶{
Subroutine register_color_space: called CC=3 SCAR=11.1
Pod documented
116 105 my $class = shift;
117 105 my ( $space ) = @_;
118  
119 105 ▶ exists $_space2class_cache{$space} and croak "Color space $space is already defined";
Branch: 1 branch
BranchTrueFalseTotal
exists $_space2class_cache{$space} and croak("Color space $space is already defined") 0 105 105
120 105 ▶ exists $_class2space_cache{$class} and croak "Class $class already declared a color space";
Branch: 1 branch
BranchTrueFalseTotal
exists $_class2space_cache{$class} and croak("Class $class already declared a color space") 0 105 105
121  
122 105 $_space2class_cache{$space} = $class;
123 105 $_class2space_cache{$class} = $space;
124  
125 105 my $metapkg = meta::get_this_package;
126 24 ▶ $metapkg->add_named_sub( "as_${space}" => sub {
Subroutine as_rgb16: called CC=1 SCAR=0.0
Subroutine as_hsv: called CC=1 SCAR=0.0
Pod undocumented
Pod documented
127 24 shift->convert_to( $space )
128 105 } );
129 }
130  
131 sub _space2class
132 129 ▶{
Subroutine _space2class: called CC=6 SCAR=20.5
133 129 my ( $space ) = @_;
134  
135 129 ▶ unless( $_space2class_cache_initialised ) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($_space2class_cache_initialised) 10 119 129
136 10 $_space2class_cache_initialised++;
137 # Initialise the space name to class cache
138 10 foreach my $class ( @plugins ) {
139 100 ( my $file = "$class.pm" ) =~ s{::}{/}g;
140 100 ▶ require $file or next;
Branch: 1 branch
BranchTrueFalseTotal
require $file or next 100 0 100
141  
142 100 ▶ $class->can( 'COLOR_SPACE' ) or next;
Branch: 1 branch
BranchTrueFalseTotal
$class->can("COLOR_SPACE") or next 0 100 100
143 0 ▶ my $thisspace = $class->COLOR_SPACE or next;
Branch: 1 branch
BranchTrueFalseTotal
my $thisspace = $class->COLOR_SPACE or next 0 0 0
144  
145 0 warnings::warn( deprecated => "Discovered $class by deprecated COLOR_SPACE method" );
146  
147 0 $class->register_color_space( $thisspace );
148 }
149 }
150  
151 129 return $_space2class_cache{$space};
152 }
153  
154 =head2 new
155  
156 $color = Convert::Color->new( STRING );
157  
158 Return a new value to represent the color specified by the string. This string
159 should be prefixed by the name of the color space to which it applies. For
160 example
161  
162 rgb:RED,GREEN,BLUE
163 rgb8:RRGGBB
164 rgb16:RRRRGGGGBBBB
165 hsv:HUE,SAT,VAL
166 hsl:HUE,SAT,LUM
167 cmy:CYAN,MAGENTA,YELLOW
168 cmyk:CYAN,MAGENTA,YELLOW,KEY
169  
170 vga:NAME
171 vga:INDEX
172  
173 x11:NAME
174  
175 For more detail, see the constructor of the color space subclass in question.
176  
177 =cut
178  
179 sub new
180 8 ▶{
Subroutine new: called CC=3 SCAR=11.2
Pod documented
181 8 shift;
182 8 my ( $str ) = @_;
183  
184 8 ▶ $str =~ m/^(\w+):(.*)$/ or croak "Unable to parse color name $str";
Branch: 1 branch
BranchTrueFalseTotal
$str =~ /^(\w+):(.*)$/u or croak("Unable to parse color name $str") 8 0 8
185 8 ( my $space, $str ) = ( $1, $2 );
186  
187 8 ▶ my $class = _space2class( $space ) or croak "Unrecognised color space name '$space'";
Branch: 1 branch
BranchTrueFalseTotal
my $class = _space2class($space) or croak("Unrecognised color space name '${space}'") 8 0 8
188  
189 8 return $class->new( $str );
190 }
191  
192 =head1 METHODS
193  
194 =cut
195  
196 =head2 rgb
197  
198 ( $red, $green, $blue ) = $color->rgb;
199  
200 Returns the individual red, green and blue color components of the color
201 value. For RGB values, this is done directly. For values in other spaces, this
202 is done by first converting them to an RGB value using their C<to_rgb()>
203 method.
204  
205 =cut
206  
207 sub rgb
208 0 ▶{
Subroutine rgb: not called CC=1 SCAR=6.9
Pod documented
209 0 my $self = shift;
210 0 croak "Abstract method - should be overloaded by ".ref($self);
211 }
212  
213 =head1 COLOR SPACE CONVERSIONS
214  
215 Cross-conversion between color spaces is provided by the C<convert_to()>
216 method, assisted by helper methods in the two color space classes involved.
217  
218 When converting C<$color> from color space SRC to color space DEST, the
219 following operations are attemped, in this order. SRC and DEST refer to the
220 names of the color spaces, e.g. C<rgb>.
221  
222 =over 4
223  
224 =item 1.
225  
226 If SRC and DEST are equal, return C<$color> as it stands.
227  
228 =item 2.
229  
230 If the SRC space's class provides a C<convert_to_DEST> method, use it.
231  
232 =item 3.
233  
234 If the DEST space's class provides a C<new_from_SRC> constructor, call it and
235 pass C<$color>.
236  
237 =item 4.
238  
239 If the DEST space's class provides a C<new_rgb> constructor, convert C<$color>
240 to red/green/blue components then call it.
241  
242 =item 5.
243  
244 If none of these operations worked, then throw an exception.
245  
246 =back
247  
248 These functions may be called in the following ways:
249  
250 $other = $color->convert_to_DEST();
251 $other = Dest::Class->new_from_SRC( $color );
252 $other = Dest::Class->new_rgb( $color->rgb );
253  
254 =cut
255  
256 =head2 convert_to
257  
258 $other = $color->convert_to( $space );
259  
260 Attempt to convert the color into its representation in the given space. See
261 above for the various ways this may be achieved.
262  
263 If the relevant subclass has already been loaded (either explicitly, or
264 implicitly by either the C<new> or C<convert_to> methods), then a specific
265 conversion method will be installed in the class.
266  
267 $other = $color->as_$space;
268  
269 Methods of this form are currently C<AUTOLOAD>ed if they do not yet exist, but
270 this feature should not be relied upon - see below.
271  
272 =cut
273  
274 sub convert_to
275 121 ▶{
Subroutine convert_to: called CC=6 SCAR=18.1
Pod documented
276 121 my $self = shift;
277 121 my ( $to_space ) = @_;
278  
279 121 ▶ my $to_class = _space2class( $to_space ) or croak "Unrecognised color space name '$to_space'";
Branch: 1 branch
BranchTrueFalseTotal
my $to_class = _space2class($to_space) or croak("Unrecognised color space name '${to_space}'") 121 0 121
280  
281 121 my $from_space = $_class2space_cache{ref $self};
282  
283 121 ▶ if( $from_space eq $to_space ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($from_space eq $to_space) 42 79 121
284 # Identity conversion
285 42 return $self;
286 }
287  
288 79 my $code;
289 79 ▶ if( $code = $self->can( "convert_to_$to_space" ) ) {
Branch: 3 branches
BranchTrueFalseTotal
if ($code = $self->can("convert_to_$to_space")) { } 30 49 79
elsif ($code = $to_class->can("new_from_$from_space")) { } 3 46 49
elsif ($code = $to_class->can('new_rgb')) { } 46 0 46
290 30 return $code->( $self );
291 }
292 elsif( $code = $to_class->can( "new_from_$from_space" ) ) {
293 3 return $code->( $to_class, $self );
294 }
295 elsif( $code = $to_class->can( "new_rgb" ) ) {
296 # TODO: check that $self->rgb is overloaded
297 46 return $code->( $to_class, $self->rgb );
298 }
299 else {
300 0 croak "Cannot convert from space '$from_space' to space '$to_space'";
301 }
302 }
303  
304 # Fallback implementations in case subclasses don't provide anything better
305  
306 sub convert_to_rgb
307 28 ▶{
Subroutine convert_to_rgb: called CC=1 SCAR=0.0
Pod undocumented
308 28 my $self = shift;
309 28 require Convert::Color::RGB;
310 28 return Convert::Color::RGB->new( $self->rgb );
311 }
312  
313 =head1 AUTOLOADED CONVERSION METHODS
314  
315 This class provides C<AUTOLOAD> and C<can> behaviour which automatically
316 constructs conversion methods. The following method calls are identical:
317  
318 $color->convert_to('rgb');
319 $color->as_rgb;
320  
321 The generated method will be stored in the package, so that future calls will
322 not have the AUTOLOAD overhead.
323  
324 This feature is deprecated and should not be relied upon, due to the delicate
325 nature of C<AUTOLOAD>.
326  
327 =cut
328  
329 # Since this is AUTOLOADed, we can dynamically provide new methods for classes
330 # discovered at runtime.
331  
332 sub can
333 274 ▶{
Subroutine can: called CC=3 SCAR=16.3
Pod undocumented
334 274 my $self = shift;
335 274 my ( $method ) = @_;
336  
337 274 ▶ if( $method =~ m/^as_(.*)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($method =~ /^as_(.*)$/u) 0 274 274
338 0 my $to_space = $1;
339 0 ▶ _space2class( $to_space ) or return undef;
Branch: 1 branch
BranchTrueFalseTotal
_space2class($to_space) or return undef 0 0 0
340  
341 0 ▶ return sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
342 0 my $self = shift;
343 0 return $self->convert_to( $to_space );
344 0 };
345 }
346  
347 274 return $self->SUPER::can( $method );
348 }
349  
350 sub AUTOLOAD
351 197 ▶{
Subroutine AUTOLOAD: called CC=6 SCAR=33.9
352 197 my ( $method ) = our $AUTOLOAD =~ m/::([^:]+)$/;
353  
354 197 ▶ return if $method eq "DESTROY";
Branch: 1 branch
BranchTrueFalseTotal
if $method eq "DESTROY" 197 0 197
355  
356 0 ▶ if( ref $_[0] and my $code = $_[0]->can( $method ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $_[0] and my $code = $_[0]->can($method)) 0 0 0
Condition0%0/3
ref $_[0] and my $code = $_[0]->can($method)
!ll&&!rl&&r
000
MC/DC0%0/2
ref $_[0] and my $code = $_[0]->can($method)
ref $_[0] my $code = $_[0]->can($method)
Truth table0%0/3
A and B
A= ref $_[0]B= my $code = $_[0]->can($method)
ABresult
0X0
100
111
357 # It's possible that the lazy loading by ->can has just created this method
358 0 warnings::warn( deprecated => "Relying on AUTOLOAD to provide $method" );
359 0 ▶ unless( defined &{$method} ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined &{$method;}) 0 0 0
360 0 my $metapkg = meta::get_this_package;
361 0 $metapkg->add_named_sub( $method => $code );
362 }
363 0 goto &$code;
364 }
365  
366 0 ▶ my $class = ref $_[0] || $_[0];
Condition0%0/3
ref $_[0] || $_[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
ref $_[0] || $_[0]
ref $_[0] $_[0]
Truth table0%0/3
A || B
A= ref $_[0]B= $_[0]
ABresult
1X1
011
000
367 0 croak qq(Cannot locate object method "$method" via package "$class");
368 }
369  
370 =head1 OTHER METHODS
371  
372 As well as the above, it is likely the subclass will provide accessors to
373 directly obtain the components of its representation in the specific space.
374 For more detail, see the documentation for the specific subclass in question.
375  
376 =cut
377  
378 =head1 SUBCLASS METHODS
379  
380 This base class is intended to be subclassed to provide more color spaces.
381  
382 =cut
383  
384 =head2 register_color_space
385  
386 $class->register_color_space( $space );
387  
388 A subclass should call this method to register itself as a named color space.
389  
390 =cut
391  
392 =head2 register_palette
393  
394 $class->register_palette( %args );
395  
396 A subclass that provides a fixed set of color values should call this method,
397 to set up automatic conversions that look for the closest match within the
398 set. This conversion process is controlled by the C<%args>:
399  
400 =over 8
401  
402 =item enumerate => STRING or CODE
403  
404 A method name or anonymous CODE reference which will be used to generate the
405 list of color values.
406  
407 =item enumerate_once => STRING or CODE
408  
409 As per C<enumerate>, but will be called only once and the results cached.
410  
411 =back
412  
413 This method creates a new class method on the calling package, called
414 C<closest_to>.
415  
416 =head2 closest_to
417  
418 $color = $pkg->closest_to( $orig, $space );
419  
420 Returns the color in the space closest to the given value. The distance is
421 measured in the named space; defaulting to C<rgb> if this is not provided.
422  
423 In the case of a tie, where two or more colors have the same distance from the
424 target, the first one will be chosen.
425  
426 =cut
427  
428 sub register_palette
429 21 ▶{
Subroutine register_palette: called CC=4 SCAR=15.8
Pod documented
430 21 my $pkg = shift;
431 21 my %args = @_;
432  
433 21 my $enumerate;
434  
435 21 ▶ if( $args{enumerate} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'enumerate'}) { } 11 10 21
elsif (my $enumerate_once = $args{'enumerate_once'}) { } 10 0 10
436 11 $enumerate = $args{enumerate};
437 }
438 elsif( my $enumerate_once = $args{enumerate_once} ) {
439 10 my @colors;
440 1 ▶ $enumerate = sub {
Subroutine __ANON__: called CC=2 SCAR=7.1
441 1 my $class = shift;
442 1 ▶ @colors = $class->$enumerate_once unless @colors;
Branch: 1 branch
BranchTrueFalseTotal
unless @colors 1 0 1
443 1 return @colors;
444 }
445 10 }
446 else {
447 0 croak "Require 'enumerate' or 'enumerate_once'";
448 }
449  
450 21 my $metapkg = meta::package->get( $pkg );
451  
452 21 my %cache;
453 1 ▶ $metapkg->add_named_sub( closest_to => sub {
Subroutine __ANON__: called CC=4 SCAR=17.5
454 1 my $class = shift;
455 1 my ( $orig, $space ) = @_;
456  
457 1 ▶ $space ||= "rgb";
Condition25%1/4
$space ||= "rgb"
l!l
10
$space ||= "rgb"
l!l
00
MC/DC0%0/1
$space ||= "rgb"
$space
MC/DC0%0/1
$space ||= "rgb"
$space
Truth table50%1/2
A
A= $space
Aresult
11
00
Truth table0%0/2
A
A= $space
Aresult
11
00
458  
459 # Prevent the cache getting -too- big
460 1 ▶ delete $cache{ each %cache } while keys %cache > MAX_CACHE_SIZE;
Branch: 2 branches
BranchTrueFalseTotal
keys %cache > 1000 and do { delete $cache{each %cache} } 0 1 1
keys %cache > 1000 and do { delete $cache{each %cache} } 0 0 0
461  
462 1 $orig = $orig->convert_to( $space );
463 1 my $dst = "dst_${space}_cheap";
464  
465 1 my $key = join ",", $space, $orig->$space;
466  
467 return $cache{$key} //=
468 1 ▶ min_by { $orig->$dst( $_->convert_to( $space ) ) } $class->$enumerate;
Subroutine __ANON__: called CC=1 SCAR=2.2
Condition16%1/6
$cache{$key} //= &min_by(sub { $orig->$dst($_->convert_to($space)); } , $class->$enumerate)
l!l&&r!l&&!r
010
$cache{$key} //= &min_by(sub { $orig->$dst($_->convert_to($space)); } , $class->$enumerate)
l!l&&r!l&&!r
000
MC/DC0%0/2
$cache{$key} //= &min_by(sub { $orig->$dst($_->convert_to($space)); } , $class->$enumerate)
$cache{$key} &min_by(sub { $orig->$dst($_->convert_to($space)); } , $class->$enumerate)
MC/DC0%0/2
$cache{$key} //= &min_by(sub { $orig->$dst($_->convert_to($space)); } , $class->$enumerate)
$cache{$key} &min_by(sub { $orig->$dst($_->convert_to($space)); } , $class->$enumerate)
Truth table33%1/3
A //= B
A= $cache{$key}B= &min_by(sub { $orig->$dst($_->convert_to($space)); } , $class->$enumerate)
ABresult
1X1
011
000
Truth table0%0/3
A //= B
A= $cache{$key}B= &min_by(sub { $orig->$dst($_->convert_to($space)); } , $class->$enumerate)
ABresult
1X1
011
000
469 21 } );
470  
471 21 foreach my $space (qw( rgb hsv hsl )) {
472 1 ▶ $metapkg->add_named_sub( "new_from_${space}" => sub {
Subroutine __ANON__: called CC=1 SCAR=1.2
Pod undocumented
473 0 my $class = shift;
474 0 my ( $rgb ) = @_;
475 0 return $pkg->closest_to( $rgb, $space );
476 63 } );
477 }
478  
479 0 ▶ $metapkg->add_named_sub( new_rgb => sub {
Subroutine __ANON__: not called CC=1 SCAR=6.9
480 0 my $class = shift;
481 0 return $class->closest_to( Convert::Color::RGB->new( @_ ), "rgb" );
482 21 } );
483 }
484  
485 =head1 AUTHOR
486  
487 Paul Evans <leonerd@leonerd.org.uk>
488  
489 =cut
490  
491 18 0x55AA;
« Color/X11.pm ↑ Summary Color/HSV.pm »