| 1 | package TUI::Drivers::Color; | ||||||||||||||||||||||
| 2 | # ABSTRACT: Represents a color in any of the supported color types | ||||||||||||||||||||||
| 3 | |||||||||||||||||||||||
| 4 | 106 | ▶ | use 5.010; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 5 | 106 | ▶ | use strict; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 6 | 106 | ▶ | use warnings; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 7 | |||||||||||||||||||||||
| 8 | 106 | our $VERSION = '2.000002'; | |||||||||||||||||||||
| 9 | 106 | $VERSION =~ tr/_//d; | |||||||||||||||||||||
| 10 | 106 | our $AUTHORITY = 'cpan:BRICKPOOL'; | |||||||||||||||||||||
| 11 | |||||||||||||||||||||||
| 12 | 106 | ▶ | use Exporter 'import'; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 13 | 106 | our @EXPORT = qw( | |||||||||||||||||||||
| 14 | TColor | ||||||||||||||||||||||
| 15 | ); | ||||||||||||||||||||||
| 16 | |||||||||||||||||||||||
| 17 | 106 | ▶ | use PerlX::Assert::PP; | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 18 | 106 | ▶ | use Scalar::Util qw( | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 19 | blessed | ||||||||||||||||||||||
| 20 | looks_like_number | ||||||||||||||||||||||
| 21 | 106 | ); | |||||||||||||||||||||
| 22 | |||||||||||||||||||||||
| 23 | 106 | ▶ | use TUI::Drivers::Const qw( :ctXXXX ); | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 24 | 106 | ▶ | use TUI::Drivers::Colors qw( | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 25 | RGBtoBIOS | ||||||||||||||||||||||
| 26 | XTerm256toXTerm16 | ||||||||||||||||||||||
| 27 | XTerm16toBIOS | ||||||||||||||||||||||
| 28 | 106 | ); | |||||||||||||||||||||
| 29 | |||||||||||||||||||||||
| 30 | sub TColor() { __PACKAGE__ } | ||||||||||||||||||||||
| 31 | |||||||||||||||||||||||
| 32 | # predeclare private methods | ||||||||||||||||||||||
| 33 | my ( | ||||||||||||||||||||||
| 34 | 106 | $type, | |||||||||||||||||||||
| 35 | ); | ||||||||||||||||||||||
| 36 | |||||||||||||||||||||||
| 37 | # Default color constructor an constructor for specific color types. | ||||||||||||||||||||||
| 38 | # Bit 0..23: Color (24 bits) | ||||||||||||||||||||||
| 39 | # Bit 24..31: Type (8 bits, of which only 2 are actually used) | ||||||||||||||||||||||
| 40 | |||||||||||||||||||||||
| 41 | 627 | ▶ | sub new { # $cell (|%args) | ||||||||||||||||||||
|
Subroutine
new: called CC=10 SCAR=23.7
Pod
documented
| |||||||||||||||||||||||
| 42 | 627 | my ( $class, @args ) = @_; | |||||||||||||||||||||
| 43 | 627 | ▶ | assert ( $class and !ref $class ); | ||||||||||||||||||||
|
Condition33%1/3
$class && !ref($class)
MC/DC0%0/2
$class && !ref($class)
$class
!ref($class)
Truth table33%1/3
A && not(B)
A= $classB= !ref($class)
| |||||||||||||||||||||||
| 44 | |||||||||||||||||||||||
| 45 | # TColor->new() | ||||||||||||||||||||||
| 46 | 627 | my $bits; | |||||||||||||||||||||
| 47 | 627 | ▶ | if (!@args) { | ||||||||||||||||||||
|
Branch: 4 branches
| |||||||||||||||||||||||
| 48 | 5 | $bits = 0; | |||||||||||||||||||||
| 49 | } | ||||||||||||||||||||||
| 50 | |||||||||||||||||||||||
| 51 | # TColor->new( bios => Int ) | ||||||||||||||||||||||
| 52 | ▶ | elsif ( @args == 2 && $args[0] eq 'bios' ) { | |||||||||||||||||||||
|
Condition66%2/3
@args == 2 and $args[0] eq "bios"
MC/DC50%1/2
@args == 2 and $args[0] eq "bios"
@args == 2
$args[0] eq "bios"
Truth table66%2/3
A and B
A= @args == 2B= $args[0] eq "bios"
| |||||||||||||||||||||||
| 53 | 594 | my $bios = $args[1]; | |||||||||||||||||||||
| 54 | 594 | assert ( looks_like_number $bios ); | |||||||||||||||||||||
| 55 | 594 | $bits = ( $bios & 0xf ) | |||||||||||||||||||||
| 56 | | ( ctBIOS << 24 ); | ||||||||||||||||||||||
| 57 | } | ||||||||||||||||||||||
| 58 | |||||||||||||||||||||||
| 59 | # TColor->new( rgb => Int | ArrayRef ) | ||||||||||||||||||||||
| 60 | ▶ | elsif ( @args == 2 && $args[0] eq 'rgb' ) { | |||||||||||||||||||||
|
Condition66%2/3
@args == 2 and $args[0] eq "rgb"
MC/DC50%1/2
@args == 2 and $args[0] eq "rgb"
@args == 2
$args[0] eq "rgb"
Truth table66%2/3
A and B
A= @args == 2B= $args[0] eq "rgb"
| |||||||||||||||||||||||
| 61 | 19 | my $rgb = $args[1]; | |||||||||||||||||||||
| 62 | 19 | ▶ | if ( ref $rgb eq 'ARRAY' ) { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 63 | 2 | assert ( @$rgb == 3 ); | |||||||||||||||||||||
| 64 | 2 | $rgb = ( ( ( $rgb->[0] << 8 ) | $rgb->[1] ) << 8 ) | $rgb->[2]; | |||||||||||||||||||||
| 65 | } | ||||||||||||||||||||||
| 66 | 19 | assert ( looks_like_number $rgb ); | |||||||||||||||||||||
| 67 | 19 | $bits = ( $rgb & 0xffffff ) | |||||||||||||||||||||
| 68 | | ( ctRGB << 24 ); | ||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||
| 70 | |||||||||||||||||||||||
| 71 | # TColor->new( xterm => Int ) | ||||||||||||||||||||||
| 72 | ▶ | elsif ( @args == 2 && $args[0] eq 'xterm' ) { | |||||||||||||||||||||
|
Condition33%1/3
@args == 2 and $args[0] eq "xterm"
MC/DC0%0/2
@args == 2 and $args[0] eq "xterm"
@args == 2
$args[0] eq "xterm"
Truth table33%1/3
A and B
A= @args == 2B= $args[0] eq "xterm"
| |||||||||||||||||||||||
| 73 | 9 | my $xterm = $args[1]; | |||||||||||||||||||||
| 74 | 9 | assert ( looks_like_number $xterm ); | |||||||||||||||||||||
| 75 | 9 | $bits = ( $xterm & 0xff ) | |||||||||||||||||||||
| 76 | | ( ctXTerm << 24 ); | ||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||
| 78 | |||||||||||||||||||||||
| 79 | else { | ||||||||||||||||||||||
| 80 | 0 | return; | |||||||||||||||||||||
| 81 | } | ||||||||||||||||||||||
| 82 | |||||||||||||||||||||||
| 83 | 627 | return bless \$bits, $class; | |||||||||||||||||||||
| 84 | } | ||||||||||||||||||||||
| 85 | |||||||||||||||||||||||
| 86 | # Copy and clone methods | ||||||||||||||||||||||
| 87 | |||||||||||||||||||||||
| 88 | 0 | ▶ | sub assign { # void ($other) | ||||||||||||||||||||
|
Subroutine
assign: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 89 | 0 | my ( $self, $other ) = @_; | |||||||||||||||||||||
| 90 | 0 | assert ( blessed $self ); | |||||||||||||||||||||
| 91 | 0 | assert ( blessed $other ); | |||||||||||||||||||||
| 92 | 0 | $$self = $$other; | |||||||||||||||||||||
| 93 | 0 | return; | |||||||||||||||||||||
| 94 | } | ||||||||||||||||||||||
| 95 | |||||||||||||||||||||||
| 96 | 0 | ▶ | sub clone { # $attr () | ||||||||||||||||||||
|
Subroutine
clone: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||||||||||||||
| 97 | 0 | my ( $self ) = @_; | |||||||||||||||||||||
| 98 | 0 | assert ( blessed $self ); | |||||||||||||||||||||
| 99 | 0 | my $v = $$self; | |||||||||||||||||||||
| 100 | 0 | return bless \$v, ref $self; | |||||||||||||||||||||
| 101 | } | ||||||||||||||||||||||
| 102 | |||||||||||||||||||||||
| 103 | # Color type getters. | ||||||||||||||||||||||
| 104 | |||||||||||||||||||||||
| 105 | 6 | ▶ | sub isDefault { # $bool () | ||||||||||||||||||||
|
Subroutine
isDefault: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 106 | 6 | assert ( blessed $_[0] ); | |||||||||||||||||||||
| 107 | 6 | return $_[0]->$type == ctDefault; | |||||||||||||||||||||
| 108 | } | ||||||||||||||||||||||
| 109 | |||||||||||||||||||||||
| 110 | 1106 | ▶ | sub isBIOS { # $bool () | ||||||||||||||||||||
|
Subroutine
isBIOS: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 111 | 1106 | assert ( blessed $_[0] ); | |||||||||||||||||||||
| 112 | 1106 | return $_[0]->$type == ctBIOS; | |||||||||||||||||||||
| 113 | } | ||||||||||||||||||||||
| 114 | |||||||||||||||||||||||
| 115 | 1 | ▶ | sub isRGB { # $bool () | ||||||||||||||||||||
|
Subroutine
isRGB: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 116 | 1 | assert ( blessed $_[0] ); | |||||||||||||||||||||
| 117 | 1 | return $_[0]->$type == ctRGB; | |||||||||||||||||||||
| 118 | } | ||||||||||||||||||||||
| 119 | |||||||||||||||||||||||
| 120 | 3 | ▶ | sub isXTerm { # $bool () | ||||||||||||||||||||
|
Subroutine
isXTerm: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 121 | 3 | assert ( blessed $_[0] ); | |||||||||||||||||||||
| 122 | 3 | return $_[0]->$type == ctXTerm; | |||||||||||||||||||||
| 123 | } | ||||||||||||||||||||||
| 124 | |||||||||||||||||||||||
| 125 | # Color value getters. They perform no conversion: make sure to check | ||||||||||||||||||||||
| 126 | # the color type first. | ||||||||||||||||||||||
| 127 | |||||||||||||||||||||||
| 128 | 14 | ▶ | sub asBIOS { # $bios () | ||||||||||||||||||||
|
Subroutine
asBIOS: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 129 | 14 | assert ( blessed $_[0] ); | |||||||||||||||||||||
| 130 | 14 | return ${ $_[0] } & 0x0f; | |||||||||||||||||||||
| 131 | } | ||||||||||||||||||||||
| 132 | |||||||||||||||||||||||
| 133 | 4 | ▶ | sub asRGB { # $rgb () | ||||||||||||||||||||
|
Subroutine
asRGB: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 134 | 4 | assert ( blessed $_[0] ); | |||||||||||||||||||||
| 135 | 4 | return ${ $_[0] } & 0x00ffffff; | |||||||||||||||||||||
| 136 | } | ||||||||||||||||||||||
| 137 | |||||||||||||||||||||||
| 138 | 3 | ▶ | sub asXTerm { # $xterm () | ||||||||||||||||||||
|
Subroutine
asXTerm: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||||||||||
| 139 | 3 | assert ( blessed $_[0] ); | |||||||||||||||||||||
| 140 | 3 | return ${ $_[0] } & 0xff; | |||||||||||||||||||||
| 141 | } | ||||||||||||||||||||||
| 142 | |||||||||||||||||||||||
| 143 | # Quantization to TColor BIOS. | ||||||||||||||||||||||
| 144 | |||||||||||||||||||||||
| 145 | 7 | ▶ | sub toBIOS { # $bios ($isForeground) | ||||||||||||||||||||
|
Subroutine
toBIOS: called CC=7 SCAR=21.0
Pod
documented
| |||||||||||||||||||||||
| 146 | 7 | my ( $self, $isForeground ) = @_; | |||||||||||||||||||||
| 147 | 7 | assert ( blessed $self ); | |||||||||||||||||||||
| 148 | |||||||||||||||||||||||
| 149 | 7 | SWITCH: for ( $self->$type ) { | |||||||||||||||||||||
| 150 | 7 | ▶ | ctBIOS == $_ and | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 151 | return $self->asBIOS(); | ||||||||||||||||||||||
| 152 | 5 | ▶ | ctRGB == $_ and | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 153 | return RGBtoBIOS( $self->asRGB() ); | ||||||||||||||||||||||
| 154 | 1 | ▶ | ctXTerm == $_ and do { | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 155 | 1 | my $idx = $self->asXTerm(); | |||||||||||||||||||||
| 156 | 1 | ▶ | $idx = XTerm256toXTerm16( $idx ) | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 157 | if $idx >= 16; | ||||||||||||||||||||||
| 158 | 1 | return XTerm16toBIOS( $idx ); | |||||||||||||||||||||
| 159 | }; | ||||||||||||||||||||||
| 160 | DEFAULT: { | ||||||||||||||||||||||
| 161 | 0 | ▶ | return $isForeground ? 0x7 : 0x0; | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 162 | } | ||||||||||||||||||||||
| 163 | } | ||||||||||||||||||||||
| 164 | } | ||||||||||||||||||||||
| 165 | |||||||||||||||||||||||
| 166 | 12 | ▶ | sub equals { # $bool ($other|$bios) | ||||||||||||||||||||
|
Subroutine
equals: called CC=4 SCAR=14.3
Pod
documented
| |||||||||||||||||||||||
| 167 | 12 | my ( $self, $other ) = @_; | |||||||||||||||||||||
| 168 | 12 | assert ( blessed $self ); | |||||||||||||||||||||
| 169 | 12 | ▶ | assert ( blessed $other or looks_like_number $other ); | ||||||||||||||||||||
|
Condition66%2/3
builtin::blessed($other) || &looks_like_number($other)
MC/DC0%0/2
builtin::blessed($other) || &looks_like_number($other)
builtin::blessed($other)
&looks_like_number($other)
Truth table66%2/3
A || B
A= builtin::blessed($other)B= &looks_like_number($other)
| |||||||||||||||||||||||
| 170 | 12 | ▶ | return $self->asBIOS() == $other | ||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||
| 171 | unless ref $other; | ||||||||||||||||||||||
| 172 | 11 | ▶ | return ref $self eq ref $other | ||||||||||||||||||||
|
Condition33%1/3
ref $self eq ref $other && $$self == $$other
MC/DC0%0/2
ref $self eq ref $other && $$self == $$other
ref $self eq ref $other
$$self == $$other
Truth table33%1/3
A && B
A= ref $self eq ref $otherB= $$self == $$other
| |||||||||||||||||||||||
| 173 | && $$self == $$other; | ||||||||||||||||||||||
| 174 | } | ||||||||||||||||||||||
| 175 | |||||||||||||||||||||||
| 176 | use overload | ||||||||||||||||||||||
| 177 | 106 | ▶ | '0+' => \&asBIOS, | ||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 178 | '==' => \&equals, | ||||||||||||||||||||||
| 179 | 106 | fallback => 1; | |||||||||||||||||||||
| 180 | |||||||||||||||||||||||
| 181 | # Private methods. | ||||||||||||||||||||||
| 182 | |||||||||||||||||||||||
| 183 | 1123 | ▶ | $type = sub { # $type () | ||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||
| 184 | 1123 | assert ( blessed $_[0] ); | |||||||||||||||||||||
| 185 | 1123 | return ${ $_[0] } >> 24; | |||||||||||||||||||||
| 186 | 106 | }; | |||||||||||||||||||||
| 187 | |||||||||||||||||||||||
| 188 | 106 | 1; | |||||||||||||||||||||
| 189 | |||||||||||||||||||||||
| 190 | __END__ | ||||||||||||||||||||||