Color/HSL.pm

filter: Statementstmt 96.8% 62 / 64 Branchbran 71.8% 23 / 32 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 14 / 14 Podpod 90.0% 9 / 10 totaltotal 90.0% 108 / 120 CC 19 SCAR 17.5
CC
19
Cov
89%
new
10.7
rgb
8.0
new_rgb
3.0
CRAP 5.7
« Color/HSV.pm ↑ Summary Color/VGA.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::HSL 0.18;
7  
8 13 ▶use v5.14;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 13 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 13 ▶use base qw( Convert::Color::HueChromaBased );
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 13 __PACKAGE__->register_color_space( 'hsl' );
13  
14 13 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 =head1 NAME
17  
18 C<Convert::Color::HSL> - a color value represented as hue/saturation/lightness
19  
20 =head1 SYNOPSIS
21  
22 Directly:
23  
24 use Convert::Color::HSL;
25  
26 my $red = Convert::Color::HSL->new( 0, 1, 0.5 );
27  
28 # Can also parse strings
29 my $pink = Convert::Color::HSL->new( '0,1,0.8' );
30  
31 Via L<Convert::Color>:
32  
33 use Convert::Color;
34  
35 my $cyan = Convert::Color->new( 'hsl:300,1,0.5' );
36  
37 =head1 DESCRIPTION
38  
39 Objects in this class represent a color in HSL space, as a set of three
40 floating-point values. Hue is stored as a value in degrees, in the range
41 0 to 360 (exclusive). Saturation and lightness are in the range 0 to 1.
42  
43 This color space may be considered as a cylinder, of height and radius 1. Hue
44 represents the position of the color as the angle around the axis, the
45 saturation as the distance from the axis, and the lightness the height above
46 the base. In this shape, the entire base of the cylinder is pure black, the
47 axis through the centre represents the range of greys, and the entire top of
48 the cylinder is pure white. The circumference of the circular cross-section
49 midway along the axis contains the pure-saturated color wheel.
50  
51 Because both surfaces of this cylinder contain pure black or white discs, a
52 closely-related color space can be created by reshaping the cylinder into a
53 bi-cone such that the top and bottom of the cylinder become single points. The
54 radius from the axis of this shape is called the chroma (though this is a
55 different definition of "chroma" than that used by CIE).
56  
57 While the components of this space are called Hue-Chroma-Lightness, it should
58 not be confused with the similarly-named Hue-Chroma-Luminance (HCL) space.
59  
60 =cut
61  
62 =head1 CONSTRUCTOR
63  
64 =cut
65  
66 =head2 new
67  
68 $color = Convert::Color::HSL->new( $hue, $saturation, $lightness );
69  
70 Returns a new object to represent the set of values given. The hue should be
71 in the range 0 to 360 (exclusive), and saturation and lightness should be
72 between 0 and 1. Values outside of these ranges will be clamped.
73  
74 $color = Convert::Color::HSL->new( $string );
75  
76 Parses C<$string> for values, and construct a new object similar to the above
77 three-argument form. The string should be in the form
78  
79 hue,saturation,lightnes
80  
81 containing the three floating-point values in decimal notation.
82  
83 =cut
84  
85 sub new
86 23 ▶{
Subroutine new: called CC=9 SCAR=23.7
Pod documented
87 23 my $class = shift;
88  
89 23 my ( $h, $s, $l );
90  
91 23 ▶ if( @_ == 1 ) {
Branch: 2 branches
BranchTrueFalseTotal
if (@_ == 1) { } 4 19 23
elsif (@_ == 3) { } 19 0 19
92 4 local $_ = $_[0];
93 4 ▶ if( m/^(\d+(?:\.\d+)?),(\d+(?:\.\d+)?),(\d+(?:\.\d+)?)$/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (/^(\d+(?:\.\d+)?),(\d+(?:\.\d+)?),(\d+(?:\.\d+)?)$/u) { } 4 0 4
94 4 ( $h, $s, $l ) = ( $1, $2, $3 );
95 }
96 else {
97 0 croak "Unrecognised HSL string spec '$_'";
98 }
99 }
100 elsif( @_ == 3 ) {
101 19 ( $h, $s, $l ) = @_;
102 }
103 else {
104 0 croak "usage: Convert::Color::HSL->new( SPEC ) or ->new( H, S, L )";
105 }
106  
107 # Clamp
108 23 for ( $s, $l ) {
109 46 ▶ $_ = 0 if $_ < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $_ < 0 0 46 46
110 46 ▶ $_ = 1 if $_ > 1;
Branch: 1 branch
BranchTrueFalseTotal
if $_ > 1 0 46 46
111 }
112  
113 # Fit to range [0,360)
114 23 ▶ $h += 360 while $h < 0;
Branch: 1 branch
BranchTrueFalseTotal
$h < 0 and do { $h += 360 } 0 23 23
115 23 ▶ $h -= 360 while $h >= 360;
Branch: 1 branch
BranchTrueFalseTotal
$h >= 360 and do { $h -= 360 } 0 23 23
116  
117 23 return bless [ $h, $s, $l ], $class;
118 }
119  
120 =head1 METHODS
121  
122 =cut
123  
124 =head2 hue
125  
126 $h = $color->hue;
127  
128 =head2 saturation
129  
130 $s = $color->saturation;
131  
132 =head2 lightness
133  
134 $v = $color->lightness;
135  
136 Accessors for the three components of the color.
137  
138 =cut
139  
140 # Simple accessors
141 34 ▶sub hue { shift->[0] }
Subroutine hue: called CC=1 SCAR=0.0
Pod documented
142 12 ▶sub saturation { shift->[1] }
Subroutine saturation: called CC=1 SCAR=0.0
Pod documented
143 36 ▶sub lightness { shift->[2] }
Subroutine lightness: called CC=1 SCAR=0.0
Pod documented
144  
145 =head2 chroma
146  
147 $c = $color->chroma;
148  
149 Returns the derived property of "chroma", which maps the color space onto a
150 bicone instead of a cylinder. This more closely measures the intuitive concept
151 of how "colorful" the color is than the saturation value and is useful for
152 distance calculations.
153  
154 =cut
155  
156 sub chroma
157 30 ▶{
Subroutine chroma: called CC=2 SCAR=6.9
Pod documented
158 30 my $self = shift;
159 30 my ( undef, $s, $l ) = $self->hsl;
160  
161 30 ▶ if( $l > 0.5 ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($l > 0.5) { } 3 27 30
162 # upper bicone
163 3 return 2 * $s * ( $l - 1 );
164 }
165 else {
166 # lower bicone
167 27 return 2 * $s * $l;
168 }
169 }
170  
171 =head2 hsl
172  
173 ( $hue, $saturation, $lightness ) = $color->hsl;
174  
175 Returns the individual hue, saturation and lightness components of the color
176 value.
177  
178 =cut
179  
180 sub hsl
181 39 ▶{
Subroutine hsl: called CC=1 SCAR=0.0
Pod documented
182 39 my $self = shift;
183 39 return @$self;
184 }
185  
186 # Conversions
187 sub rgb
188 5 ▶{
Subroutine rgb: called CC=8 SCAR=20.8
Pod documented
189 5 my $self = shift;
190  
191 # See also
192 # http://en.wikipedia.org/wiki/HSV_color_space
193  
194 5 my ( $h, $s, $l ) = $self->hsl;
195  
196 5 ▶ my $q = $l < 0.5 ? $l * ( 1 + $s )
Branch: 1 branch
BranchTrueFalseTotal
$l < 0.5 ? : 1 4 5
197 : $l + $s - ( $l * $s );
198  
199 5 my $p = 2 * $l - $q;
200  
201 # Modify the algorithm slightly, so we scale this up by 6
202 5 my $hk = $h / 60;
203  
204 5 my $tr = $hk + 2;
205 5 my $tg = $hk;
206 5 my $tb = $hk - 2;
207  
208 5 for ( $tr, $tg, $tb ) {
209 15 ▶ $_ += 6 while $_ < 0;
Branch: 1 branch
BranchTrueFalseTotal
$_ < 0 and do { $_ += 6 } 3 15 18
210 15 ▶ $_ -= 6 while $_ > 6;
Branch: 1 branch
BranchTrueFalseTotal
$_ > 6 and do { $_ -= 6 } 0 15 15
211 }
212  
213 return map {
214 5 ▶ $_ < 1 ? $p + ( ( $q - $p ) * $_ ) :
Branch: 3 branches
BranchTrueFalseTotal
$_ < 1 ? : 4 11 15
$_ < 3 ? : 5 6 11
$_ < 4 ? : 0 6 6
215 $_ < 3 ? $q :
216 $_ < 4 ? $p + ( ( $q - $p ) * ( 4 - $_ ) ) :
217 $p
218 } ( $tr, $tg, $tb );
219 }
220  
221 sub new_rgb
222 5 ▶{
Subroutine new_rgb: called CC=3 SCAR=11.0
Pod undocumented
223 5 my $class = shift;
224 5 my ( $r, $g, $b ) = @_;
225  
226 5 my ( $hue, $min, $max ) = $class->_hue_min_max( $r, $g, $b );
227  
228 5 my $l = ( $max + $min ) / 2;
229  
230 5 ▶ my $s = $min == $max ? 0 :
Branch: 2 branches
BranchTrueFalseTotal
$min == $max ? : 2 3 5
$l <= 0.5 ? : 3 0 3
231 $l <= 1/2 ? ( $max - $min ) / ( 2 * $l ) :
232 ( $max - $min ) / ( 2 - 2 * $l );
233  
234 5 return $class->new( $hue, $s, $l );
235 }
236  
237 =head2 dst_hsl
238  
239 $measure = $color->dst_hsl( $other );
240  
241 Returns a measure of the distance between the two colors. This is the
242 Euclidean distance between the two colors as points in the chroma-adjusted
243 cone space.
244  
245 =cut
246  
247 sub dst_hsl
248 6 ▶{
Subroutine dst_hsl: called CC=1 SCAR=0.0
Pod documented
249 6 my $self = shift;
250 6 my ( $other ) = @_;
251  
252 # ... / sqrt(4)
253 6 return sqrt( $self->dst_hsl_cheap( $other ) ) / 2;
254 }
255  
256 =head2 dst_hsl_cheap
257  
258 $measure = $color->dst_hsl_cheap( $other );
259  
260 Returns a measure of the distance between the two colors. This is used in the
261 calculation of C<dst_hsl> but since it omits the final square-root and scaling
262 it is cheaper to calculate, for use in cases where only the relative values
263 matter, such as when picking the "best match" out of a set of colors. It
264 ranges between 0 for identical colors and 4 for the distance between
265 complementary pure-saturated colors.
266  
267 =cut
268  
269 sub dst_hsl_cheap
270 12 ▶{
Subroutine dst_hsl_cheap: called CC=1 SCAR=0.0
Pod documented
271 12 my $self = shift;
272 12 my ( $other ) = @_;
273  
274 12 my $dl = $self->lightness - $other->lightness;
275  
276 12 return $self->_huechroma_dst_squ( $other ) + $dl*$dl;
277 }
278  
279 =head1 SEE ALSO
280  
281 =over 4
282  
283 =item *
284  
285 L<Convert::Color> - color space conversions
286  
287 =item *
288  
289 L<Convert::Color::RGB> - a color value represented as red/green/blue
290  
291 =item *
292  
293 L<http://en.wikipedia.org/wiki/HSL_and_HSV> - HSL and HSV on Wikipedia
294  
295 =back
296  
297 =head1 AUTHOR
298  
299 Paul Evans <leonerd@leonerd.org.uk>
300  
301 =cut
302  
303 13 0x55AA;
« Color/HSV.pm ↑ Summary Color/VGA.pm »