« 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
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 11 | ▶ | return ref($value) . ' reference' if ref $value; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%3/6
defined $value and not ref $value and &looks_like_number($value)
defined $value and not ref $value
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)
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 1484 | ▶ | return 0 if $value != $value; # NaN | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 66 | 1432 | ▶ | die "Term::Fabulous::Color: $name must be a number in 0..255, got " . _describe($value) | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
defined $rounded and $rounded >= 0 and $rounded <= 255
defined $rounded and $rounded >= 0
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition50%3/6
_is_finite_number($value) and $value >= $low and $value <= $high
_is_finite_number($value) and $value >= $low
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
builtin::blessed($color) and $color->isa("Term::Fabulous::Color")
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 79 | 369 | ▶ | return _channels_from_hash($color) if ref $color eq 'HASH'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 80 | 363 | ▶ | return _channels_from_array($color) if ref $color eq 'ARRAY'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 81 | 325 | ▶ | return _channels_from_string($color) if defined $color && !ref $color; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
defined $color and not ref $color
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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}")
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}
| |||||||||||||||||||||||||||||||||||||||||||
| 88 | 4 | my @channels = @{$hash}{@$keys}; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 4 | ▶ | return @channels == 3 ? ( @channels, 255 ) : @channels; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition100%3/3
$count == 3 or $count == 4
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
| |||||||||||||||||||||||||||||||||||||||||||
| 95 | unless $count == 3 || $count == 4; | ||||||||||||||||||||||||||||||||||||||||||
| 96 | 37 | ▶ | return $count == 3 ? ( @$array, 255 ) : @$array; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 101 | 2 | ▶ | die "Term::Fabulous::Color: a packed integer color must be in 0..0xFFFFFF, got $spec" if $spec > 0xFFFFFF; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 102 | 1 | return ( ( $spec >> 16 ) & 0xFF, ( $spec >> 8 ) & 0xFF, $spec & 0xFF, 255 ); | |||||||||||||||||||||||||||||||||||||||||
| 103 | } | ||||||||||||||||||||||||||||||||||||||||||
| 104 | 322 | ▶ | if ( my ($hex) = $spec =~ $HEX_SPEC ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 105 | 299 | my @channels = map { CORE::hex($_) } unpack '(A2)*', $hex; | |||||||||||||||||||||||||||||||||||||||||
| 106 | 299 | ▶ | return @channels == 3 ? ( @channels, 255 ) : @channels; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 107 | } | ||||||||||||||||||||||||||||||||||||||||||
| 108 | 23 | ▶ | if ( my @rgb = $spec =~ /\Argb\(\s*($NUMBER)$COMMA($NUMBER)$COMMA($NUMBER)\s*\)\z/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
defined $token and not ref $token
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
| |||||||||||||||||||||||||||||||||||||||||||
| 145 | 27 | ▶ | if ( my ($percent) = $token =~ /\A(.+)%\z/ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 146 | 4 | return _checked_range( 'alpha percentage', $percent, 0, 100 ) / 100 * 255; | |||||||||||||||||||||||||||||||||||||||||
| 147 | } | ||||||||||||||||||||||||||||||||||||||||||
| 148 | 23 | ▶ | if ( $token =~ /\./ ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 168 | |||||||||||||||||||||||||||||||||||||||||||
| 169 | 12 | ▶ | my $q = $l < 0.5 ? $l * ( 1 + $s ) : $l + $s - $l * $s; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 176 | 36 | ▶ | $t -= 1 if $t >= 1; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 177 | |||||||||||||||||||||||||||||||||||||||||||
| 178 | 36 | ▶ | my $value | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 3 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 197 | |||||||||||||||||||||||||||||||||||||||||||
| 198 | 5 | my $delta = $max - $min; | |||||||||||||||||||||||||||||||||||||||||
| 199 | 5 | ▶ | my $saturation = $lightness < 0.5 ? $delta / ( $max + $min ) : $delta / ( 2 - $max - $min ); | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 200 | 5 | ▶ | my $hue | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition66%2/3
builtin::blessed($other) and $other->isa("Term::Fabulous::Color")
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")
| |||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC100%2/2
$alpha > 0 && $alpha < 255
$alpha > 0
$alpha < 255
Truth table100%3/3
A && B
A= $alpha > 0B= $alpha < 255
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/2
$_fg_sgr_cache //= $alpha == 0 ? "\e[39m" : sprintf("\e[38;2;%d;%d;%dm", $red, $green, $blue)
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/2
$_bg_sgr_cache //= $alpha == 0 ? "\e[49m" : sprintf("\e[48;2;%d;%d;%dm", $red, $green, $blue)
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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
MC/DC0%0/1
$_rgb_int_cache //= $red << 16 | $green << 8 | $blue
$_rgb_int_cache
Truth table50%1/2
A
A= $_rgb_int_cache
| |||||||||||||||||||||||||||||||||||||||||||
| 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
Condition0%0/6
defined $spec and not ref $spec and $spec =~ /$HEX_SPEC/u
defined $spec and not ref $spec
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
| |||||||||||||||||||||||||||||||||||||||||||
| 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__ | ||||||||||||||||||||||||||||||||||||||||||