| 1 | package Term::Fabulous::Render::Attr; | ||||||||||||||||||||||||||||
| 2 | |||||||||||||||||||||||||||||
| 3 | 5 | ▶ | use v5.32; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 4 | 5 | ▶ | use warnings; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 5 | 5 | ▶ | use feature 'signatures'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 6 | 5 | ▶ | no warnings 'experimental::signatures'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||
| 8 | 5 | our $VERSION = '0.05'; | |||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||
| 10 | 5 | ▶ | use Exporter 'import'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 11 | 5 | our @EXPORT_OK = qw(color_attr clay_color cell_color_attr blended_bg_attr blended_fg_attr STYLE_FLAGS); | |||||||||||||||||||||||||||
| 12 | |||||||||||||||||||||||||||||
| 13 | 5 | ▶ | use Scalar::Util qw(blessed); | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 14 | 5 | ▶ | use Term::Fabulous::Termbox qw( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 15 | TB_DEFAULT TB_HI_BLACK | ||||||||||||||||||||||||||||
| 16 | TB_BOLD TB_DIM TB_ITALIC TB_UNDERLINE TB_BLINK TB_REVERSE TB_INVISIBLE TB_STRIKEOUT TB_UNDERLINE_2 TB_OVERLINE | ||||||||||||||||||||||||||||
| 17 | 5 | ); | |||||||||||||||||||||||||||
| 18 | |||||||||||||||||||||||||||||
| 19 | 5 | ▶ | use Term::Fabulous::Color; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||
| 21 | 5 | ▶ | use constant CACHE_LIMIT => 4096; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 22 | 5 | ▶ | use constant MAX_RGB => 0xFFFFFF; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 23 | |||||||||||||||||||||||||||||
| 24 | # The bits of an attribute that hold its color; the rest are style flags. | ||||||||||||||||||||||||||||
| 25 | 5 | ▶ | use constant COLOR_BITS => MAX_RGB | TB_HI_BLACK; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 26 | |||||||||||||||||||||||||||||
| 27 | # Every style flag: [ flag, SGR parameter, name ], by SGR parameter. The | ||||||||||||||||||||||||||||
| 28 | # one table of what a flag means on a terminal. | ||||||||||||||||||||||||||||
| 29 | 5 | ▶ | use constant STYLE_FLAGS => ( | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 30 | [ TB_BOLD, 1, 'bold' ], | ||||||||||||||||||||||||||||
| 31 | [ TB_DIM, 2, 'dim' ], | ||||||||||||||||||||||||||||
| 32 | [ TB_ITALIC, 3, 'italic' ], | ||||||||||||||||||||||||||||
| 33 | [ TB_UNDERLINE, 4, 'underline' ], | ||||||||||||||||||||||||||||
| 34 | [ TB_BLINK, 5, 'blink' ], | ||||||||||||||||||||||||||||
| 35 | [ TB_REVERSE, 7, 'reverse' ], | ||||||||||||||||||||||||||||
| 36 | [ TB_INVISIBLE, 8, 'invisible' ], | ||||||||||||||||||||||||||||
| 37 | [ TB_STRIKEOUT, 9, 'strikeout' ], | ||||||||||||||||||||||||||||
| 38 | [ TB_UNDERLINE_2, 21, 'double_underline' ], | ||||||||||||||||||||||||||||
| 39 | [ TB_OVERLINE, 53, 'overline' ], | ||||||||||||||||||||||||||||
| 40 | 5 | ); | |||||||||||||||||||||||||||
| 41 | |||||||||||||||||||||||||||||
| 42 | 5 | my %attr_by_rgba; | |||||||||||||||||||||||||||
| 43 | my %color_by_clay_rgba; | ||||||||||||||||||||||||||||
| 44 | 5 | my %blended_attr_by_pair; | |||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||
| 46 | 7 | ▶ | sub color_attr ($color) { | ||||||||||||||||||||||||||
|
Subroutine
color_attr: called CC=3 SCAR=11.3
Pod
documented
| |||||||||||||||||||||||||||||
| 47 | 7 | my $rgba = $color->rgba_int; | |||||||||||||||||||||||||||
| 48 | 7 | my $attr = $attr_by_rgba{$rgba}; | |||||||||||||||||||||||||||
| 49 | 7 | ▶ | return $attr if defined $attr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 50 | |||||||||||||||||||||||||||||
| 51 | 7 | ▶ | %attr_by_rgba = () if keys(%attr_by_rgba) >= CACHE_LIMIT; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 52 | 7 | return $attr_by_rgba{$rgba} = _termbox_attr($color); | |||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||
| 55 | 4 | ▶ | sub clay_color ($clay_rgba) { | ||||||||||||||||||||||||||
|
Subroutine
clay_color: called CC=5 SCAR=16.2
Pod
documented
| |||||||||||||||||||||||||||||
| 56 | 4 | ▶ | die "Term::Fabulous::Render::Attr: a Clay color must be a hash reference with r, g, b, a" | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 57 | unless ref $clay_rgba eq 'HASH'; | ||||||||||||||||||||||||||||
| 58 | |||||||||||||||||||||||||||||
| 59 | 3 | ▶ | my $key = join ',', map { $_ // '' } @{$clay_rgba}{qw(r g b a)}; | ||||||||||||||||||||||||||
|
Condition50%1/2
$_ // ""
MC/DC0%0/1
$_ // ""
$_
Truth table50%1/2
A
A= $_
| |||||||||||||||||||||||||||||
| 60 | 3 | my $color = $color_by_clay_rgba{$key}; | |||||||||||||||||||||||||||
| 61 | 3 | ▶ | return $color if defined $color; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 2 | ▶ | %color_by_clay_rgba = () if keys(%color_by_clay_rgba) >= CACHE_LIMIT; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 2 | return $color_by_clay_rgba{$key} = Term::Fabulous::Color->new( color => $clay_rgba ); | |||||||||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||||||||
| 66 | |||||||||||||||||||||||||||||
| 67 | # undef, and colors with alpha 0, leave a cell without a color of its own. | ||||||||||||||||||||||||||||
| 68 | 0 | ▶ | sub cell_color_attr ( $what, $color ) { | ||||||||||||||||||||||||||
|
Subroutine
cell_color_attr: not called CC=9 SCAR=45.0
Pod
documented
| |||||||||||||||||||||||||||||
| 69 | 0 | ▶ | return undef unless defined $color; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | ▶ | if ( !ref $color && $color =~ /\A[0-9]+\z/ ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
not ref $color and $color =~ /\A[0-9]+\z/u
MC/DC0%0/2
not ref $color and $color =~ /\A[0-9]+\z/u
not ref $color
$color =~ /\A[0-9]+\z/u
Truth table0%0/3
not(A) and B
A= not ref $colorB= $color =~ /\A[0-9]+\z/u
| |||||||||||||||||||||||||||||
| 71 | 0 | ▶ | die "Term::Fabulous::Render::Attr: $what must be a packed 0xRRGGBB value, got $color" if $color > MAX_RGB; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 72 | 0 | ▶ | return $color == 0 ? TB_HI_BLACK : $color + 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||
| 74 | 0 | ▶ | my $object = blessed $color && $color->isa('Term::Fabulous::Color') ? $color : Term::Fabulous::Color->new( color => $color ); | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/3
builtin::blessed($color) and $color->isa("Term::Fabulous::Color")
MC/DC0%0/2
builtin::blessed($color) and $color->isa("Term::Fabulous::Color")
builtin::blessed($color)
$color->isa("Term::Fabulous::Color")
Truth table0%0/3
A and B
A= builtin::blessed($color)B= $color->isa("Term::Fabulous::Color")
| |||||||||||||||||||||||||||||
| 75 | 0 | my $attr = color_attr($object); | |||||||||||||||||||||||||||
| 76 | 0 | ▶ | return $attr == TB_DEFAULT ? undef : $attr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | |||||||||||||||||||||||||||||
| 79 | # termbox2 in truecolor mode reads 0x000000 as "terminal default", so opaque | ||||||||||||||||||||||||||||
| 80 | # black needs its dedicated flag; alpha 0 means "no color" and maps to the | ||||||||||||||||||||||||||||
| 81 | # terminal default on purpose. | ||||||||||||||||||||||||||||
| 82 | 7 | ▶ | sub _termbox_attr ($color) { | ||||||||||||||||||||||||||
|
Subroutine
_termbox_attr: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 83 | 7 | ▶ | return TB_DEFAULT if $color->alpha == 0; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 84 | |||||||||||||||||||||||||||||
| 85 | 5 | my $rgb = $color->rgb_int; | |||||||||||||||||||||||||||
| 86 | 5 | ▶ | return $rgb == 0 ? TB_HI_BLACK : $rgb; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | # A translucent background painted over the background attribute below it. | ||||||||||||||||||||||||||||
| 90 | # The terminal default color has no RGB to blend with, so the color is | ||||||||||||||||||||||||||||
| 91 | # drawn opaque there. Style flags of the attribute below are kept. | ||||||||||||||||||||||||||||
| 92 | 7 | ▶ | sub blended_bg_attr ( $color, $under ) { | ||||||||||||||||||||||||||
|
Subroutine
blended_bg_attr: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 93 | 7 | my $flags = $under & ~COLOR_BITS; | |||||||||||||||||||||||||||
| 94 | 7 | my $rgb = _attr_rgb($under); | |||||||||||||||||||||||||||
| 95 | 7 | ▶ | return $flags | color_attr($color) unless defined $rgb; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 96 | 6 | return $flags | _blend_over_rgb( $color, $rgb ); | |||||||||||||||||||||||||||
| 97 | } | ||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||
| 99 | # A translucent background painted over a glyph: its foreground attribute | ||||||||||||||||||||||||||||
| 100 | # is tinted the same way, except that a terminal-default foreground, which | ||||||||||||||||||||||||||||
| 101 | # cannot be blended, stays as it is. | ||||||||||||||||||||||||||||
| 102 | 2 | ▶ | sub blended_fg_attr ( $color, $under ) { | ||||||||||||||||||||||||||
|
Subroutine
blended_fg_attr: called CC=2 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||||||||
| 103 | 2 | my $rgb = _attr_rgb($under); | |||||||||||||||||||||||||||
| 104 | 2 | ▶ | return $under unless defined $rgb; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 105 | 1 | return ( $under & ~COLOR_BITS ) | _blend_over_rgb( $color, $rgb ); | |||||||||||||||||||||||||||
| 106 | } | ||||||||||||||||||||||||||||
| 107 | |||||||||||||||||||||||||||||
| 108 | # The 0xRRGGBB value of an attribute; undef for the terminal default. | ||||||||||||||||||||||||||||
| 109 | 9 | ▶ | sub _attr_rgb ($attr) { | ||||||||||||||||||||||||||
|
Subroutine
_attr_rgb: called CC=3 SCAR=11.0
| |||||||||||||||||||||||||||||
| 110 | 9 | ▶ | return 0 if $attr & TB_HI_BLACK; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 111 | 8 | my $rgb = $attr & MAX_RGB; | |||||||||||||||||||||||||||
| 112 | 8 | ▶ | return $rgb == 0 ? undef : $rgb; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||
| 115 | # The color attribute of $color composited over the opaque color $rgb. | ||||||||||||||||||||||||||||
| 116 | 7 | ▶ | sub _blend_over_rgb ( $color, $rgb ) { | ||||||||||||||||||||||||||
|
Subroutine
_blend_over_rgb: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||
| 117 | 7 | my $key = $color->rgba_int . ",$rgb"; | |||||||||||||||||||||||||||
| 118 | 7 | my $attr = $blended_attr_by_pair{$key}; | |||||||||||||||||||||||||||
| 119 | 7 | ▶ | return $attr if defined $attr; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 120 | |||||||||||||||||||||||||||||
| 121 | 6 | ▶ | %blended_attr_by_pair = () if keys(%blended_attr_by_pair) >= CACHE_LIMIT; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 122 | 6 | my @over = $color->to_rgba; | |||||||||||||||||||||||||||
| 123 | 6 | my $alpha = pop @over; | |||||||||||||||||||||||||||
| 124 | 6 | my @under = ( ( $rgb >> 16 ) & 0xFF, ( $rgb >> 8 ) & 0xFF, $rgb & 0xFF ); | |||||||||||||||||||||||||||
| 125 | 6 | my $mixed = 0; | |||||||||||||||||||||||||||
| 126 | 6 | foreach my $channel ( 0 .. 2 ) { | |||||||||||||||||||||||||||
| 127 | 18 | $mixed = ( $mixed << 8 ) | int( ( $over[$channel] * $alpha + $under[$channel] * ( 255 - $alpha ) ) / 255 + 0.5 ); | |||||||||||||||||||||||||||
| 128 | } | ||||||||||||||||||||||||||||
| 129 | 6 | ▶ | return $blended_attr_by_pair{$key} = $mixed == 0 ? TB_HI_BLACK : $mixed; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 130 | } | ||||||||||||||||||||||||||||
| 131 | |||||||||||||||||||||||||||||
| 132 | 5 | 1; | |||||||||||||||||||||||||||
| 133 | |||||||||||||||||||||||||||||
| 134 | __END__ | ||||||||||||||||||||||||||||