HUSL.pm

filter: Statementstmt 100% 79 / 79 Branchbran 100% 14 / 14 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 22 / 22 Podpod 60.0% 6 / 10 totaltotal 96.8% 121 / 125 CC 10 SCAR 6.4
CC
10
Cov
100%
_get_bounds
4.0
convert_to_lch
3.0
new_from_lch
3.0
CRAP 1.9
↑ Summary LCh.pm »
1 package Convert::Color::HUSL;
2  
3 2 ▶use 5.008009;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use parent qw/Convert::Color/;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use Convert::Color::XYZ;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶use Convert::Color::LUV;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 2 ▶use Convert::Color::LCh;
Subroutine BEGIN: called CC=1 SCAR=0.0
11 2 ▶use List::Util qw/min/;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 2 ▶use Math::Trig qw/:pi/;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 2 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
15 2 *MAT_R = *Convert::Color::XYZ::MAT_R;
16 2 *MAT_G = *Convert::Color::XYZ::MAT_G;
17 2 *MAT_B = *Convert::Color::XYZ::MAT_B;
18  
19 2 *KAPPA = *Convert::Color::LUV::KAPPA;
20 2 *EPS = *Convert::Color::LUV::EPS;
21 }
22  
23 2 our $VERSION = '1.000';
24  
25 2 __PACKAGE__->register_color_space('husl');
26  
27 2049 ▶sub new {
Subroutine new: called CC=2 SCAR=6.9
Pod documented
28 2049 my ($class, $h, $s, $l) = @_;
29 2049 ▶ ($h, $s, $l) = split /,/s, $h unless defined $s;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $s 1 2048 2049
30 2049 bless [$h, $s, $l], $class
31 }
32  
33 1 ▶sub H { shift->[0] }
Subroutine H: called CC=1 SCAR=0.0
Pod documented
34 1 ▶sub S { shift->[1] }
Subroutine S: called CC=1 SCAR=0.0
Pod documented
35 1 ▶sub L { shift->[2] }
Subroutine L: called CC=1 SCAR=0.0
Pod documented
36  
37 1 ▶sub hsl { @{$_[0]} }
Subroutine hsl: called CC=1 SCAR=0.0
Pod documented
38  
39 3060 ▶sub _get_bounds {
Subroutine _get_bounds: called CC=4 SCAR=13.9
40 3060 my ($l) = @_;
41 3060 my $sub1 = ($l + 16) ** 3 / 1_560_896;
42 3060 ▶ my $sub2 = $sub1 > EPS ? $sub1 : $l / KAPPA;
Branch: 1 branch
BranchTrueFalseTotal
$sub1 > 0.0088564516790356311027 ? : 3030 30 3060
43 3060 my @ret;
44  
45 3060 for (MAT_R, MAT_G, MAT_B) {
46 9180 my ($m1, $m2, $m3) = @$_;
47 9180 for (0, 1) {
48 18360 my $top1 = (284_517 * $m1 - 94_839 * $m3) * $sub2;
49 18360 my $top2 = (838_422 * $m3 + 769_860 * $m2 + 731_718 * $m1) * $l * $sub2 - 769_860 * $_ * $l;
50 18360 my $bottom = (632_260 * $m3 - 126_452 * $m2) * $sub2 + 126_452 * $_;
51 18360 push @ret, [$top1 / $bottom, $top2 / $bottom]
52 }
53 }
54  
55 @ret
56 3060 }
57  
58 9180 ▶sub _length_of_ray_until_intersect {
Subroutine _length_of_ray_until_intersect: called CC=2 SCAR=6.9
59 9180 my ($theta, $line) = @_;
60 9180 my ($m, $n) = @$line;
61 9180 my $len = $n / (sin ($theta) - $m * cos $theta);
62 9180 ▶ return if $len < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $len < 0 4137 5043 9180
63 5043 $len
64 }
65  
66 1530 ▶sub max_chroma_for_lh {
Subroutine max_chroma_for_lh: called CC=1 SCAR=0.0
Pod undocumented
67 1530 my ($self, $l, $h) = @_;
68 1530 my $hrad = $h / 180 * pi;
69 min map {
70 1530 _length_of_ray_until_intersect $hrad, $_
71 } _get_bounds $l;
72 }
73  
74 1024 ▶sub convert_to_lch {
Subroutine convert_to_lch: called CC=3 SCAR=11.0
Pod undocumented
75 1024 my ($self) = @_;
76 1024 my ($h, $s, $l) = @$self;
77 1024 ▶ return Convert::Color::LCh->new(100, 0, $h) if $l > 99.9999999;
Branch: 1 branch
BranchTrueFalseTotal
if $l > "99.9999999" 2 1022 1024
78 1022 ▶ return Convert::Color::LCh->new(0, 0, $h) if $l < 0.00000001;
Branch: 1 branch
BranchTrueFalseTotal
if $l < "1e-08" 2 1020 1022
79 1020 my $max = $self->max_chroma_for_lh($l, $h);
80 1020 my $c = $max / 100 * $s;
81 1020 Convert::Color::LCh->new($l, $c, $h)
82 }
83  
84 2048 ▶sub new_from_lch {
Subroutine new_from_lch: called CC=3 SCAR=11.0
Pod undocumented
85 2048 my ($class, $lch) = @_;
86 2048 my ($l, $c, $h) = @$lch;
87 2048 ▶ return $class->new($h, 0, 100) if $l > 99.9999999;
Branch: 1 branch
BranchTrueFalseTotal
if $l > "99.9999999" 4 2044 2048
88 2044 ▶ return $class->new($h, 0, 0) if $l < 0.00000001;
Branch: 1 branch
BranchTrueFalseTotal
if $l < "1e-08" 4 2040 2044
89 2040 my $max = $class->max_chroma_for_lh($l, $h);
90 2040 my $s = $c / $max * 100;
91 2040 $class->new($h, $s, $l)
92 }
93  
94 1024 ▶sub rgb { shift->convert_to_lch->rgb }
Subroutine rgb: called CC=1 SCAR=0.0
Pod documented
95 2048 ▶sub new_rgb { shift->new_from_lch(Convert::Color::LCh->new_rgb(@_)) }
Subroutine new_rgb: called CC=1 SCAR=0.0
Pod undocumented
96  
97 2 1;
98 __END__
↑ Summary LCh.pm »