blib/lib/App/ANSIColorUtils.pm

filter: Statementstmt 17.5% 21 / 120 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 10 MC/DCmcdc 0.0% 0 / 5 Subroutinesub 17.6% 3 / 17 Podpod 100% 7 / 7 totaltotal 17.1% 31 / 181 CC 22 SCAR 30.0
CC
22
Cov
14%
show_colors
42.0
show_assigned_rgb_colors
30.0
show_text_using_color_gradation
30.0
CRAP 20.2
↑ Summary
1 package App::ANSIColorUtils;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
8 1 our $DATE = '2025-03-18'; # DATE
9 1 our $DIST = 'App-ANSIColorUtils'; # DIST
10 1 our $VERSION = '0.011'; # VERSION
11  
12 1 our %SPEC;
13  
14 $SPEC{show_ansi_color_table} = {
15 v => 1.1,
16 summary => 'Show a table of ANSI codes & colors',
17 args => {
18 width => {
19 schema => ['str*', in=>[8, 16, 256]],
20 default => 8,
21 cmdline_aliases => {
22 0 ▶ 8 => {is_flag=>1, summary => 'Shortcut for --width=8' , code => sub { $_[0]{width} = 8 }},
Subroutine __ANON__: not called CC=1 SCAR=6.9
23 0 ▶ 16 => {is_flag=>1, summary => 'Shortcut for --width=16' , code => sub { $_[0]{width} = 16 }},
Subroutine __ANON__: not called CC=1 SCAR=6.9
24 0 ▶ 256 => {is_flag=>1, summary => 'Shortcut for --width=256', code => sub { $_[0]{width} = 256 }},
Subroutine __ANON__: not called CC=1 SCAR=6.9
25 },
26 },
27 },
28 1 };
29 0 ▶sub show_ansi_color_table {
Subroutine show_ansi_color_table: not called CC=4 SCAR=30.0
Pod documented
30 0 require Color::ANSI::Util;
31  
32 0 my %args = @_;
33  
34 0 my $width = $args{width};
35  
36 0 my @rows;
37 0 for (0 .. $width - 1) {
38 0 ▶ push @rows, {
Branch: 2 branches
BranchTrueFalseTotal
$_ < 8 ? : 0 0 0
$_ < 16 ? : 0 0 0
39 code => $_,
40 color=>
41 $_ < 8 ? sprintf("\e[%dm%s\e[0m", 30+$_, "This is ANSI color #$_") :
42 $_ < 16 ? sprintf("\e[1;%dm%s\e[0m", 30+$_-8, "This is ANSI color #$_") :
43 sprintf("\e[38;5;%dm%s\e[0m", $_, "This is ANSI color #$_"),
44 };
45 }
46 0 [200, "OK", \@rows];
47 }
48  
49 $SPEC{show_colors} = {
50 1 v => 1.1,
51 summary => 'Show colors specified in argument as text with ANSI colors',
52 args => {
53 colors => {
54 'x.name.is_plural' => 1,
55 'x.name.singular' => 'color',
56 schema => ['array*', of=>'str*'],
57 req => 1,
58 pos => 0,
59 slurpy => 1,
60 },
61 },
62 };
63 0 ▶sub show_colors {
Subroutine show_colors: not called CC=6 SCAR=37.4
Pod documented
64 0 require Color::ANSI::Util;
65 0 require Graphics::ColorNamesLite::All;
66 #require String::Escape; # ugly: \x1b...
67 0 require Data::Dmp;
68  
69 0 my $codes = $Graphics::ColorNamesLite::All::NAMES_RGB_TABLE;
70  
71 0 my %args = @_;
72  
73 0 my @colornames;
74 my @colorcodes;
75 0 ▶ if ($args{_colors_hash}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($args{'_colors_hash'}) { } 0 0 0
76 0 @colornames = sort keys %{ $args{_colors_hash} };
77 0 @colorcodes = map { $args{_colors_hash}{$_} } @colornames;
78 } else {
79 0 @colornames = @colorcodes = @{ $args{colors} };
80 }
81  
82 0 my @rows;
83 0 for my $j (0 .. $#colornames) {
84 0 my $colorname = $colornames[$j];
85 0 my $colorcode = $colorcodes[$j];
86 0 ▶ unless ($colorcode =~ /\A[0-9A-fa-f]{6}\z/) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($colorcode =~ /\A[0-9A-fa-f]{6}\z/) 0 0 0
87 0 ▶ $colorcode = $codes->{$colorcode}; defined $colorcode or die "Unknown color name '$colorcode'";
Branch: 1 branch
BranchTrueFalseTotal
defined $colorcode or die "Unknown color name '${colorcode}'" 0 0 0
88 }
89 0 my $ansifg = Color::ANSI::Util::ansifg($colorcode);
90 0 my $ansibg = Color::ANSI::Util::ansibg($colorcode);
91 0 ▶ push @rows, {
Branch: 1 branch
BranchTrueFalseTotal
Color::RGB::Util::rgb_is_light($colorcode) ? : 0 0 0
92 name => $colorname,
93 rgb_code => $colorcode,
94 ansi_fg_code => Data::Dmp::dmp($ansifg),
95 ansi_bg_code => Data::Dmp::dmp($ansibg),
96 fg =>
97 $ansifg . "This is text with foreground color $colorname (#$colorcode)" . Color::ANSI::Util::ansi_reset(1) . "\n" .
98 $ansifg . "\e[1m" . "This is text with foreground color $colorname (#$colorcode) + BOLD" . Color::ANSI::Util::ansi_reset(1) . "\n",
99 bg => $ansibg . Color::ANSI::Util::ansifg(Color::RGB::Util::rgb_is_light($colorcode) ? "000000":"ffffff") . "This is text with background color $colorname (#$colorcode)" . Color::ANSI::Util::ansi_reset(1),
100 };
101 }
102 0 [200, "OK", \@rows];
103 }
104  
105 $SPEC{show_colors_from_scheme} = {
106 1 v => 1.1,
107 summary => 'Show colors from a Graphics::ColorNames scheme',
108 args => {
109 scheme => {
110 schema => 'perl::colorscheme::modname*',
111 req => 1,
112 pos => 0,
113 },
114 },
115 };
116 0 ▶sub show_colors_from_scheme {
Subroutine show_colors_from_scheme: not called CC=1 SCAR=6.9
Pod documented
117 0 my %args = @_;
118 0 my $mod = "Graphics::ColorNames::$args{scheme}";
119 0 (my $modpm = "$mod.pm") =~ s!::!/!g;
120 0 require $modpm;
121  
122 0 my $table = $mod->NamesRgbTable;
123 0 show_colors(colors => [sort keys %$table]);
124 }
125  
126 $SPEC{show_colors_from_theme} = {
127 1 v => 1.1,
128 summary => 'Show colors from a ColorTheme scheme',
129 args => {
130 theme => {
131 schema => 'perl::colortheme::modname_with_optional_args*',
132 req => 1,
133 pos => 0,
134 },
135 },
136 links => [
137 {url=>'prog:show-color-theme-swatch'},
138 ],
139 };
140 0 ▶sub show_colors_from_theme {
Subroutine show_colors_from_theme: not called CC=4 SCAR=30.0
Pod documented
141 0 require Module::Load::Util;
142  
143 0 my %args = @_;
144 0 ▶ my $mod = $args{theme}; $mod = "ColorTheme::$mod" unless $mod =~ /^ColorTheme::/;
Branch: 1 branch
BranchTrueFalseTotal
unless $mod =~ /^ColorTheme::/ 0 0 0
145 0 my $theme = Module::Load::Util::instantiate_class_with_optional_args($mod);
146  
147 0 my @item_names = $theme->list_items;
148 0 my %colors;
149 0 for my $item ($theme->list_items) {
150 0 my $k = $item;
151 0 my $v = $theme->get_item_color($item);
152 0 ▶ if (ref $v) {
Branch: 1 branch
BranchTrueFalseTotal
if (ref $v) 0 0 0
153 0 $k = "$k (hash or coderef)";
154 0 $v = "ffffff";
155 }
156 0 $colors{$k} = $v;
157 }
158 0 show_colors(_colors_hash =>\%colors);
159 }
160  
161 $SPEC{show_assigned_rgb_colors} = {
162 v => 1.1,
163 summary => 'Take arguments, pass them through assign_rgb_color(), show the results',
164 description => <<'_',
165  
166 `assign_rgb_color()` from <pm:Color::RGB::Util> takes a string, produce SHA1
167 digest from it, then take 24bit from the digest as the assigned color.
168  
169 _
170 args => {
171 strings => {
172 schema => ['array*', of=>'str*'],
173 req => 1,
174 pos => 0,
175 greedy => 1,
176 },
177 tone => {
178 schema => ['str*', in=>['light', 'dark']],
179 cmdline_aliases => {
180 0 ▶ light => {is_flag=>1, summary=>'Shortcut for --tone=light', code=>sub { $_[0]{tone} = 'light' }},
Subroutine __ANON__: not called CC=1 SCAR=6.9
181 0 ▶ dark => {is_flag=>1, summary=>'Shortcut for --tone=dark' , code=>sub { $_[0]{tone} = 'dark' }},
Subroutine __ANON__: not called CC=1 SCAR=6.9
182 },
183 },
184 },
185 1 };
186 0 ▶sub show_assigned_rgb_colors {
Subroutine show_assigned_rgb_colors: not called CC=5 SCAR=34.0
Pod documented
187 0 require Color::ANSI::Util;
188 0 require Color::RGB::Util;
189  
190 0 my %args = @_;
191  
192 0 ▶ my $tone = $args{tone} // '';
Condition0%0/2
$args{"tone"} // ""
l!l
00
MC/DC0%0/1
$args{"tone"} // ""
$args{"tone"}
Truth table0%0/2
A
A= $args{"tone"}
Aresult
11
00
193 0 my $strings = $args{strings};
194  
195 0 my @rows;
196 0 for (0 .. $#{ $strings }) {
197 0 my $str = $strings->[$_];
198 0 ▶ my $rgb =
Branch: 2 branches
BranchTrueFalseTotal
$tone eq 'light' ? : 0 0 0
$tone eq 'dark' ? : 0 0 0
199 $tone eq 'light' ? Color::RGB::Util::assign_rgb_light_color($str) :
200 $tone eq 'dark' ? Color::RGB::Util::assign_rgb_dark_color($str) :
201 Color::RGB::Util::assign_rgb_color($str);
202 0 push @rows, {
203 number => $_+1,
204 string => $str,
205 color => sprintf("%s%s\e[0m", Color::ANSI::Util::ansifg($rgb), "'$str' is assigned color #$rgb"),
206 "light?" => Color::RGB::Util::rgb_is_light($rgb),
207 };
208 }
209 0 [200, "OK", \@rows, {"table.fields" => [qw/number string color light?/]}];
210 }
211  
212 $SPEC{show_rand_rgb_colors} = {
213 v => 1.1,
214 summary => 'Produce N random RGB colors using rand_rgb_colors() and show the results',
215 args => {
216 n => {
217 schema => 'posint*',
218 req => 1,
219 pos => 0,
220 },
221 light_color => {
222 schema => 'bool',
223 default => 1,
224 cmdline_aliases => {
225 0 ▶ light_or_dark_color => {is_flag=>1, code=>sub { $_[0]{light_color} = undef }},
Subroutine __ANON__: not called CC=1 SCAR=6.9
226 0 ▶ dark_color => {is_flag=>1, code=>sub { $_[0]{light_color} = 0 }},
Subroutine __ANON__: not called CC=1 SCAR=6.9
227 },
228 },
229 },
230 1 };
231 0 ▶sub show_rand_rgb_colors {
Subroutine show_rand_rgb_colors: not called CC=3 SCAR=24.8
Pod documented
232 0 require Color::ANSI::Util;
233 0 require Color::RGB::Util;
234  
235 0 my %args = @_;
236 0 my $n = $args{n};
237  
238 my @colors = Color::RGB::Util::rand_rgb_colors({
239 light_color => $args{light_color},
240 0 }, $n);
241 0 my @rows;
242 0 for (1 .. $n) {
243 0 my $color = $colors[$_-1];
244 0 ▶ push @rows, {
Branch: 1 branch
BranchTrueFalseTotal
Color::RGB::Util::rgb_is_dark($color) ? : 0 0 0
245 number => $_,
246 color => sprintf("%s %s \e[0m",
247 Color::ANSI::Util::ansifg(Color::RGB::Util::rgb_is_dark($color) ? "ffffff" : "000000").
248 Color::ANSI::Util::ansibg($color),
249 "#".$color),
250 };
251 }
252 0 [200, "OK", \@rows, {"table.fields" => [qw/number color/]}];
253 }
254  
255 $SPEC{show_text_using_color_gradation} = {
256 1 v => 1.1,
257 summary => 'Print text using gradation between two colors',
258 description => <<'_',
259  
260 This can be used to demonstrate 24bit color support in terminal emulators.
261  
262 _
263 args => {
264 text => {
265 schema => ['str*', min_len=>1],
266 pos => 0,
267 description => <<'_',
268  
269 If unspecified, will show a bar of '=' across the terminal.
270  
271 _
272 },
273 color1 => {
274 schema => 'color::rgb24*',
275 default => 'ffff00',
276 },
277 color2 => {
278 schema => 'color::rgb24*',
279 default => '0000ff',
280 },
281 },
282 examples => [
283 {
284 args => {color1=>'blue', color2=>'pink', text=>'Hello, world'},
285 test => 0,
286 'x.doc_show_result'=>0,
287 },
288 ],
289 };
290 0 ▶sub show_text_using_color_gradation {
Subroutine show_text_using_color_gradation: not called CC=5 SCAR=34.0
Pod documented
291 0 require Color::ANSI::Util;
292 0 require Color::RGB::Util;
293 0 require Term::Size;
294  
295 0 my %args = @_;
296  
297 0 my $color1 = $args{color1};
298 0 my $color2 = $args{color2};
299  
300 0 my $text = $args{text};
301 0 ▶ $text //= do {
Condition0%0/3
$text //= do { my $width = ($ENV{"COLUMNS"} // (Term::Size::chars(scalar *STDOUT{"IO"}))[0]) // 80; "X" x $width }
l!l&&r!l&&!r
000
MC/DC0%0/2
$text //= do { my $width = ($ENV{"COLUMNS"} // (Term::Size::chars(scalar *STDOUT{"IO"}))[0]) // 80; "X" x $width }
$text do { my $width = ($ENV{"COLUMNS"} // (Term::Size::chars(scalar *STDOUT{"IO"}))[0]) // 80; "X" x $width }
Truth table0%0/3
A //= B
A= $textB= do { my $width = ($ENV{"COLUMNS"} // (Term::Size::chars(scalar *STDOUT{"IO"}))[0]) // 80; "X" x $width }
ABresult
1X1
011
000
302 0 ▶ my $width = $ENV{COLUMNS} // (Term::Size::chars(*STDOUT{IO}))[0] // 80;
Condition0%0/5
($ENV{"COLUMNS"} // (Term::Size::chars(scalar *STDOUT{"IO"}))[0]) // 80
l!l
00
$ENV{"COLUMNS"} // (Term::Size::chars(scalar *STDOUT{"IO"}))[0]
l!l&&r!l&&!r
000
MC/DC0%0/2
($ENV{"COLUMNS"} // (Term::Size::chars(scalar *STDOUT{"IO"}))[0]) // 80
$ENV{"COLUMNS"} (Term::Size::chars(scalar *STDOUT{"IO"}))[0]
Truth table0%0/3
A // B
A= $ENV{"COLUMNS"}B= (Term::Size::chars(scalar *STDOUT{"IO"}))[0]
ABresult
1X1
011
000
303 0 "X" x $width;
304 };
305 0 my $len = length $text;
306 0 my $i = 0;
307 0 for my $c (split //, $text) {
308 0 $i++;
309 0 my $color = Color::RGB::Util::mix_2_rgb_colors($color1, $color2, $i/$len);
310 0 print Color::ANSI::Util::ansifg($color), $c;
311 }
312 0 print "\n\e[0m";
313  
314 0 [200];
315 }
316  
317 1 1;
318 # ABSTRACT: Utilities related to ANSI color
319  
320 __END__
↑ Summary