blib/lib/Term/Fabulous/Color.pm

filter: Statementstmt 88.1% 252 / 286 Branchbran 84.7% 78 / 92 Conditioncond 59.6% 31 / 52 MC/DCmcdc 51.7% 15 / 29 Subroutinesub 83.6% 41 / 49 Podpod 100% 27 / 27 totaltotal 82.9% 444 / 535 CC 67 SCAR 16.8
CC
67
Cov
84%
hex
20.0
fg_sgr
12.0
bg_sgr
12.0
CRAP 5.4
« blib/lib/Term/Fabulous/Enum/BorderStyle.pm ↑ Summary blib/lib/Term/Fabulous/Widget/Table/Mutator.pm »
1 package Term::Fabulous::Color;
2  
3 9 ▶use v5.32;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 9 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
6 9 ▶no warnings 'experimental::signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 9 our $VERSION = '0.05';
9  
10 9 ▶use Object::Pad 0.825;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 class Term::Fabulous::Color :strict(params) {
13 9 ▶ use List::Util qw( min max );
Subroutine BEGIN: called CC=1 SCAR=0.0
14 9 ▶ use POSIX qw( floor );
Subroutine BEGIN: called CC=1 SCAR=0.0
15 9 ▶ use Scalar::Util qw( blessed looks_like_number );
Subroutine BEGIN: called CC=1 SCAR=0.0
16  
17 9 my @CHANNEL_NAMES = qw( red green blue alpha );
18  
19 # Accepted hash shapes, keyed by their sorted key list.
20 9 my %CHANNEL_KEYS_BY_HASH_SHAPE = (
21 'b g r' => [qw( r g b )],
22 'a b g r' => [qw( r g b a )],
23 'blue green red' => [qw( red green blue )],
24 'alpha blue green red' => [qw( red green blue alpha )],
25 );
26  
27 9 my $HEX_SPEC = qr/\A#?([0-9a-fA-F]{6}(?:[0-9a-fA-F]{2})?)\z/;
28 9 my $NUMBER = qr/[+-]?(?:[0-9]+(?:\.[0-9]*)?|\.[0-9]+)/;
29 9 my $COMMA = qr/\s*,\s*/;
30  
31 1 ▶ field $red :reader;
Subroutine red: called CC=1 SCAR=0.0
Pod documented
32 1 ▶ field $green :reader;
Subroutine green: called CC=1 SCAR=0.0
Pod documented
33 1 ▶ field $blue :reader;
Subroutine blue: called CC=1 SCAR=0.0
Pod documented
34 15 ▶ field $alpha :reader;
Subroutine alpha: called CC=1 SCAR=0.0
Pod documented
35  
36 # INVARIANT: Color is immutable post-ADJUST; lazy memo slots are safe.
37 9 field $_rgb_int_cache;
38 9 field $_rgba_int_cache;
39 9 field $_fg_sgr_cache;
40 9 field $_bg_sgr_cache;
41  
42 ADJUST :params ( :$color ) {
43 my @channels = _channels_from_input($color);
44 ( $red, $green, $blue, $alpha ) = map { _checked_channel( $CHANNEL_NAMES[$_], $channels[$_] ) } 0 .. 3;
45 }
46  
47 9 ▶ sub _describe ($value) {
Subroutine _describe: called CC=3 SCAR=11.0
48 13 ▶ return 'undef' unless defined $value;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value 2 11 13
49 11 ▶ return ref($value) . ' reference' if ref $value;
Branch: 1 branch
BranchTrueFalseTotal
if ref $value 1 10 11
50 10 return "'$value'";
51 }
52  
53 1485 ▶ sub _is_finite_number ($value) {
Subroutine _is_finite_number: called CC=5 SCAR=16.7
54 1485 ▶ return 0 unless defined $value && !ref $value && looks_like_number($value);
Branch: 1 branch
BranchTrueFalseTotal
unless defined $value and not ref $value and &looks_like_number($value) 1 1484 1485
Condition50%3/6
defined $value and not ref $value and &looks_like_number($value)
!ll&&!rl&&r
011484
defined $value and not ref $value
!ll&&!rl&&r
001485
MC/DC33%1/3
defined $value and not ref $value and &looks_like_number($value)
defined $value not ref $value &looks_like_number($value)
Truth table50%2/4
A and not(B) and C
A= defined $valueB= not ref $valueC= &looks_like_number($value)
ABCresult
0XX0
10X0
1100
1111
55 1484 ▶ return 0 if $value != $value; # NaN
Branch: 1 branch
BranchTrueFalseTotal
if $value != $value 1 1483 1484
56 1483 return abs($value) != 9**9**9; # +/- Inf
57 }
58  
59 1439 ▶ sub _round ($value) {
Subroutine _round: called CC=1 SCAR=0.0
60 1439 return floor( $value + 0.5 );
61 }
62  
63 # Rounds to the nearest integer; the rounded value must lie in 0..255.
64 1432 ▶ sub _checked_channel ( $name, $value ) {
Subroutine _checked_channel: called CC=5 SCAR=16.1
65 1432 ▶ my $rounded = _is_finite_number($value) ? _round($value) : undef;
Branch: 1 branch
BranchTrueFalseTotal
_is_finite_number($value) ? : 1430 2 1432
66 1432 ▶ die "Term::Fabulous::Color: $name must be a number in 0..255, got " . _describe($value)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $rounded and $rounded >= 0 and $rounded <= 255 6 1426 1432
Condition100%6/6
defined $rounded and $rounded >= 0 and $rounded <= 255
!ll&&!rl&&r
331426
defined $rounded and $rounded >= 0
!ll&&!rl&&r
211429
MC/DC100%3/3
defined $rounded and $rounded >= 0 and $rounded <= 255
defined $rounded $rounded >= 0 $rounded <= 255
Truth table100%4/4
A and B and C
A= defined $roundedB= $rounded >= 0C= $rounded <= 255
ABCresult
0XX0
10X0
1100
1111
67 unless defined $rounded && $rounded >= 0 && $rounded <= 255;
68 1426 return $rounded;
69 }
70  
71 39 ▶ sub _checked_range ( $name, $value, $low, $high ) {
Subroutine _checked_range: called CC=4 SCAR=14.9
72 39 ▶ die "Term::Fabulous::Color: $name must be a number in $low..$high, got " . _describe($value)
Branch: 1 branch
BranchTrueFalseTotal
unless _is_finite_number($value) and $value >= $low and $value <= $high 4 35 39
Condition50%3/6
_is_finite_number($value) and $value >= $low and $value <= $high
!ll&&!rl&&r
0435
_is_finite_number($value) and $value >= $low
!ll&&!rl&&r
0039
MC/DC33%1/3
_is_finite_number($value) and $value >= $low and $value <= $high
_is_finite_number($value) $value >= $low $value <= $high
Truth table50%2/4
A and B and C
A= _is_finite_number($value)B= $value >= $lowC= $value <= $high
ABCresult
0XX0
10X0
1100
1111
73 unless _is_finite_number($value) && $value >= $low && $value <= $high;
74 35 return $value;
75 }
76  
77 370 ▶ sub _channels_from_input ($color) {
Subroutine _channels_from_input: called CC=7 SCAR=19.5
78 370 ▶ return $color->to_rgba if blessed $color && $color->isa('Term::Fabulous::Color');
Branch: 1 branch
BranchTrueFalseTotal
if builtin::blessed($color) and $color->isa("Term::Fabulous::Color") 1 369 370
Condition66%2/3
builtin::blessed($color) and $color->isa("Term::Fabulous::Color")
!ll&&!rl&&r
36901
MC/DC50%1/2
builtin::blessed($color) and $color->isa("Term::Fabulous::Color")
builtin::blessed($color) $color->isa("Term::Fabulous::Color")
Truth table66%2/3
A and B
A= builtin::blessed($color)B= $color->isa("Term::Fabulous::Color")
ABresult
0X0
100
111
79 369 ▶ return _channels_from_hash($color) if ref $color eq 'HASH';
Branch: 1 branch
BranchTrueFalseTotal
if ref $color eq "HASH" 6 363 369
80 363 ▶ return _channels_from_array($color) if ref $color eq 'ARRAY';
Branch: 1 branch
BranchTrueFalseTotal
if ref $color eq "ARRAY" 38 325 363
81 325 ▶ return _channels_from_string($color) if defined $color && !ref $color;
Branch: 1 branch
BranchTrueFalseTotal
if defined $color and not ref $color 324 1 325
Condition66%2/3
defined $color and not ref $color
!ll&&!rl&&r
10324
MC/DC50%1/2
defined $color and not ref $color
defined $color not ref $color
Truth table66%2/3
A and not(B)
A= defined $colorB= not ref $color
ABresult
0X0
100
111
82 1 die "Term::Fabulous::Color: invalid color input " . _describe($color);
83 }
84  
85 6 ▶ sub _channels_from_hash ($hash) {
Subroutine _channels_from_hash: called CC=3 SCAR=11.0
86 6 my $shape = join ' ', sort keys %$hash;
87 6 ▶ my $keys = $CHANNEL_KEYS_BY_HASH_SHAPE{$shape} // die "Term::Fabulous::Color: a color hash needs exactly the keys r, g, b[, a] or red, green, blue[, alpha], got {$shape}";
Condition100%2/2
$CHANNEL_KEYS_BY_HASH_SHAPE{$shape} // die("Term::Fabulous::Color: a color hash needs exactly the keys r, g, b[, a] or red, green, blue[, alpha], got {$shape}")
l!l
42
MC/DC100%1/1
$CHANNEL_KEYS_BY_HASH_SHAPE{$shape} // die("Term::Fabulous::Color: a color hash needs exactly the keys r, g, b[, a] or red, green, blue[, alpha], got {$shape}")
$CHANNEL_KEYS_BY_HASH_SHAPE{$shape}
Truth table100%2/2
A
A= $CHANNEL_KEYS_BY_HASH_SHAPE{$shape}
Aresult
11
00
88 4 my @channels = @{$hash}{@$keys};
89 4 ▶ return @channels == 3 ? ( @channels, 255 ) : @channels;
Branch: 1 branch
BranchTrueFalseTotal
@channels == 3 ? : 1 3 4
90 }
91  
92 38 ▶ sub _channels_from_array ($array) {
Subroutine _channels_from_array: called CC=4 SCAR=13.9
93 38 my $count = @$array;
94 38 ▶ die "Term::Fabulous::Color: a color array needs 3 or 4 elements, got $count"
Branch: 1 branch
BranchTrueFalseTotal
unless $count == 3 or $count == 4 1 37 38
Condition100%3/3
$count == 3 or $count == 4
l!l&&r!l&&!r
12251
MC/DC100%2/2
$count == 3 or $count == 4
$count == 3 $count == 4
Truth table100%3/3
A or B
A= $count == 3B= $count == 4
ABresult
1X1
011
000
95 unless $count == 3 || $count == 4;
96 37 ▶ return $count == 3 ? ( @$array, 255 ) : @$array;
Branch: 1 branch
BranchTrueFalseTotal
$count == 3 ? : 12 25 37
97 }
98  
99 324 ▶ sub _channels_from_string ($spec) {
Subroutine _channels_from_string: called CC=10 SCAR=23.0
100 324 ▶ if ( $spec =~ /\A[0-9]+\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($spec =~ /\A[0-9]+\z/u) 2 322 324
101 2 ▶ die "Term::Fabulous::Color: a packed integer color must be in 0..0xFFFFFF, got $spec" if $spec > 0xFFFFFF;
Branch: 1 branch
BranchTrueFalseTotal
if $spec > 16777215 1 1 2
102 1 return ( ( $spec >> 16 ) & 0xFF, ( $spec >> 8 ) & 0xFF, $spec & 0xFF, 255 );
103 }
104 322 ▶ if ( my ($hex) = $spec =~ $HEX_SPEC ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($hex) = $spec =~ /$HEX_SPEC/u) 299 23 322
105 299 my @channels = map { CORE::hex($_) } unpack '(A2)*', $hex;
106 299 ▶ return @channels == 3 ? ( @channels, 255 ) : @channels;
Branch: 1 branch
BranchTrueFalseTotal
@channels == 3 ? : 298 1 299
107 }
108 23 ▶ if ( my @rgb = $spec =~ /\Argb\(\s*($NUMBER)$COMMA($NUMBER)$COMMA($NUMBER)\s*\)\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@rgb) = $spec =~ /\Argb\(\s*($NUMBER)$COMMA($NUMBER)$COMMA($NUMBER)\s*\)\z/u) 1 22 23
109 1 return ( @rgb, 255 );
110 }
111 22 ▶ if ( my @rgba = $spec =~ /\Argba\(\s*($NUMBER)$COMMA($NUMBER)$COMMA($NUMBER)$COMMA($NUMBER%?)\s*\)\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@rgba) = $spec =~ /\Argba\(\s*($NUMBER)$COMMA($NUMBER)$COMMA($NUMBER)$COMMA($NUMBER%?)\s*\)\z/u) 8 14 22
112 8 return ( @rgba[ 0 .. 2 ], _alpha_from_token( $rgba[3] ) );
113 }
114 14 ▶ if ( my @hsl = $spec =~ /\Ahsl\(\s*($NUMBER)$COMMA($NUMBER)%$COMMA($NUMBER)%\s*\)\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@hsl) = $spec =~ /\Ahsl\(\s*($NUMBER)$COMMA($NUMBER)%$COMMA($NUMBER)%\s*\)\z/u) 3 11 14
115 3 return ( _hsl_to_rgb_float( _normalized_hsl(@hsl) ), 255 );
116 }
117 11 ▶ if ( my @hsla = $spec =~ /\Ahsla\(\s*($NUMBER)$COMMA($NUMBER)%$COMMA($NUMBER)%$COMMA($NUMBER%?)\s*\)\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my(@hsla) = $spec =~ /\Ahsla\(\s*($NUMBER)$COMMA($NUMBER)%$COMMA($NUMBER)%$COMMA($NUMBER%?)\s*\)\z/u) 1 10 11
118 1 return ( _hsl_to_rgb_float( _normalized_hsl( @hsla[ 0 .. 2 ] ) ), _alpha_from_token( $hsla[3] ) );
119 }
120 10 ▶ if ( my $channels = _web_color_channels($spec) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $channels = _web_color_channels($spec)) 8 2 10
121 8 return @$channels;
122 }
123 2 die "Term::Fabulous::Color: unrecognized color string '$spec'";
124 }
125  
126 # The CSS color names of Term::Fabulous::Enum::WebColor, looked up
127 # without regard to case. Its items are Colors themselves, so the
128 # enumeration cannot be loaded before this class; the table is built
129 # when the first name is looked up.
130 9 my %RGBA_BY_WEB_NAME;
131  
132 10 ▶ sub _web_color_channels ($name) {
Subroutine _web_color_channels: called CC=2 SCAR=6.9
133 10 ▶ if ( !%RGBA_BY_WEB_NAME ) {
Branch: 1 branch
BranchTrueFalseTotal
unless (%RGBA_BY_WEB_NAME) 1 9 10
134 1 require Term::Fabulous::Enum::WebColor;
135 1 %RGBA_BY_WEB_NAME = map { lc( $_->name ) => [ $_->to_rgba ] } Term::Fabulous::Enum::WebColor->values;
136 }
137 10 return $RGBA_BY_WEB_NAME{ lc $name };
138 }
139  
140 # Alpha grammar of rgba()/hsla(), string and factory alike: "N%" is a
141 # percentage, a number with a decimal point is a fraction of 1, a bare
142 # integer is the 0..255 channel.
143 28 ▶ sub _alpha_from_token ($token) {
Subroutine _alpha_from_token: called CC=5 SCAR=16.1
144 28 ▶ die "Term::Fabulous::Color: alpha must be a number, got " . _describe($token) unless defined $token && !ref $token;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $token and not ref $token 1 27 28
Condition66%2/3
defined $token and not ref $token
!ll&&!rl&&r
1027
MC/DC50%1/2
defined $token and not ref $token
defined $token not ref $token
Truth table66%2/3
A and not(B)
A= defined $tokenB= not ref $token
ABresult
0X0
100
111
145 27 ▶ if ( my ($percent) = $token =~ /\A(.+)%\z/ ) {
Branch: 1 branch
BranchTrueFalseTotal
if (my($percent) = $token =~ /\A(.+)%\z/u) 4 23 27
146 4 return _checked_range( 'alpha percentage', $percent, 0, 100 ) / 100 * 255;
147 }
148 23 ▶ if ( $token =~ /\./ ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($token =~ /\./u) 7 16 23
149 7 return _checked_range( 'alpha fraction', $token, 0, 1 ) * 255;
150 }
151 16 return $token;
152 }
153  
154 10 ▶ sub _normalized_hsl ( $h, $s, $l ) {
Subroutine _normalized_hsl: called CC=2 SCAR=7.0
155 10 ▶ die "Term::Fabulous::Color: hue must be a number, got " . _describe($h)
Branch: 1 branch
BranchTrueFalseTotal
unless _is_finite_number($h) 0 10 10
156 unless _is_finite_number($h);
157 10 _checked_range( 'saturation', $s, 0, 100 );
158 10 _checked_range( 'lightness', $l, 0, 100 );
159 10 return ( $h - 360 * floor( $h / 360 ), $s, $l );
160 }
161  
162 # Unrounded HSL -> RGB. Hue in [0, 360), saturation and lightness in
163 # 0..100; returns three channels in 0..255 as floats.
164 13 ▶ sub _hsl_to_rgb_float ( $h, $s, $l ) {
Subroutine _hsl_to_rgb_float: called CC=9 SCAR=22.0
165 13 $s /= 100;
166 13 $l /= 100;
167 13 ▶ return ( $l * 255 ) x 3 if $s == 0;
Branch: 1 branch
BranchTrueFalseTotal
if $s == 0 1 12 13
168  
169 12 ▶ my $q = $l < 0.5 ? $l * ( 1 + $s ) : $l + $s - $l * $s;
Branch: 1 branch
BranchTrueFalseTotal
$l < 0.5 ? : 3 9 12
170 12 my $p = 2 * $l - $q;
171  
172 12 my @channels;
173 12 foreach my $offset ( 1 / 3, 0, -1 / 3 ) {
174 36 my $t = $h / 360 + $offset;
175 36 ▶ $t += 1 if $t < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $t < 0 3 33 36
176 36 ▶ $t -= 1 if $t >= 1;
Branch: 1 branch
BranchTrueFalseTotal
if $t >= 1 0 36 36
177  
178 36 ▶ my $value
Branch: 3 branches
BranchTrueFalseTotal
$t < 0.16666666666666665741 ? : 12 24 36
$t < 0.5 ? : 12 12 24
$t < 0.66666666666666662966 ? : 0 12 12
179 = $t < 1 / 6 ? $p + ( $q - $p ) * 6 * $t
180 : $t < 1 / 2 ? $q
181 : $t < 2 / 3 ? $p + ( $q - $p ) * 6 * ( 2 / 3 - $t )
182 : $p;
183 36 push @channels, $value * 255;
184 }
185 12 return @channels;
186 }
187  
188 # Unrounded RGB -> HSL. Channels in 0..255; returns hue in [0, 360) and
189 # saturation / lightness in 0..100 as floats.
190 5 ▶ sub _rgb_to_hsl_float ( $red, $green, $blue ) {
Subroutine _rgb_to_hsl_float: called CC=6 SCAR=18.1
191 5 ( $red, $green, $blue ) = map { $_ / 255 } ( $red, $green, $blue );
192  
193 5 my $max = max( $red, $green, $blue );
194 5 my $min = min( $red, $green, $blue );
195 5 my $lightness = ( $max + $min ) / 2;
196 5 ▶ return ( 0, 0, $lightness * 100 ) if $max == $min;
Branch: 1 branch
BranchTrueFalseTotal
if $max == $min 0 5 5
197  
198 5 my $delta = $max - $min;
199 5 ▶ my $saturation = $lightness < 0.5 ? $delta / ( $max + $min ) : $delta / ( 2 - $max - $min );
Branch: 1 branch
BranchTrueFalseTotal
$lightness < 0.5 ? : 4 1 5
200 5 ▶ my $hue
Branch: 2 branches
BranchTrueFalseTotal
$max == $red ? : 1 4 5
$max == $green ? : 4 0 4
201 = $max == $red ? 60 * ( ( $green - $blue ) / $delta )
202 : $max == $green ? 60 * ( 2 + ( $blue - $red ) / $delta )
203 : 60 * ( 4 + ( $red - $green ) / $delta );
204 5 ▶ $hue += 360 if $hue < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $hue < 0 0 5 5
205  
206 5 return ( $hue, $saturation * 100, $lightness * 100 );
207 }
208  
209 3 ▶ method blend ( $other, $ratio ) {
Subroutine blend: called CC=3 SCAR=11.0
Pod documented
210 3 ▶ die "Term::Fabulous::Color: blend needs a Term::Fabulous::Color to blend with, got " . _describe($other)
Branch: 1 branch
BranchTrueFalseTotal
unless builtin::blessed($other) and $other->isa("Term::Fabulous::Color") 1 2 3
Condition66%2/3
builtin::blessed($other) and $other->isa("Term::Fabulous::Color")
!ll&&!rl&&r
102
MC/DC50%1/2
builtin::blessed($other) and $other->isa("Term::Fabulous::Color")
builtin::blessed($other) $other->isa("Term::Fabulous::Color")
Truth table66%2/3
A and B
A= builtin::blessed($other)B= $other->isa("Term::Fabulous::Color")
ABresult
0X0
100
111
211 unless blessed $other && $other->isa('Term::Fabulous::Color');
212 2 _checked_range( 'blend ratio', $ratio, 0, 1 );
213 1 my $inv_ratio = 1 - $ratio;
214  
215 1 return Term::Fabulous::Color->new(
216 color => [
217 $red * $inv_ratio + $other->red * $ratio,
218 $green * $inv_ratio + $other->green * $ratio,
219 $blue * $inv_ratio + $other->blue * $ratio,
220 $alpha * $inv_ratio + $other->alpha * $ratio,
221 ],
222 );
223 9 }
224  
225 1 ▶ method with_alpha ($new_alpha) {
Subroutine with_alpha: called CC=1 SCAR=0.0
Pod documented
226 1 return Term::Fabulous::Color->new( color => [ $red, $green, $blue, $new_alpha ], );
227 9 }
228  
229 4 ▶ method is_translucent () {
Subroutine is_translucent: called CC=2 SCAR=6.9
Pod documented
230 4 ▶ return $alpha > 0 && $alpha < 255;
Condition100%3/3
$alpha > 0 && $alpha < 255
!ll&&!rl&&r
112
MC/DC100%2/2
$alpha > 0 && $alpha < 255
$alpha > 0 $alpha < 255
Truth table100%3/3
A && B
A= $alpha > 0B= $alpha < 255
ABresult
0X0
100
111
231 9 }
232  
233 3 ▶ method lighten ($amount) {
Subroutine lighten: called CC=2 SCAR=7.0
Pod documented
234 3 ▶ die "Term::Fabulous::Color: lighten amount must be a number, got " . _describe($amount)
Branch: 1 branch
BranchTrueFalseTotal
unless _is_finite_number($amount) 0 3 3
235 unless _is_finite_number($amount);
236  
237 3 my ( $h, $s, $l ) = _rgb_to_hsl_float( $red, $green, $blue );
238 3 my $lightness = min( 100, max( 0, $l + $amount * 100 ) );
239 3 return Term::Fabulous::Color->new( color => [ _hsl_to_rgb_float( $h, $s, $lightness ), $alpha ] );
240 9 }
241  
242 1 ▶ method darken ($amount) {
Subroutine darken: called CC=2 SCAR=7.0
Pod documented
243 1 ▶ die "Term::Fabulous::Color: darken amount must be a number, got " . _describe($amount)
Branch: 1 branch
BranchTrueFalseTotal
unless _is_finite_number($amount) 0 1 1
244 unless _is_finite_number($amount);
245  
246 1 return $self->lighten( -$amount );
247 9 }
248  
249 0 ▶ method ansi () {
Subroutine ansi: not called CC=1 SCAR=6.9
Pod documented
250 0 return sprintf( "\e[38;2;%d;%d;%dm", $red, $green, $blue );
251 9 }
252  
253 0 ▶ method ansi_bg () {
Subroutine ansi_bg: not called CC=1 SCAR=6.9
Pod documented
254 0 return sprintf( "\e[48;2;%d;%d;%dm", $red, $green, $blue );
255 9 }
256  
257 0 ▶ method hexString () {
Subroutine hexString: not called CC=1 SCAR=6.9
Pod documented
258 0 return sprintf( "#%02x%02x%02x%02x", $red, $green, $blue, $alpha );
259 9 }
260  
261 191 ▶ method to_rgba () {
Subroutine to_rgba: called CC=1 SCAR=0.0
Pod documented
262 191 return ( $red, $green, $blue, $alpha );
263 9 }
264  
265 1 ▶ method to_hsl () {
Subroutine to_hsl: called CC=1 SCAR=0.0
Pod documented
266 1 return Term::Fabulous::Color->rgb_to_hsl( $red, $green, $blue );
267 9 }
268  
269 14 ▶ method rgba_int () {
Subroutine rgba_int: called CC=2 SCAR=6.9
Pod documented
270 14 ▶ return $_rgba_int_cache //= ( $red << 24 ) | ( $green << 16 ) | ( $blue << 8 ) | $alpha;
Condition100%2/2
$_rgba_int_cache //= $red << 24 | $green << 16 | $blue << 8 | $alpha
l!l
410
MC/DC100%1/1
$_rgba_int_cache //= $red << 24 | $green << 16 | $blue << 8 | $alpha
$_rgba_int_cache
Truth table100%2/2
A
A= $_rgba_int_cache
Aresult
11
00
271 9 }
272  
273 # Alpha-0 emits the terminal default-fg reset (\e[39m) instead of literal black.
274 0 ▶ method fg_sgr () {
Subroutine fg_sgr: not called CC=3 SCAR=24.8
Pod documented
275 0 ▶ return $_fg_sgr_cache
Branch: 1 branch
BranchTrueFalseTotal
$alpha == 0 ? : 0 0 0
Condition0%0/2
$_fg_sgr_cache //= $alpha == 0 ? "\e[39m" : sprintf("\e[38;2;%d;%d;%dm", $red, $green, $blue)
l!l
00
MC/DC0%0/1
$_fg_sgr_cache //= $alpha == 0 ? "\e[39m" : sprintf("\e[38;2;%d;%d;%dm", $red, $green, $blue)
$_fg_sgr_cache
Truth table0%0/2
A
A= $_fg_sgr_cache
Aresult
11
00
276 //= $alpha == 0
277 ? "\e[39m"
278 : sprintf( "\e[38;2;%d;%d;%dm", $red, $green, $blue );
279 9 }
280  
281 # Alpha-0 emits the terminal default-bg reset (\e[49m); see fg_sgr.
282 0 ▶ method bg_sgr () {
Subroutine bg_sgr: not called CC=3 SCAR=24.8
Pod documented
283 0 ▶ return $_bg_sgr_cache
Branch: 1 branch
BranchTrueFalseTotal
$alpha == 0 ? : 0 0 0
Condition0%0/2
$_bg_sgr_cache //= $alpha == 0 ? "\e[49m" : sprintf("\e[48;2;%d;%d;%dm", $red, $green, $blue)
l!l
00
MC/DC0%0/1
$_bg_sgr_cache //= $alpha == 0 ? "\e[49m" : sprintf("\e[48;2;%d;%d;%dm", $red, $green, $blue)
$_bg_sgr_cache
Truth table0%0/2
A
A= $_bg_sgr_cache
Aresult
11
00
284 //= $alpha == 0
285 ? "\e[49m"
286 : sprintf( "\e[48;2;%d;%d;%dm", $red, $green, $blue );
287 9 }
288  
289 7 ▶ method rgb_int () {
Subroutine rgb_int: called CC=2 SCAR=7.0
Pod documented
290 7 ▶ return $_rgb_int_cache //= ( $red << 16 ) | ( $green << 8 ) | $blue;
Condition50%1/2
$_rgb_int_cache //= $red << 16 | $green << 8 | $blue
l!l
07
MC/DC0%0/1
$_rgb_int_cache //= $red << 16 | $green << 8 | $blue
$_rgb_int_cache
Truth table50%1/2
A
A= $_rgb_int_cache
Aresult
11
00
291 9 }
292  
293 0 ▶ method rgba_float () {
Subroutine rgba_float: not called CC=1 SCAR=6.9
Pod documented
294 0 return ( $red / 255, $green / 255, $blue / 255, $alpha / 255 );
295 9 }
296  
297 0 ▶ method rgb_float () {
Subroutine rgb_float: not called CC=1 SCAR=6.9
Pod documented
298 0 return ( $red / 255, $green / 255, $blue / 255 );
299 9 }
300  
301 1 ▶ method hsl_to_rgb :common ( $h, $s, $l ) {
Subroutine hsl_to_rgb: called CC=1 SCAR=0.0
Pod documented
302 1 return map { _round($_) } _hsl_to_rgb_float( _normalized_hsl( $h, $s, $l ) );
303 9 }
304  
305 2 ▶ method rgb_to_hsl :common ( $r, $g, $b ) {
Subroutine rgb_to_hsl: called CC=1 SCAR=0.0
Pod documented
306 2 _checked_range( 'red', $r, 0, 255 );
307 2 _checked_range( 'green', $g, 0, 255 );
308 2 _checked_range( 'blue', $b, 0, 255 );
309  
310 2 my ( $h, $s, $l ) = map { _round($_) } _rgb_to_hsl_float( $r, $g, $b );
311 2 return ( $h % 360, $s, $l );
312 9 }
313  
314 6 ▶ method rgb :common ( $r, $g, $b ) {
Subroutine rgb: called CC=1 SCAR=0.0
Pod documented
315 6 return Term::Fabulous::Color->new( color => [ $r, $g, $b ] );
316 9 }
317  
318 15 ▶ method rgba :common ( $r, $g, $b, $a ) {
Subroutine rgba: called CC=1 SCAR=0.0
Pod documented
319 15 return Term::Fabulous::Color->new( color => [ $r, $g, $b, _alpha_from_token($a) ] );
320 9 }
321  
322 0 ▶ method hex :common ( $spec ) {
Subroutine hex: not called CC=4 SCAR=30.0
Pod documented
323 0 ▶ die "Term::Fabulous::Color: hex expects '#rrggbb' or '#rrggbbaa', got " . _describe($spec)
Branch: 1 branch
BranchTrueFalseTotal
unless defined $spec and not ref $spec and $spec =~ /$HEX_SPEC/u 0 0 0
Condition0%0/6
defined $spec and not ref $spec and $spec =~ /$HEX_SPEC/u
!ll&&!rl&&r
000
defined $spec and not ref $spec
!ll&&!rl&&r
000
MC/DC0%0/3
defined $spec and not ref $spec and $spec =~ /$HEX_SPEC/u
defined $spec not ref $spec $spec =~ /$HEX_SPEC/u
Truth table0%0/4
A and not(B) and C
A= defined $specB= not ref $specC= $spec =~ /$HEX_SPEC/u
ABCresult
0XX0
10X0
1100
1111
324 unless defined $spec && !ref $spec && $spec =~ $HEX_SPEC;
325 0 return Term::Fabulous::Color->new( color => $spec );
326 9 }
327  
328 1 ▶ method hsl :common ( $h, $s, $l ) {
Subroutine hsl: called CC=1 SCAR=0.0
Pod documented
329 1 return Term::Fabulous::Color->new( color => [ _hsl_to_rgb_float( _normalized_hsl( $h, $s, $l ) ), 255 ] );
330 9 }
331  
332 4 ▶ method hsla :common ( $h, $s, $l, $a ) {
Subroutine hsla: called CC=1 SCAR=0.0
Pod documented
333 4 return Term::Fabulous::Color->new( color => [ _hsl_to_rgb_float( _normalized_hsl( $h, $s, $l ) ), _alpha_from_token($a) ] );
334 9 }
335  
336 }
337  
338 9 1;
339  
340 __END__
« blib/lib/Term/Fabulous/Enum/BorderStyle.pm ↑ Summary blib/lib/Term/Fabulous/Widget/Table/Mutator.pm »