Color/CMY.pm

filter: Statementstmt 94.4% 34 / 36 Branchbran 60.0% 6 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 11 / 11 Podpod 85.7% 6 / 7 totaltotal 89.0% 57 / 64 CC 7 SCAR 13.4
CC
7
Cov
87%
new
7.9
BEGIN
1.0
BEGIN
1.0
CRAP 3.8
« Color/RGB8.pm ↑ Summary Color/RGB.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::CMY 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( 'cmy' );
13  
14 12 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 NAME
17  
18 C<Convert::Color::CMY> - a color value represented as cyan/magenta/yellow
19  
20 =head1 SYNOPSIS
21  
22 Directly:
23  
24 use Convert::Color::CMY;
25  
26 my $red = Convert::Color::CMY->new( 0, 1, 1 );
27  
28 # Can also parse strings
29 my $pink = Convert::Color::CMY->new( '0,0.3,0.3' );
30  
31 Via L<Convert::Color>:
32  
33 use Convert::Color;
34  
35 my $cyan = Convert::Color->new( 'cmy:1,0,0' );
36  
37 =head1 DESCRIPTION
38  
39 Objects in this class represent a color in CMY space, as a set of three
40 floating-point values in the range 0 to 1.
41  
42 =cut
43  
44 =head1 CONSTRUCTOR
45  
46 =cut
47  
48 =head2 new
49  
50 $color = Convert::Color::CMY->new( $cyan, $magenta, $yellow );
51  
52 Returns a new object to represent the set of values given. These values should
53 be floating-point numbers between 0 and 1. Values outside of this range will
54 be clamped.
55  
56 $color = Convert::Color::CMY->new( $string );
57  
58 Parses C<$string> for values, and construct a new object similar to the above
59 three-argument form. The string should be in the form
60  
61 cyan,magenta,yellow
62  
63 containing the three floating-point values in decimal notation.
64  
65 =cut
66  
67 sub new
68 17 ▶{
Subroutine new: called CC=7 SCAR=20.6
Pod documented
69 17 my $class = shift;
70  
71 17 my ( $c, $m, $y );
72  
73 17 ▶ if( @_ == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 2 15 17
elsif (@_ == 3) { } 15 0 15
74 2 local $_ = $_[0];
75 2 ▶ if( m/^(\d+(?:\.\d+)?),(\d+(?:\.\d+)?),(\d+(?:\.\d+)?)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(\d+(?:\.\d+)?),(\d+(?:\.\d+)?),(\d+(?:\.\d+)?)$/u) { } 2 0 2
76 2 ( $c, $m, $y ) = ( $1, $2, $3 );
77 }
78 else {
79 0 croak "Unrecognised CMY string spec '$_'";
80 }
81 }
82 elsif( @_ == 3 ) {
83 15 ( $c, $m, $y ) = @_;
84 }
85 else {
86 0 croak "usage: Convert::Color::CMY->new( SPEC ) or ->new( C, M, Y )";
87 }
88  
89 # Clamp
90 17 for ( $c, $m, $y ) {
91 51 ▶ $_ = 0 if $_ < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $_ < 0 0 51 51
92 51 ▶ $_ = 1 if $_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $_ > 1 0 51 51
93 }
94  
95 17 return bless [ $c, $m, $y ], $class;
96 }
97  
98 =head1 METHODS
99  
100 =cut
101  
102 =head2 cyan
103  
104 $c = $color->cyan;
105  
106 =head2 magenta
107  
108 $m = $color->magenta;
109  
110 =head2 yellow
111  
112 $y = $color->yellow;
113  
114 Accessors for the three components of the color.
115  
116 =cut
117  
118 # Simple accessors
119 19 ▶sub cyan { shift->[0] }
Subroutine cyan: called CC=1 SCAR=0.0
Pod documented
120 19 ▶sub magenta { shift->[1] }
Subroutine magenta: called CC=1 SCAR=0.0
Pod documented
121 19 ▶sub yellow { shift->[2] }
Subroutine yellow: called CC=1 SCAR=0.0
Pod documented
122  
123 =head2 cmy
124  
125 ( $cyan, $magenta, $yellow ) = $color->cmy;
126  
127 Returns the individual cyan, magenta and yellow color components of the color
128 value.
129  
130 =cut
131  
132 sub cmy
133 4 ▶{
Subroutine cmy: called CC=1 SCAR=0.0
Pod documented
134 4 my $self = shift;
135 4 return @$self;
136 }
137  
138 # Conversions
139 sub rgb
140 5 ▶{
Subroutine rgb: called CC=1 SCAR=0.0
Pod documented
141 5 my $self = shift;
142  
143 5 return 1 - $self->cyan, 1 - $self->magenta, 1 - $self->yellow;
144 }
145  
146 sub new_rgb
147 5 ▶{
Subroutine new_rgb: called CC=1 SCAR=0.0
Pod undocumented
148 5 my $class = shift;
149 5 my ( $r, $g, $b ) = @_;
150  
151 5 $class->new( 1 - $r, 1 - $g, 1 - $b );
152 }
153  
154 =head1 SEE ALSO
155  
156 =over 4
157  
158 =item *
159  
160 L<Convert::Color> - color space conversions
161  
162 =item *
163  
164 L<Convert::Color::RGB> - a color value represented as red/green/blue
165  
166 =item *
167  
168 L<Convert::Color::CMYK> - a color value represented as cyan/magenta/yellow/key
169  
170 =back
171  
172 =head1 AUTHOR
173  
174 Paul Evans <leonerd@leonerd.org.uk>
175  
176 =cut
177  
178 12 0x55AA;
« Color/RGB8.pm ↑ Summary Color/RGB.pm »