Color/CMYK.pm

filter: Statementstmt 96.7% 59 / 61 Branchbran 71.4% 10 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 17 / 17 Podpod 58.3% 7 / 12 totaltotal 89.4% 93 / 104 CC 9 SCAR 11.3
CC
9
Cov
92%
new
7.9
cmy
2.0
new_cmy
2.0
CRAP 3.1
« Color/RGB.pm ↑ Summary Color/HueChromaBased.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-2022 -- leonerd@leonerd.org.uk
5  
6 package Convert::Color::CMYK 0.18;
7  
8 12 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 12 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 12 ▶use base qw( Convert::Color );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 12 __PACKAGE__->register_color_space( 'cmyk' );
13  
14 12 ▶use List::Util qw( min );
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
17  
18 =head1 NAME
19  
20 C<Convert::Color::CMYK> - a color value represented as cyan/magenta/yellow/key
21  
22 =head1 SYNOPSIS
23  
24 Directly:
25  
26 use Convert::Color::CMYK;
27  
28 my $red = Convert::Color::CMYK->new( 0, 1, 1, 0 );
29  
30 # Can also parse strings
31 my $pink = Convert::Color::CMYK->new( '0,0.3,0.3,0' );
32  
33 Via L<Convert::Color>:
34  
35 use Convert::Color;
36  
37 my $cyan = Convert::Color->new( 'cmyk:1,0,0,0' );
38  
39 =head1 DESCRIPTION
40  
41 Objects in this class represent a color in CMYK space, as a set of four
42 floating-point values in the range 0 to 1.
43  
44 =cut
45  
46 =head1 CONSTRUCTOR
47  
48 =cut
49  
50 =head2 new
51  
52 $color = Convert::Color::CMYK->new( $cyan, $magenta, $yellow, $key );
53  
54 Returns a new object to represent the set of values given. These values should
55 be floating-point numbers between 0 and 1. Values outside of this range will
56 be clamped.
57  
58 $color = Convert::Color::CMYK->new( $string );
59  
60 Parses C<$string> for values, and construct a new object similar to the above
61 three-argument form. The string should be in the form
62  
63 cyan,magenta,yellow,key
64  
65 containing the three floating-point values in decimal notation.
66  
67 =cut
68  
69 sub new
70 18 ▶{
Subroutine new: called CC=7 SCAR=20.6
Pod documented
71 18 my $class = shift;
72  
73 18 my ( $c, $m, $y, $k );
74  
75 18 ▶ if( @_ == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 3 15 18
elsif (@_ == 4) { } 15 0 15
76 3 local $_ = $_[0];
77 3 ▶ if( m/^(\d+(?:\.\d+)?),(\d+(?:\.\d+)?),(\d+(?:\.\d+)?),(\d+(?:\.\d+)?)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(\d+(?:\.\d+)?),(\d+(?:\.\d+)?),(\d+(?:\.\d+)?),(\d+(?:\.\d+)?)$/u) { } 3 0 3
78 3 ( $c, $m, $y, $k ) = ( $1, $2, $3, $4 );
79 }
80 else {
81 0 croak "Unrecognised CMYK string spec '$_'";
82 }
83 }
84 elsif( @_ == 4 ) {
85 15 ( $c, $m, $y, $k ) = @_;
86 }
87 else {
88 0 croak "usage: Convert::Color::CMYK->new( SPEC ) or ->new( C, M, Y, K )";
89 }
90  
91 # Clamp
92 18 for ( $c, $m, $y, $k ) {
93 72 ▶ $_ = 0 if $_ < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $_ < 0 0 72 72
94 72 ▶ $_ = 1 if $_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $_ > 1 0 72 72
95 }
96  
97 18 return bless [ $c, $m, $y, $k ], $class;
98 }
99  
100 =head1 METHODS
101  
102 =cut
103  
104 =head2 cyan
105  
106 $c = $color->cyan;
107  
108 =head2 magenta
109  
110 $m = $color->magenta;
111  
112 =head2 yellow
113  
114 $y = $color->yellow;
115  
116 =head2 key
117  
118 $k = $color->key;
119  
120 Accessors for the four components of the color.
121  
122 =cut
123  
124 # Simple accessors
125 18 ▶sub cyan { shift->[0] }
Subroutine cyan: called CC=1 SCAR=0.0
Pod documented
126 18 ▶sub magenta { shift->[1] }
Subroutine magenta: called CC=1 SCAR=0.0
Pod documented
127 18 ▶sub yellow { shift->[2] }
Subroutine yellow: called CC=1 SCAR=0.0
Pod documented
128 25 ▶sub key { shift->[3] }
Subroutine key: called CC=1 SCAR=0.0
Pod documented
129  
130 =head2 black
131  
132 $k = $color->black;
133  
134 An alias to C<key>
135  
136 =cut
137  
138 12 *black = \&key; # alias
139  
140 =head2 cmyk
141  
142 ( $cyan, $magenta, $yellow, $key ) = $color->cmyk;
143  
144 Returns the individual cyan, magenta, yellow and key components of the color
145 value.
146  
147 =cut
148  
149 sub cmyk
150 5 ▶{
Subroutine cmyk: called CC=1 SCAR=0.0
Pod documented
151 5 my $self = shift;
152 5 return @$self;
153 }
154  
155 # Conversions
156  
157 sub cmy
158 7 ▶{
Subroutine cmy: called CC=2 SCAR=6.9
Pod undocumented
159 7 my $self = shift;
160  
161 7 ▶ if( $self->key == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->key == 1) 2 5 7
162 # Pure black
163 2 return ( 1, 1, 1 );
164 }
165  
166 5 my $k = $self->key;
167 5 my $w = 1 - $k;
168  
169 5 return ( ($self->cyan * $w) + $k, ($self->magenta * $w) + $k, ($self->yellow * $w) + $k );
170 }
171  
172 sub rgb
173 5 ▶{
Subroutine rgb: called CC=1 SCAR=0.0
Pod documented
174 5 my $self = shift;
175 5 my ( $c, $m, $y ) = $self->cmy;
176 5 return ( 1 - $c, 1 - $m, 1 - $y );
177 }
178  
179 sub new_cmy
180 7 ▶{
Subroutine new_cmy: called CC=2 SCAR=6.9
Pod undocumented
181 7 my $class = shift;
182 7 my ( $c, $m, $y ) = @_;
183  
184 7 my $k = min( $c, $m, $y );
185  
186 7 ▶ if( $k == 1 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($k == 1) { } 2 5 7
187 # Pure black
188 2 return $class->new( 0, 0, 0, 1 );
189 }
190 else {
191 # Rescale other components around key
192 5 my $w = 1 - $k; # whiteness
193 5 return $class->new( ($c - $k) / $w, ($m - $k) / $w, ($y - $k) / $w, $k );
194 }
195 }
196  
197 sub new_rgb
198 5 ▶{
Subroutine new_rgb: called CC=1 SCAR=0.0
Pod undocumented
199 5 my $class = shift;
200 5 my ( $r, $g, $b ) = @_;
201  
202 5 return $class->new_cmy( 1-$r, 1-$g, 1-$b );
203 }
204  
205 sub convert_to_cmy
206 2 ▶{
Subroutine convert_to_cmy: called CC=1 SCAR=0.0
Pod undocumented
207 2 my $self = shift;
208 2 require Convert::Color::CMY;
209 2 return Convert::Color::CMY->new( $self->cmy );
210 }
211  
212 sub new_from_cmy
213 2 ▶{
Subroutine new_from_cmy: called CC=1 SCAR=0.0
Pod undocumented
214 2 my $class = shift;
215 2 my ( $cmy ) = @_;
216 2 return $class->new_cmy( $cmy->cyan, $cmy->magenta, $cmy->yellow );
217 }
218  
219 =head1 SEE ALSO
220  
221 =over 4
222  
223 =item *
224  
225 L<Convert::Color> - color space conversions
226  
227 =item *
228  
229 L<Convert::Color::CMY> - a color value represented as cyan/magenta/yellow
230  
231 =back
232  
233 =head1 AUTHOR
234  
235 Paul Evans <leonerd@leonerd.org.uk>
236  
237 =cut
238  
239 12 0x55AA;
« Color/RGB.pm ↑ Summary Color/HueChromaBased.pm »