File: Color.pm

package info (click to toggle)
libconvert-color-perl 0.08-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 256 kB
  • sloc: perl: 1,546; makefile: 2
file content (448 lines) | stat: -rw-r--r-- 10,588 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
#  You may distribute under the terms of either the GNU General Public License
#  or the Artistic License (the same terms as Perl itself)
#
#  (C) Paul Evans, 2009,2010 -- leonerd@leonerd.org.uk

package Convert::Color;

use strict;
use warnings;

use Carp;

use List::UtilsBy qw( min_by );

use Module::Pluggable require => 0,
                      search_path => [ 'Convert::Color' ];
my @plugins = Convert::Color->plugins;

our $VERSION = '0.08';

=head1 NAME

C<Convert::Color> - color space conversions and named lookups

=head1 SYNOPSIS

 use Convert::Color;

 my $color = Convert::Color->new( 'hsv:76,0.43,0.89' );

 my ( $red, $green, $blue ) = $color->rgb;

 # GTK uses 16-bit values
 my $gtk_col = Gtk2::Gdk::Color->new( $color->as_rgb16->rgb16 );

 # HTML uses #rrggbb in hex
 my $html = '<td bgcolor="#' . $color->as_rgb8->hex . '">';

=head1 DESCRIPTION

This module provides conversions between commonly used ways to express colors.
It provides conversions between color spaces such as RGB and HSV, and it
provides ways to look up colors by a name.

This class provides a base for subclasses which represent particular color
values in particular spaces. The base class provides methods to represent the
color in a few convenient forms, though subclasses may provide more specific
details for the space in question.

For more detail, read the documentation on these classes; namely:

=over 4

=item *

L<Convert::Color::RGB> - red/green/blue as floats between 0 and 1

=item *

L<Convert::Color::RGB8> - red/green/blue as 8-bit integers

=item *

L<Convert::Color::RGB16> - red/green/blue as 16-bit integers

=item *

L<Convert::Color::HSV> - hue/saturation/value

=item *

L<Convert::Color::HSL> - hue/saturation/lightness

=item *

L<Convert::Color::CMY> - cyan/magenta/yellow

=item *

L<Convert::Color::CMYK> - cyan/magenta/yellow/key (blackness)

=back

The following classes are subclasses of one of the above, which provide a way
to access predefined colors by names:

=over 4

=item *

L<Convert::Color::VGA> - named lookup for the basic VGA colors

=item *

L<Convert::Color::X11> - named lookup of colors from X11's F<rgb.txt>

=back

=cut

=head1 CONSTRUCTOR

=cut

my $_space2class_cache_initialised;
my %_space2class_cache; # {$space} = $class
my %_class2space_cache; # {$class} = $space

# doc'ed later for readability...
sub register_color_space
{
   my $class = shift;
   my ( $space ) = @_;

   exists $_space2class_cache{$space} and croak "Color space $space is already defined";
   exists $_class2space_cache{$class} and croak "Class $class already declared a color space";

   $_space2class_cache{$space} = $class;
   $_class2space_cache{$class} = $space;

   no strict 'refs';
   *{"as_$space"} = sub { shift->convert_to( $space ) };
}

sub _space2class
{
   my ( $space ) = @_;

   unless( $_space2class_cache_initialised ) {
      $_space2class_cache_initialised++;
      # Initialise the space name to class cache
      foreach my $class ( @plugins ) {
         ( my $file = "$class.pm" ) =~ s{::}{/}g;
         require $file or next;

         $class->can( 'COLOR_SPACE' ) or next;
         my $thisspace = $class->COLOR_SPACE or next;

         warnings::warn( deprecated => "Discovered $class by deprecated COLOR_SPACE method" );

         $class->register_color_space( $thisspace );
      }
   }

   return $_space2class_cache{$space};
}

=head2 $color = Convert::Color->new( STRING )

Return a new value to represent the color specified by the string. This string
should be prefixed by the name of the color space to which it applies. For
example

 rgb:RED,GREEN,BLUE
 rgb8:RRGGBB
 rgb16:RRRRGGGGBBBB
 hsv:HUE,SAT,VAL
 hsl:HUE,SAT,LUM
 cmy:CYAN,MAGENTA,YELLOW
 cmyk:CYAN,MAGENTA,YELLOW,KEY

 vga:NAME
 vga:INDEX

 x11:NAME

For more detail, see the constructor of the color space subclass in question.

=cut

sub new
{
   shift;
   my ( $str ) = @_;

   $str =~ m/^(\w+):(.*)$/ or croak "Unable to parse color name $str";
   ( my $space, $str ) = ( $1, $2 );

   my $class = _space2class( $space ) or croak "Unrecognised color space name '$space'";

   return $class->new( $str );
}

=head1 METHODS

=cut

=head2 ( $red, $green, $blue ) = $color->rgb

Returns the individual red, green and blue color components of the color
value. For RGB values, this is done directly. For values in other spaces, this
is done by first converting them to an RGB value using their C<to_rgb()>
method.

=cut

sub rgb
{
   my $self = shift;
   croak "Abstract method - should be overloaded by ".ref($self);
}

=head1 COLOR SPACE CONVERSIONS

Cross-conversion between color spaces is provided by the C<convert_to()>
method, assisted by helper methods in the two color space classes involved.

When converting C<$color> from color space SRC to color space DEST, the
following operations are attemped, in this order. SRC and DEST refer to the
names of the color spaces, e.g. C<rgb>.

=over 4

=item 1.

If SRC and DEST are equal, return C<$color> as it stands.

=item 2.

If the SRC space's class provides a C<convert_to_DEST> method, use it.

=item 3.

If the DEST space's class provides a C<new_from_SRC> constructor, call it and
pass C<$color>.

=item 4.

If the DEST space's class provides a C<new_rgb> constructor, convert C<$color>
to red/green/blue components then call it.

=item 5.

If none of these operations worked, then throw an exception.

=back

These functions may be called in the following ways:

 $other = $color->convert_to_DEST()
 $other = Dest::Class->new_from_SRC( $color )
 $other = Dest::Class->new_rgb( $color->rgb )

=cut

=head2 $other = $color->convert_to( $space )

Attempt to convert the color into its representation in the given space. See
above for the various ways this may be achieved.

If the relevant subclass has already been loaded (either explicitly, or
implicitly by either the C<new> or C<convert_to> methods), then a specific
conversion method will be installed in the class.

 $other = $color->as_$space

Methods of this form are currently C<AUTOLOAD>ed if they do not yet exist, but
this feature should not be relied upon - see below.

=cut

sub convert_to
{
   my $self = shift;
   my ( $to_space ) = @_;

   my $to_class = _space2class( $to_space ) or croak "Unrecognised color space name '$to_space'";

   my $from_space = $_class2space_cache{ref $self};

   if( $from_space eq $to_space ) {
      # Identity conversion
      return $self;
   }

   my $code;
   if( $code = $self->can( "convert_to_$to_space" ) ) {
      return $code->( $self );
   }
   elsif( $code = $to_class->can( "new_from_$from_space" ) ) {
      return $code->( $to_class, $self );
   }
   elsif( $code = $to_class->can( "new_rgb" ) ) {
      # TODO: check that $self->rgb is overloaded
      return $code->( $to_class, $self->rgb );
   }
   else {
      croak "Cannot convert from space '$from_space' to space '$to_space'";
   }
}

# Fallback implementations in case subclasses don't provide anything better

sub convert_to_rgb
{
   my $self = shift;
   require Convert::Color::RGB;
   return Convert::Color::RGB->new( $self->rgb );
}

=head1 AUTOLOADED CONVERSION METHODS

This class provides C<AUTOLOAD> and C<can> behaviour which automatically
constructs conversion methods. The following method calls are identical:

 $color->convert_to('rgb')
 $color->as_rgb

The generated method will be stored in the package, so that future calls will
not have the AUTOLOAD overhead.

This feature is deprecated and should not be relied upon, due to the delicate
nature of C<AUTOLOAD>.

=cut

# Since this is AUTOLOADed, we can dynamically provide new methods for classes
# discovered at runtime.

sub can
{
   my $self = shift;
   my ( $method ) = @_;

   if( $method =~ m/^as_(.*)$/ ) {
      my $to_space = $1;
      _space2class( $to_space ) or return undef;

      return sub {
         my $self = shift;
         return $self->convert_to( $to_space );
      };
   }

   return $self->SUPER::can( $method );
}

sub AUTOLOAD
{
   my ( $method ) = our $AUTOLOAD =~ m/::([^:]+)$/;

   return if $method eq "DESTROY";

   if( ref $_[0] and my $code = $_[0]->can( $method ) ) {
      # It's possible that the lazy loading by ->can has just created this method
      warnings::warn( deprecated => "Relying on AUTOLOAD to provide $method" );
      no strict 'refs';
      unless( defined &{$method} ) {
         *{$method} = $code;
      }
      goto &$code;
   }

   my $class = ref $_[0] || $_[0];
   croak qq(Cannot locate object method "$method" via package "$class");
}

=head1 OTHER METHODS

As well as the above, it is likely the subclass will provide accessors to
directly obtain the components of its representation in the specific space.
For more detail, see the documentation for the specific subclass in question.

=cut

=head1 SUBCLASS METHODS

This base class is intended to be subclassed to provide more color spaces.

=cut

=head2 $class->register_color_space( $space )

A subclass should call this method to register itself as a named color space.

=cut

=head2 $class->register_palette( %args )

A subclass that provides a fixed set of color values should call this method,
to set up automatic conversions that look for the closest match within the
set. This conversion process is controlled by the C<%args>:

=over 8

=item enumerate => STRING or CODE

A method name or anonymous CODE reference which will be used to generate the
list of color values.

=item enumerate_once => STRING or CODE

As per C<enumerate>, but will be called only once and the results cached.

=back

This conversion process only finds the closest match in RGB space, so it may
not give exact results.

In the case of a tie, where two or more colors have the same distance from the
target, the first one will be chosen.

=cut

sub register_palette
{
   my $pkg = shift;
   my %args = @_;

   my $enumerate;

   if( $args{enumerate} ) {
      $enumerate = $args{enumerate};
   }
   elsif( my $enumerate_once = $args{enumerate_once} ) {
      my @colors;
      $enumerate = sub {
         my $class = shift;
         @colors = $class->$enumerate_once unless @colors;
         return @colors;
      }
   }
   else {
      croak "Require 'enumerate' or 'enumerate_once'";
   }

   no strict 'refs';

   *{"${pkg}::new_from_rgb"} = sub {
      my $class = shift;
      my ( $rgb ) = @_;

      return min_by { $rgb->dst_rgb_cheap( $_ ) } $class->$enumerate;
   };

   *{"${pkg}::new_rgb"} = sub {
      my $class = shift;
      return $class->new_from_rgb( Convert::Color::RGB->new( @_ ) );
   };
}

=head1 AUTHOR

Paul Evans <leonerd@leonerd.org.uk>

=cut

0x55AA;