File Coverage

blib/lib/Color/Palette/Types.pm
Criterion Covered Total %
statement 18 18 100.0
branch n/a
condition n/a
subroutine 6 6 100.0
pod n/a
total 24 24 100.0


line stmt bran cond sub pod time code
1             package Color::Palette::Types 0.100004;
2 1     1   7 use strict;
  1         3  
  1         31  
3 1     1   5 use warnings;
  1         3  
  1         33  
4             # ABSTRACT: type constraints for use with Color::Palette
5              
6             #pod =head1 BEAR WITH ME
7             #pod
8             #pod I'm not yet sure how best to document a type library.
9             #pod
10             #pod =head1 TYPES
11             #pod
12             #pod The following types are defined:
13             #pod
14             #pod Color - a Graphics::Color object
15             #pod Palette - a Color::Palette::Color object
16             #pod ColorName - a valid color name: /\A[a-z][-a-z0-9]*\z/i
17             #pod
18             #pod ColorDict - a hash mapping ColorName to Color
19             #pod RecursiveColorDict - a hash mapping ColorName to (Color | ColorName)
20             #pod
21             #pod HexColorStr - a string like #000 or #ababab
22             #pod ArrayRGB - an ArrayRef of three Bytes
23             #pod Byte - and Int from 0 to 255
24             #pod
25             #pod Colors can be coerced from ArrayRGB or HexColorStr, and dicts of colors try to
26             #pod coerce, too.
27             #pod
28             #pod =cut
29              
30 1     1   823 use Graphics::Color::RGB;
  1         757399  
  1         49  
31              
32 1     1   9 use List::Util 1.33 qw(all);
  1         23  
  1         94  
33              
34 1         8 use MooseX::Types -declare => [ qw(
35             Color Palette
36             ColorName
37             ColorDict
38             RecursiveColorDict
39             HexColorStr
40             ArrayRGB
41             Byte
42 1     1   8 ) ];
  1         2  
43              
44 1     1   8348 use MooseX::Types::Moose qw(Str Int ArrayRef HashRef);
  1         3  
  1         9  
45              
46             class_type Color, { class => 'Graphics::Color::RGB' };
47             class_type Palette, { class => 'Color::Palette' };
48              
49             subtype ColorName, as Str, where { /\A[a-z][-a-z0-9]*\z/i };
50              
51             subtype HexColorStr, as Str, where { /\A#?(?:[0-9a-f]{3}|[0-9a-f]{6})\z/i };
52              
53             subtype Byte, as Int, where { $_ >= 0 and $_ <= 255 };
54              
55             subtype ArrayRGB, as ArrayRef[Byte], where { @$_ == 3 };
56              
57             coerce Color, from ArrayRGB, via {
58             Graphics::Color::RGB->new({
59             red => $_->[0] / 255,
60             green => $_->[1] / 255,
61             blue => $_->[2] / 255,
62             })
63             };
64              
65             coerce Color, from HexColorStr, via {
66             my $copy = $_;
67             $copy =~ s/\A#//;
68             my $width = length $copy == 3 ? 2 : 1;
69              
70             my @rgb = $copy =~ /\A([0-9a-f]{1,2})([0-9a-f]{1,2})([0-9a-f]{1,2})\z/;
71             Graphics::Color::RGB->new({
72             red => hex($rgb[0] x $width) / 255,
73             green => hex($rgb[1] x $width) / 255,
74             blue => hex($rgb[2] x $width) / 255,
75             });
76             };
77              
78             subtype ColorDict, as HashRef[ Color ], where {
79             all { is_ColorName($_) } keys %$_;
80             };
81              
82             coerce ColorDict, from HashRef, via {
83             my $input = $_;
84             return { map {; $_ => to_Color($input->{$_}) } keys %$_ };
85             };
86              
87             subtype RecursiveColorDict, as HashRef[ Color | ColorName ], where {
88             all { is_ColorName($_) } keys %$_
89             };
90              
91             coerce RecursiveColorDict, from HashRef, via {
92             my $input = $_;
93             my %output;
94             for my $name (keys %$input) {
95             my $val = $input->{ $name };
96             $output{ $name } = $val, next unless ref $val or is_HexColorStr($val);
97             $output{ $name } = to_Color($val);
98             }
99              
100             return \%output
101             };
102              
103             1;
104              
105             __END__
106              
107             =pod
108              
109             =encoding UTF-8
110              
111             =head1 NAME
112              
113             Color::Palette::Types - type constraints for use with Color::Palette
114              
115             =head1 VERSION
116              
117             version 0.100004
118              
119             =head1 PERL VERSION
120              
121             This library should run on perls released even a long time ago. It should work
122             on any version of perl released in the last five years.
123              
124             Although it may work on older versions of perl, no guarantee is made that the
125             minimum required version will not be increased. The version may be increased
126             for any reason, and there is no promise that patches will be accepted to lower
127             the minimum required perl.
128              
129             =head1 BEAR WITH ME
130              
131             I'm not yet sure how best to document a type library.
132              
133             =head1 TYPES
134              
135             The following types are defined:
136              
137             Color - a Graphics::Color object
138             Palette - a Color::Palette::Color object
139             ColorName - a valid color name: /\A[a-z][-a-z0-9]*\z/i
140              
141             ColorDict - a hash mapping ColorName to Color
142             RecursiveColorDict - a hash mapping ColorName to (Color | ColorName)
143              
144             HexColorStr - a string like #000 or #ababab
145             ArrayRGB - an ArrayRef of three Bytes
146             Byte - and Int from 0 to 255
147              
148             Colors can be coerced from ArrayRGB or HexColorStr, and dicts of colors try to
149             coerce, too.
150              
151             =head1 AUTHOR
152              
153             Ricardo SIGNES <cpan@semiotic.systems>
154              
155             =head1 COPYRIGHT AND LICENSE
156              
157             This software is copyright (c) 2022 by Ricardo SIGNES.
158              
159             This is free software; you can redistribute it and/or modify it under
160             the same terms as the Perl 5 programming language system itself.
161              
162             =cut