1 package Convert
::Color
::HUSL
;
6 use parent qw
/Convert::Color/;
8 use Convert
::Color
::XYZ
;
9 use Convert
::Color
::LUV
;
10 use Convert
::Color
::LCh
;
11 use List
::Util qw
/min/;
12 use Math
::Trig qw
/:pi/;
15 *MAT_R
= *Convert
::Color
::XYZ
::MAT_R
;
16 *MAT_G
= *Convert
::Color
::XYZ
::MAT_G
;
17 *MAT_B
= *Convert
::Color
::XYZ
::MAT_B
;
19 *KAPPA
= *Convert
::Color
::LUV
::KAPPA
;
20 *EPS
= *Convert
::Color
::LUV
::EPS
;
23 our $VERSION = '0.001';
25 __PACKAGE__
->register_color_space('husl');
28 my ($class, $h, $s, $l) = @_;
29 ($h, $s, $l) = split /,/s, $h unless defined $s;
30 bless [$h, $s, $l], $class
41 my $sub1 = ($l + 16) ** 3 / 1_560_896
;
42 my $sub2 = $sub1 > EPS ?
$sub1 : $l / KAPPA
;
45 for (MAT_R
, MAT_G
, MAT_B
) {
46 my ($m1, $m2, $m3) = @
$_;
48 my $top1 = (284_517
* $m1 - 94_839
* $m3) * $sub2;
49 my $top2 = (838_422
* $m3 + 769_860
* $m2 + 731_718
* $m1) * $l * $sub2 - 769_860
* $_ * $l;
50 my $bottom = (632_260
* $m3 - 126_452
* $m2) * $sub2 + 126_452
* $_;
51 push @ret, [$top1 / $bottom, $top2 / $bottom]
58 sub _length_of_ray_until_intersect
{
59 my ($theta, $line) = @_;
61 my $len = $n / (sin ($theta) - $m * cos $theta);
66 sub max_chroma_for_lh
{
67 my ($self, $l, $h) = @_;
68 my $hrad = $h / 180 * pi
;
70 _length_of_ray_until_intersect
$hrad, $_
76 my ($h, $s, $l) = @
$self;
77 return Convert
::Color
::LCh
->new(100, 0, $h) if $l > 99.9999999;
78 return Convert
::Color
::LCh
->new(0, 0, $h) if $l < 0.00000001;
79 my $max = $self->max_chroma_for_lh($l, $h);
80 my $c = $max / 100 * $s;
81 Convert
::Color
::LCh
->new($l, $c, $h)
85 my ($class, $lch) = @_;
86 my ($l, $c, $h) = @
$lch;
87 return $class->new($h, 0, 100) if $l > 99.9999999;
88 return $class->new($h, 0, 0) if $l < 0.00000001;
89 my $max = $class->max_chroma_for_lh($l, $h);
90 my $s = $c / $max * 100;
91 $class->new($h, $s, $l)
94 sub rgb
{ shift->convert_to_lch->rgb }
95 sub new_rgb
{ shift->new_from_lch(Convert
::Color
::LCh
->new_rgb(@_)) }
104 Convert::Color::HUSL - a color value in the HUSL color space
108 use Convert::Color::HUSL;
109 my $red = Convert::Color::HUSL->new(12.17705, 100, 53.23712);
110 my $green = Convert::Color::HUSL->new('127.71501,100,87.73552');
113 my $blue = Convert::Color->new('husl:265.87432,100,32.30087');
115 say $red->H; # 12.17705
117 say $red->L; # 53.23712
118 say join ',', $blue->hsl; # 265.87432,100,32.30087
122 Objects of this class represent colors in the HUSL color space, revision 3.
128 =item Convert::Color::HUSL->B<new>(I<$h>, I<$s>, I<$l>)
130 Construct a color from its components.
132 =item Convert::Color::HUSL->B<new>(I<"$h,$s,$l">)
134 Construct a color from a string. The string should contain the three
135 components, separated by commas.
143 Accessors for the three components of the color.
147 Returns the three components as a list.
153 L<Convert::Color>, L<http://www.husl-colors.org/>
157 Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
159 =head1 COPYRIGHT AND LICENSE
161 Copyright (C) 2015 by Marius Gavrilescu
163 This library is free software; you can redistribute it and/or modify
164 it under the same terms as Perl itself, either Perl version 5.20.2 or,
165 at your option, any later version of Perl 5 you may have available.