Gradients/Utils.pm

filter: Statementstmt 87.7% 86 / 98 Branchbran 55.0% 22 / 40 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 8 / 8 Podpod 40.0% 2 / 5 totaltotal 78.1% 118 / 151 CC 22 SCAR 20.2
CC
22
Cov
78%
rgb_to_hsv
12.2
hsv_to_rgb
9.2
build_contrast
3.8
CRAP 7.6
↑ Summary BUILD.pl »
1 package Term::ANSIColor::Gradients::Utils ;
2  
3 1 ▶use strict ;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use warnings ;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use Exporter 'import' ;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $VERSION = '0.10' ;
8 1 our @EXPORT_OK = qw(build_contrast intensity_shift) ;
9  
10 # ---------------------------------------------------------------------------
11 # ANSI 256 color index to RGB table, built once at load time
12 # ---------------------------------------------------------------------------
13  
14 1 my @CUBE = (0, 95, 135, 175, 215, 255) ;
15  
16 1 my @SYS_RGB =
17 (
18 [ 0, 0, 0], [128, 0, 0], [ 0,128, 0], [128,128, 0],
19 [ 0, 0,128], [128, 0,128], [ 0,128,128], [192,192,192],
20 [128,128,128], [255, 0, 0], [ 0,255, 0], [255,255, 0],
21 [ 0, 0,255], [255, 0,255], [ 0,255,255], [255,255,255],
22 ) ;
23  
24 1 my @ANSI_RGB ;
25  
26 {
27 1 $ANSI_RGB[$_] = $SYS_RGB[$_] for 0 .. 15 ;
28  
29 1 for my $i (16 .. 231)
30 {
31 216 my $n = $i - 16 ;
32 216 my $r = int($n / 36) ;
33 216 my $g = int(($n % 36) / 6) ;
34 216 my $b = $n % 6 ;
35 216 $ANSI_RGB[$i] = [$CUBE[$r], $CUBE[$g], $CUBE[$b]] ;
36 }
37  
38 1 for my $i (232 .. 255)
39 {
40 24 my $v = 8 + ($i - 232) * 10 ;
41 24 $ANSI_RGB[$i] = [$v, $v, $v] ;
42 }
43 }
44  
45 # ---------------------------------------------------------------------------
46  
47 sub rgb_to_hsv
48 3 ▶{
Subroutine rgb_to_hsv: called CC=10 SCAR=25.1
Pod undocumented
49 3 my ($r, $g, $b) = @_ ;
50  
51 3 $r /= 255.0 ;
52 3 $g /= 255.0 ;
53 3 $b /= 255.0 ;
54  
55 3 my $max = $r ;
56 3 ▶$max = $g if $g > $max ;
Branch: 1 branch
BranchTrueFalseTotal
if $g > $max 0 3 3
57 3 ▶$max = $b if $b > $max ;
Branch: 1 branch
BranchTrueFalseTotal
if $b > $max 3 0 3
58  
59 3 my $min = $r ;
60 3 ▶$min = $g if $g < $min ;
Branch: 1 branch
BranchTrueFalseTotal
if $g < $min 0 3 3
61 3 ▶$min = $b if $b < $min ;
Branch: 1 branch
BranchTrueFalseTotal
if $b < $min 0 3 3
62  
63 3 my $delta = $max - $min ;
64 3 my $v = $max ;
65 3 ▶my $s = $max > 0 ? $delta / $max : 0 ;
Branch: 1 branch
BranchTrueFalseTotal
$max > 0 ? : 3 0 3
66 3 my $h = 0 ;
67  
68 3 ▶if ($delta > 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($delta > 0) 3 0 3
69 {
70 0 ▶ if ($max == $r) { $h = 60 * (($g - $b) / $delta) }
Branch: 2 branches
BranchTrueFalseTotal
if ($max == $r) { } 0 3 3
elsif ($max == $g) { } 0 3 3
71 0 elsif ($max == $g) { $h = 60 * (($b - $r) / $delta) + 120 }
72 3 else { $h = 60 * (($r - $g) / $delta) + 240 }
73  
74 3 ▶ $h += 360 if $h < 0 ;
Branch: 1 branch
BranchTrueFalseTotal
if $h < 0 0 3 3
75 }
76  
77 3 return ($h, $s, $v) ;
78 }
79  
80 # ---------------------------------------------------------------------------
81  
82 sub hsv_to_rgb
83 3 ▶{
Subroutine hsv_to_rgb: called CC=7 SCAR=22.2
Pod undocumented
84 3 my ($h, $s, $v) = @_ ;
85  
86 3 ▶if ($s == 0)
Branch: 1 branch
BranchTrueFalseTotal
if ($s == 0) 0 3 3
87 {
88 0 my $c = int($v * 255 + 0.5) ;
89 0 return ($c, $c, $c) ;
90 }
91  
92 3 $h /= 60.0 ;
93 3 my $i = int($h) ;
94 3 my $f = $h - $i ;
95 3 my $p = $v * (1 - $s) ;
96 3 my $q = $v * (1 - $s * $f) ;
97 3 my $t = $v * (1 - $s * (1 - $f)) ;
98  
99 3 my ($r, $g, $b) ;
100 0 ▶if ($i == 0) { ($r, $g, $b) = ($v, $t, $p) }
Branch: 5 branches
BranchTrueFalseTotal
if ($i == 0) { } 0 3 3
elsif ($i == 1) { } 1 2 3
elsif ($i == 2) { } 0 2 2
elsif ($i == 3) { } 0 2 2
elsif ($i == 4) { } 2 0 2
101 1 elsif ($i == 1) { ($r, $g, $b) = ($q, $v, $p) }
102 0 elsif ($i == 2) { ($r, $g, $b) = ($p, $v, $t) }
103 0 elsif ($i == 3) { ($r, $g, $b) = ($p, $q, $v) }
104 2 elsif ($i == 4) { ($r, $g, $b) = ($t, $p, $v) }
105 0 else { ($r, $g, $b) = ($v, $p, $q) }
106  
107 3 return (int($r * 255 + 0.5), int($g * 255 + 0.5), int($b * 255 + 0.5)) ;
108 }
109  
110 # ---------------------------------------------------------------------------
111  
112 sub nearest_ansi
113 3 ▶{
Subroutine nearest_ansi: called CC=3 SCAR=11.0
Pod undocumented
114 3 my ($tr, $tg, $tb) = @_ ;
115  
116 3 my $best_idx = 0 ;
117 3 my $best_dist = 1e18 ;
118  
119 3 for my $i (0 .. 255)
120 {
121 768 my ($r, $g, $b) = @{$ANSI_RGB[$i]} ;
122 768 my $d = ($r - $tr) ** 2 + ($g - $tg) ** 2 + ($b - $tb) ** 2 ;
123  
124 768 ▶ if ($d < $best_dist)
Branch: 1 branch
BranchTrueFalseTotal
if ($d < $best_dist) 10 758 768
125 {
126 10 $best_dist = $d ;
127 10 $best_idx = $i ;
128 }
129 }
130  
131 3 return $best_idx ;
132 }
133  
134 # ---------------------------------------------------------------------------
135  
136 sub build_contrast
137 1 ▶{
Subroutine build_contrast: called CC=3 SCAR=13.2
Pod documented
138 1 my ($idx) = @_ ;
139  
140 1 my ($r, $g, $b) = @{$ANSI_RGB[$idx]} ;
141 1 my ($h, $s, $v) = rgb_to_hsv($r, $g, $b) ;
142  
143 # Greyscale: no meaningful hue, flip luminance instead
144 1 ▶if ($s < 0.15)
Branch: 1 branch
BranchTrueFalseTotal
if ($s < "0.15") 0 1 1
145 {
146 0 my $lum = 0.299 * $r + 0.587 * $g + 0.114 * $b ;
147 0 ▶ my $cv = $lum < 128 ? 1.0 : 0.0 ;
Branch: 1 branch
BranchTrueFalseTotal
$lum < 128 ? : 0 0 0
148 0 my ($nr, $ng, $nb) = hsv_to_rgb($h, $s, $cv) ;
149  
150 0 return nearest_ansi($nr, $ng, $nb) ;
151 }
152  
153 # Chromatic: complementary hue, same saturation and value
154 1 my $ch = ($h + 180) % 360 ;
155 1 my ($nr, $ng, $nb) = hsv_to_rgb($ch, $s, $v) ;
156  
157 1 return nearest_ansi($nr, $ng, $nb) ;
158 }
159  
160 # ---------------------------------------------------------------------------
161  
162 sub intensity_shift
163 2 ▶{
Subroutine intensity_shift: called CC=3 SCAR=11.0
Pod documented
164 2 my ($idx, $delta) = @_ ;
165  
166 2 my ($r, $g, $b) = @{$ANSI_RGB[$idx]} ;
167 2 my ($h, $s, $v) = rgb_to_hsv($r, $g, $b) ;
168  
169 2 $v += $delta * 0.05 ;
170 2 ▶$v = 0 if $v < 0 ;
Branch: 1 branch
BranchTrueFalseTotal
if $v < 0 0 2 2
171 2 ▶$v = 1 if $v > 1 ;
Branch: 1 branch
BranchTrueFalseTotal
if $v > 1 1 1 2
172  
173 2 my ($nr, $ng, $nb) = hsv_to_rgb($h, $s, $v) ;
174  
175 2 return nearest_ansi($nr, $ng, $nb) ;
176 }
177  
178 1 1 ;
179  
180 __END__
↑ Summary BUILD.pl »