Set h to 0 when C is very small
[convert-color-husl.git] / lib / Convert / Color / LCh.pm
CommitLineData
1f72b051
MG
1package Convert::Color::LCh;
2
3use 5.014000;
4use strict;
5use warnings;
6use parent qw/Convert::Color/;
7
8our $VERSION = '0.001';
9
10use Convert::Color::LUV;
11use Math::Trig ':pi';
12
13__PACKAGE__->register_color_space('lch');
14
15sub new {
16 my ($class, $l, $c, $h) = @_;
17 ($l, $c, $h) = split /,/s, $l unless defined $c;
18 bless [$l, $c, $h], $class
19}
20
21sub L { shift->[0] }
22sub C { shift->[1] }
23sub h { shift->[2] }
24
25sub lch { @{$_[0]} }
26
27sub convert_to_luv {
28 my ($self) = @_;
29 my ($l, $c, $h) = @$self;
30 my $hrad = $h / 180 * pi;
31 my $u = $c * cos $hrad;
32 my $v = $c * sin $hrad;
33 Convert::Color::LUV->new($l, $u, $v)
34}
35
36sub new_from_luv {
37 my ($class, $luv) = @_;
38 my ($l, $u, $v) = @$luv;
39 my $c = sqrt $u * $u + $v * $v;
00e063a5 40 return $class->new($l, $c, 0) if $c < 0.00000001;
1f72b051
MG
41 my $hrad = atan2 $v, $u;
42 my $h = $hrad * 180 / pi;
43 $h += 360 if $h < 0;
44 $class->new($l, $c, $h)
45}
46
47sub rgb { shift->convert_to_luv->rgb }
48sub new_rgb { shift->new_from_luv(Convert::Color::LUV->new_rgb(@_)) }
49
501;
51__END__
52
53=encoding utf-8
54
55=head1 NAME
56
57Convert::Color::LCh - a color value in the CIE LCh color space
58
59=head1 SYNOPSIS
60
61 use Convert::Color::LCh;
62 my $red = Convert::Color::LCh->new(53.23712, 179.03810, 12.17705);
63 my $green = Convert::Color::LCh->new('87.73552,135.78953,127.71501');
64
65 use Convert::Color;
66 my $blue = Convert::Color->new('lch:32.30087,130.68975,265.87432');
67
68 say $red->L; # 53.23712
69 say $red->C; # 179.03810
70 say $red->h; # 12.17705
71 say join ',', $blue->lch; # 32.30087,130.68975,265.87432
72
73=head1 DESCRIPTION
74
75Objects of this class represent colors in the CIE LCh color space.
76
77Methods:
78
79=over
80
81=item Convert::Color::LCh->B<new>(I<$l>, I<$c>, I<$h>)
82
83Construct a color from its components.
84
85=item Convert::Color::LCh->B<new>(I<"$l,$c,$h">)
86
87Construct a color from a string. The string should contain the three
88components, separated by commas.
89
90=item $lch->B<L>
91
92=item $lch->B<C>
93
94=item $lch->B<h>
95
96Accessors for the three components of the color.
97
98=item $lch->B<lch>
99
100Returns the three components as a list.
101
102=back
103
104=head1 SEE ALSO
105
106L<Convert::Color>
107
108=head1 AUTHOR
109
110Marius Gavrilescu, E<lt>marius@ieval.roE<gt>
111
112=head1 COPYRIGHT AND LICENSE
113
114Copyright (C) 2015 by Marius Gavrilescu
115
116This library is free software; you can redistribute it and/or modify
117it under the same terms as Perl itself, either Perl version 5.20.2 or,
118at your option, any later version of Perl 5 you may have available.
119
120
121=cut
This page took 0.016632 seconds and 4 git commands to generate.