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