File Coverage

blib/lib/Term/ANSIColor/Concise/ColorObject.pm
Criterion Covered Total %
statement 76 91 83.5
branch 15 20 75.0
condition 9 20 45.0
subroutine 18 20 90.0
pod 0 11 0.0
total 118 162 72.8


line stmt bran cond sub pod time code
1             # -*- indent-tabs-mode: nil -*-
2              
3             =head1 SEE ALSO
4              
5             L
6              
7             L(
8              
9             =cut
10              
11             package Term::ANSIColor::Concise::ColorObject;
12              
13             our $VERSION = "3.02";
14              
15 5     5   3329 use v5.14;
  5         34  
16 5     5   27 use warnings;
  5         9  
  5         308  
17 5     5   52 use utf8;
  5         17  
  5         31  
18              
19 5     5   183 use Data::Dumper;
  5         8  
  5         297  
20 5     5   2073 use parent 'Graphics::ColorObject';
  5         1372  
  5         36  
21              
22             {
23 5     5   321295 no strict 'refs';
  5         11  
  5         204  
24 5     5   20 no warnings 'redefine';
  5         8  
  5         3601  
25             for my $sub (qw(namecolor)) {
26             my $name = "Graphics::ColorObject::$sub";
27             my $save = \&{$name};
28             *{$name} = sub {
29 142   50 142   4866 $_[1] // return;
30 0         0 goto $save;
31             }
32             }
33             }
34              
35             # RGB
36             sub rgb {
37 101     101 0 7377 my $self = shift;
38 101   66     330 my $class = ref $self || $self;
39 101 100       238 if (@_) {
40 31         152 bless $self->SUPER::new_RGB255(\@_), $class;
41             } else {
42 70         117 map int, @{$self->as_RGB255};
  70         208  
43             }
44             }
45              
46             # HSL
47             sub hsl {
48 67     67 0 116 my $self = shift;
49 67   66     255 my $class = ref $self || $self;
50 67 100       166 if (@_) {
51 44         103 my($h, $s, $l) = @_;
52 44         242 bless $self->SUPER::new_HSL([ $h, $s/100, $l/100 ]), $class;
53             } else {
54 23         30 my($h, $s, $l) = @{$self->as_HSL};
  23         81  
55 23         4433 map int, ($h, $s * 100, $l * 100);
56             }
57             }
58              
59             # Lab
60             sub lab {
61 62     62 0 116 my $self = shift;
62 62   66     246 my $class = ref $self || $self;
63 62 100       152 if (@_) {
64 57         251 bless Graphics::ColorObject->new_Lab(\@_), $class;
65             } else {
66 5         8 @{$self->as_Lab};
  5         31  
67             }
68             }
69              
70             # Luv
71             sub luv {
72 0     0 0 0 my $self = shift;
73 0   0     0 my $class = ref $self || $self;
74 0 0       0 if (@_) {
75 0         0 my($L, $u, $v) = @_;
76 0         0 bless Graphics::ColorObject->new_Luv([ $L, $u / 100, $v / 100 ]), $class;
77             } else {
78 0         0 my($L, $u, $v) = @{$self->as_Luv};
  0         0  
79 0         0 map int, ($L, $u * 100, $v * 100);
80             }
81             }
82              
83             # LCHab
84             sub lch {
85 13     13 0 22 my $self = shift;
86 13   66     47 my $class = ref $self || $self;
87 13 100       53 if (@_) {
88 10         52 bless Graphics::ColorObject->new_LCHab(\@_), $class;
89             } else {
90 3         3 @{$self->as_LCHab};
  3         10  
91             }
92             }
93              
94             # YIQ
95             sub yiq {
96 0     0 0 0 my $self = shift;
97 0   0     0 my $class = ref $self || $self;
98 0 0       0 if (@_) {
99 0         0 bless Graphics::ColorObject->new_YIQ(\@_), $class;
100             } else {
101 0         0 @{$self->as_YIQ};
  0         0  
102             }
103             }
104              
105             sub luminance {
106 10     10 0 21 my $self = shift;
107 10 100       28 if (@_) {
108 5         24 $self->set_luminance(@_);
109             } else {
110 5         18 $self->get_luminance;
111             }
112             }
113              
114 5     5   86 use List::Util qw(pairs);
  5         10  
  5         2216  
115              
116             sub set {
117 5     5 0 14 my $map = shift;
118 5         92 $_[$_->[0]] = $_->[1] for pairs @$map;
119 5         34 @_;
120             }
121              
122             sub get_luminance {
123 5     5 0 32 int $_[0] -> as_Lab -> [0];
124             }
125              
126             sub in_rgb_gamut {
127 40     40 0 79 my $self = shift;
128 40         71 my @rgb = @{$self->as_RGB};
  40         147  
129 40 100       5888 !grep { $_ < 0 || $_ > 1 } @rgb;
  120         575  
130             }
131              
132             sub set_luminance {
133 5     5 0 32 my($self, $L) = @_;
134 5         20 my @lab = $self->lab;
135 5         867 my $new = __PACKAGE__->lab(set([ 0 => $L ], @lab));
136 5 50       1361 return $new if $new->in_rgb_gamut;
137              
138             # Reduce chroma to fit in RGB gamut using binary search
139 5         19 my($a, $b) = @lab[1, 2];
140 5         13 my($lo, $hi) = (0, 100);
141 5         19 while ($hi - $lo > 1) {
142 35         77 my $mid = ($lo + $hi) / 2;
143 35         193 my $test = __PACKAGE__->lab($L, $a * $mid / 100, $b * $mid / 100);
144 35 100       8812 if ($test->in_rgb_gamut) {
145 22         120 $lo = $mid;
146             } else {
147 13         67 $hi = $mid;
148             }
149             }
150 5         31 __PACKAGE__->lab($L, $a * $lo / 100, $b * $lo / 100);
151             }
152              
153             1;