| 1 | package TUI::Drivers::Colors; | ||||||||||||||
| 2 | # ABSTRACT: defines color conversion functions | ||||||||||||||
| 3 | |||||||||||||||
| 4 | 107 | ▶ | use 5.010; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 5 | 107 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 6 | 107 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 7 | |||||||||||||||
| 8 | 107 | our $VERSION = '2.000002'; | |||||||||||||
| 9 | 107 | $VERSION =~ tr/_//d; | |||||||||||||
| 10 | 107 | our $AUTHORITY = 'cpan:BRICKPOOL'; | |||||||||||||
| 11 | |||||||||||||||
| 12 | 107 | ▶ | use Exporter 'import'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 13 | |||||||||||||||
| 14 | 107 | our @EXPORT_OK = qw( | |||||||||||||
| 15 | BIOStoXTerm16 | ||||||||||||||
| 16 | RGBtoBIOS | ||||||||||||||
| 17 | RGBtoXTerm16 | ||||||||||||||
| 18 | RGBtoXTerm256 | ||||||||||||||
| 19 | XTerm16toBIOS | ||||||||||||||
| 20 | XTerm256toRGB | ||||||||||||||
| 21 | XTerm256toXTerm16 | ||||||||||||||
| 22 | ); | ||||||||||||||
| 23 | |||||||||||||||
| 24 | 107 | our %EXPORT_TAGS = ( | |||||||||||||
| 25 | all => \@EXPORT_OK, | ||||||||||||||
| 26 | ); | ||||||||||||||
| 27 | |||||||||||||||
| 28 | 107 | ▶ | use PerlX::Assert::PP; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 29 | 107 | ▶ | use Scalar::Util qw( | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 30 | looks_like_number | ||||||||||||||
| 31 | 107 | ); | |||||||||||||
| 32 | |||||||||||||||
| 33 | 107 | ▶ | use constant HUE_PRECISION => 32; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 34 | 107 | ▶ | use constant HUE_MAX => 6 * HUE_PRECISION; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 35 | |||||||||||||||
| 36 | 25680 | ▶ | sub _pack { # $rgb ($r, $g, $b) | ||||||||||||
|
Subroutine
_pack: called CC=1 SCAR=0.0
| |||||||||||||||
| 37 | 25680 | return ( ( ( $_[0] << 8 ) | $_[1] ) << 8 ) | $_[2]; | |||||||||||||
| 38 | } | ||||||||||||||
| 39 | |||||||||||||||
| 40 | 25927 | ▶ | sub _rgb8 { # @rgb ($rgb) | ||||||||||||
|
Subroutine
_rgb8: called CC=1 SCAR=0.0
| |||||||||||||||
| 41 | return ( | ||||||||||||||
| 42 | 25927 | ( $_[0] >> 16 ) & 0xff, | |||||||||||||
| 43 | ( $_[0] >> 8 ) & 0xff, | ||||||||||||||
| 44 | $_[0] & 0xff, | ||||||||||||||
| 45 | ); | ||||||||||||||
| 46 | } | ||||||||||||||
| 47 | |||||||||||||||
| 48 | 25686 | ▶ | sub _RGBtoHCL { # @hcl ($r, $g, $b) | ||||||||||||
|
Subroutine
_RGBtoHCL: called CC=10 SCAR=23.0
| |||||||||||||||
| 49 | 25686 | my ( $r, $g, $b ) = @_; | |||||||||||||
| 50 | |||||||||||||||
| 51 | 25686 | ▶ | my $xmin = $r < $g ? $r : $g; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 52 | 25686 | ▶ | $xmin = $xmin < $b ? $xmin : $b; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 53 | |||||||||||||||
| 54 | 25686 | ▶ | my $xmax = $r > $g ? $r : $g; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 55 | 25686 | ▶ | $xmax = $xmax > $b ? $xmax : $b; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 56 | |||||||||||||||
| 57 | 25686 | my $v = $xmax; | |||||||||||||
| 58 | 25686 | my $l = int( ( $xmax + $xmin ) / 2 ); | |||||||||||||
| 59 | 25686 | my $c = $xmax - $xmin; | |||||||||||||
| 60 | 25686 | my $h = 0; | |||||||||||||
| 61 | |||||||||||||||
| 62 | 25686 | ▶ | if ( $c ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 63 | 22474 | ▶ | if ( $v == $r ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 64 | 9095 | $h = int( HUE_PRECISION * ( $g - $b ) / $c ); | |||||||||||||
| 65 | } | ||||||||||||||
| 66 | elsif ( $v == $g ) { | ||||||||||||||
| 67 | 7490 | $h = int( HUE_PRECISION * ( $b - $r ) / $c ) | |||||||||||||
| 68 | + 2 * HUE_PRECISION; | ||||||||||||||
| 69 | } | ||||||||||||||
| 70 | else { | ||||||||||||||
| 71 | 5889 | $h = int( HUE_PRECISION * ( $r - $g ) / $c ) | |||||||||||||
| 72 | + 4 * HUE_PRECISION; | ||||||||||||||
| 73 | } | ||||||||||||||
| 74 | |||||||||||||||
| 75 | 22474 | ▶ | if ( $h < 0 ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 76 | 3745 | $h += HUE_MAX; | |||||||||||||
| 77 | } | ||||||||||||||
| 78 | elsif ( $h >= HUE_MAX ) { | ||||||||||||||
| 79 | 0 | $h -= HUE_MAX; | |||||||||||||
| 80 | } | ||||||||||||||
| 81 | } | ||||||||||||||
| 82 | |||||||||||||||
| 83 | 25686 | return ( $h, $c, $l ); | |||||||||||||
| 84 | } | ||||||||||||||
| 85 | |||||||||||||||
| 86 | 8 | ▶ | sub BIOStoXTerm16 { # $xterm16 ($bios) | ||||||||||||
|
Subroutine
BIOStoXTerm16: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 87 | 8 | my ( $c ) = @_; | |||||||||||||
| 88 | 8 | assert ( looks_like_number $c ); | |||||||||||||
| 89 | |||||||||||||||
| 90 | # BIOS uses RGBI bit order, XTerm16 uses ANSI order. | ||||||||||||||
| 91 | # Swap red and blue, keep green and intensity. | ||||||||||||||
| 92 | 8 | return ( $c & 0x0a ) | |||||||||||||
| 93 | | ( ( $c & 0x01 ) << 2 ) | ||||||||||||||
| 94 | | ( ( $c & 0x04 ) >> 2 ); | ||||||||||||||
| 95 | } | ||||||||||||||
| 96 | |||||||||||||||
| 97 | 7 | ▶ | sub XTerm16toBIOS { # $bios ($xterm16) | ||||||||||||
|
Subroutine
XTerm16toBIOS: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 98 | 7 | goto &BIOStoXTerm16; | |||||||||||||
| 99 | } | ||||||||||||||
| 100 | |||||||||||||||
| 101 | 25686 | ▶ | sub RGBtoXTerm16 { # $xterm16 ($rgb) | ||||||||||||
|
Subroutine
RGBtoXTerm16: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||||||
| 102 | 25686 | my ( $rgb ) = @_; | |||||||||||||
| 103 | 25686 | assert ( looks_like_number $rgb ); | |||||||||||||
| 104 | |||||||||||||||
| 105 | 25686 | my ( $r, $g, $b ) = _rgb8( $rgb ); | |||||||||||||
| 106 | 25686 | my ( $h, $c, $l ) = _RGBtoHCL( $r, $g, $b ); | |||||||||||||
| 107 | |||||||||||||||
| 108 | 25686 | ▶ | if ( $c >= 12 ) { # Color if Chroma >= 12. | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 109 | 22474 | my @normal = ( 0x1, 0x3, 0x2, 0x6, 0x4, 0x5 ); | |||||||||||||
| 110 | 22474 | my @bright = ( 0x9, 0xb, 0xa, 0xe, 0xc, 0xd ); | |||||||||||||
| 111 | |||||||||||||||
| 112 | 22474 | ▶ | my $index = int( | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 113 | ( | ||||||||||||||
| 114 | $h < HUE_MAX - HUE_PRECISION / 2 | ||||||||||||||
| 115 | ? $h + HUE_PRECISION / 2 | ||||||||||||||
| 116 | : $h - ( HUE_MAX - HUE_PRECISION / 2 ) | ||||||||||||||
| 117 | ) / HUE_PRECISION | ||||||||||||||
| 118 | ); | ||||||||||||||
| 119 | |||||||||||||||
| 120 | 22474 | ▶ | return $normal[$index] if $l < int( 0.5 * 255 ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 121 | 15408 | ▶ | return $bright[$index] if $l < int( 0.925 * 255 ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 122 | 642 | return 15; | |||||||||||||
| 123 | } | ||||||||||||||
| 124 | |||||||||||||||
| 125 | 3212 | ▶ | return 0 if $l < int( 0.25 * 255 ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 126 | 2463 | ▶ | return 8 if $l < int( 0.625 * 255 ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 127 | 1179 | ▶ | return 7 if $l < int( 0.875 * 255 ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 128 | 322 | return 15; | |||||||||||||
| 129 | } | ||||||||||||||
| 130 | |||||||||||||||
| 131 | 5 | ▶ | sub RGBtoBIOS { # $bios ($rgb) | ||||||||||||
|
Subroutine
RGBtoBIOS: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 132 | 5 | my ( $rgb ) = @_; | |||||||||||||
| 133 | 5 | assert ( looks_like_number $rgb ); | |||||||||||||
| 134 | |||||||||||||||
| 135 | 5 | return XTerm16toBIOS( | |||||||||||||
| 136 | RGBtoXTerm16( $rgb ) | ||||||||||||||
| 137 | ); | ||||||||||||||
| 138 | } | ||||||||||||||
| 139 | |||||||||||||||
| 140 | 107 | my @XTERM256_TO_RGB; | |||||||||||||
| 141 | my @XTERM256_TO_XTERM16; | ||||||||||||||
| 142 | |||||||||||||||
| 143 | 107 | ▶ | BEGIN { | ||||||||||||
|
Subroutine
BEGIN: called CC=9 SCAR=22.0
| |||||||||||||||
| 144 | 107 | @XTERM256_TO_RGB = (0) x 256; | |||||||||||||
| 145 | 107 | @XTERM256_TO_XTERM16 = (0) x 256; | |||||||||||||
| 146 | |||||||||||||||
| 147 | 107 | for my $i ( 0 .. 15 ) { | |||||||||||||
| 148 | 1712 | $XTERM256_TO_XTERM16[$i] = $i; | |||||||||||||
| 149 | } | ||||||||||||||
| 150 | |||||||||||||||
| 151 | 107 | for my $r_idx ( 0 .. 5 ) { | |||||||||||||
| 152 | 642 | ▶ | my $r = $r_idx ? 55 + $r_idx * 40 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 153 | |||||||||||||||
| 154 | 642 | for my $g_idx ( 0 .. 5 ) { | |||||||||||||
| 155 | 3852 | ▶ | my $g = $g_idx ? 55 + $g_idx * 40 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 156 | |||||||||||||||
| 157 | 3852 | for my $b_idx ( 0 .. 5 ) { | |||||||||||||
| 158 | 23112 | ▶ | my $b = $b_idx ? 55 + $b_idx * 40 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 159 | |||||||||||||||
| 160 | 23112 | my $idx = 16 + ( $r_idx * 6 + $g_idx ) * 6 + $b_idx; | |||||||||||||
| 161 | 23112 | my $rgb = _pack( $r, $g, $b ); | |||||||||||||
| 162 | |||||||||||||||
| 163 | 23112 | $XTERM256_TO_RGB[$idx] = $rgb; | |||||||||||||
| 164 | 23112 | $XTERM256_TO_XTERM16[$idx] = RGBtoXTerm16( $rgb ); | |||||||||||||
| 165 | } | ||||||||||||||
| 166 | } | ||||||||||||||
| 167 | } | ||||||||||||||
| 168 | |||||||||||||||
| 169 | 107 | for my $i ( 0 .. 23 ) { | |||||||||||||
| 170 | 2568 | my $l = $i * 10 + 8; | |||||||||||||
| 171 | 2568 | my $idx = 232 + $i; | |||||||||||||
| 172 | 2568 | my $rgb = _pack( $l, $l, $l ); | |||||||||||||
| 173 | |||||||||||||||
| 174 | 2568 | $XTERM256_TO_RGB[$idx] = $rgb; | |||||||||||||
| 175 | 2568 | $XTERM256_TO_XTERM16[$idx] = RGBtoXTerm16( $rgb ); | |||||||||||||
| 176 | } | ||||||||||||||
| 177 | } | ||||||||||||||
| 178 | |||||||||||||||
| 179 | 1 | ▶ | sub XTerm256toRGB { # $rgb ($xterm256) | ||||||||||||
|
Subroutine
XTerm256toRGB: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 180 | 1 | my ( $idx ) = @_; | |||||||||||||
| 181 | 1 | assert ( looks_like_number $idx ); | |||||||||||||
| 182 | |||||||||||||||
| 183 | 1 | return $XTERM256_TO_RGB[ $idx & 0xff ]; | |||||||||||||
| 184 | } | ||||||||||||||
| 185 | |||||||||||||||
| 186 | 2 | ▶ | sub XTerm256toXTerm16 { # $xterm16 ($xterm256) | ||||||||||||
|
Subroutine
XTerm256toXTerm16: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 187 | 2 | my ( $idx ) = @_; | |||||||||||||
| 188 | 2 | assert ( looks_like_number $idx ); | |||||||||||||
| 189 | |||||||||||||||
| 190 | 2 | return $XTERM256_TO_XTERM16[ $idx & 0xff ]; | |||||||||||||
| 191 | } | ||||||||||||||
| 192 | |||||||||||||||
| 193 | 1 | ▶ | sub RGBtoXTerm256 { # $xterm256 ($rgb) | ||||||||||||
|
Subroutine
RGBtoXTerm256: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 194 | 1 | my ( $rgb ) = @_; | |||||||||||||
| 195 | 1 | assert ( looks_like_number $rgb ); | |||||||||||||
| 196 | |||||||||||||||
| 197 | 1 | my ( $r, $g, $b ) = _rgb8( $rgb ); | |||||||||||||
| 198 | |||||||||||||||
| 199 | 1 | my $best_i = 16; | |||||||||||||
| 200 | 1 | my $best_d = 0x7fffffff; | |||||||||||||
| 201 | |||||||||||||||
| 202 | 1 | for my $i ( 16 .. 255 ) { | |||||||||||||
| 203 | 240 | my ( $pr, $pg, $pb ) = _rgb8( $XTERM256_TO_RGB[$i] ); | |||||||||||||
| 204 | |||||||||||||||
| 205 | 240 | my $d = | |||||||||||||
| 206 | ( $r - $pr ) * ( $r - $pr ) | ||||||||||||||
| 207 | + ( $g - $pg ) * ( $g - $pg ) | ||||||||||||||
| 208 | + ( $b - $pb ) * ( $b - $pb ); | ||||||||||||||
| 209 | |||||||||||||||
| 210 | 240 | ▶ | if ( $d < $best_d ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 211 | 8 | $best_d = $d; | |||||||||||||
| 212 | 8 | $best_i = $i; | |||||||||||||
| 213 | } | ||||||||||||||
| 214 | } | ||||||||||||||
| 215 | |||||||||||||||
| 216 | 1 | return $best_i; | |||||||||||||
| 217 | } | ||||||||||||||
| 218 | |||||||||||||||
| 219 | 107 | 1; | |||||||||||||
| 220 | |||||||||||||||
| 221 | __END__ | ||||||||||||||