line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Color::ANSI::Util; |
2
|
|
|
|
|
|
|
|
3
|
|
|
|
|
|
|
our $DATE = '2019-08-20'; # DATE |
4
|
|
|
|
|
|
|
our $VERSION = '0.163'; # VERSION |
5
|
|
|
|
|
|
|
|
6
|
1
|
|
|
1
|
|
134807
|
use 5.010001; |
|
1
|
|
|
|
|
7
|
|
7
|
1
|
|
|
1
|
|
5
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
18
|
|
8
|
1
|
|
|
1
|
|
4
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
25
|
|
9
|
|
|
|
|
|
|
|
10
|
1
|
|
|
1
|
|
438
|
use Color::RGB::Util qw(rgb_diff); |
|
1
|
|
|
|
|
2355
|
|
|
1
|
|
|
|
|
1915
|
|
11
|
|
|
|
|
|
|
|
12
|
|
|
|
|
|
|
require Exporter; |
13
|
|
|
|
|
|
|
our @ISA = qw(Exporter); |
14
|
|
|
|
|
|
|
our @EXPORT_OK = qw( |
15
|
|
|
|
|
|
|
ansi16_to_rgb |
16
|
|
|
|
|
|
|
rgb_to_ansi16 |
17
|
|
|
|
|
|
|
rgb_to_ansi16_fg_code |
18
|
|
|
|
|
|
|
ansi16fg |
19
|
|
|
|
|
|
|
rgb_to_ansi16_bg_code |
20
|
|
|
|
|
|
|
ansi16bg |
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
ansi256_to_rgb |
23
|
|
|
|
|
|
|
rgb_to_ansi256 |
24
|
|
|
|
|
|
|
rgb_to_ansi256_fg_code |
25
|
|
|
|
|
|
|
ansi256fg |
26
|
|
|
|
|
|
|
rgb_to_ansi256_bg_code |
27
|
|
|
|
|
|
|
ansi256bg |
28
|
|
|
|
|
|
|
|
29
|
|
|
|
|
|
|
rgb_to_ansi24b_fg_code |
30
|
|
|
|
|
|
|
ansi24bfg |
31
|
|
|
|
|
|
|
rgb_to_ansi24b_bg_code |
32
|
|
|
|
|
|
|
ansi24bbg |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
rgb_to_ansi_fg_code |
35
|
|
|
|
|
|
|
ansifg |
36
|
|
|
|
|
|
|
rgb_to_ansi_bg_code |
37
|
|
|
|
|
|
|
ansibg |
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
ansi_reset |
40
|
|
|
|
|
|
|
); |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
our %SPEC; |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
my %ansi16 = ( |
45
|
|
|
|
|
|
|
0 => '000000', |
46
|
|
|
|
|
|
|
1 => '800000', |
47
|
|
|
|
|
|
|
2 => '008000', |
48
|
|
|
|
|
|
|
3 => '808000', |
49
|
|
|
|
|
|
|
4 => '000080', |
50
|
|
|
|
|
|
|
5 => '800080', |
51
|
|
|
|
|
|
|
6 => '008080', |
52
|
|
|
|
|
|
|
7 => 'c0c0c0', |
53
|
|
|
|
|
|
|
8 => '808080', |
54
|
|
|
|
|
|
|
9 => 'ff0000', |
55
|
|
|
|
|
|
|
10 => '00ff00', |
56
|
|
|
|
|
|
|
11 => 'ffff00', |
57
|
|
|
|
|
|
|
12 => '0000ff', |
58
|
|
|
|
|
|
|
13 => 'ff00ff', |
59
|
|
|
|
|
|
|
14 => '00ffff', |
60
|
|
|
|
|
|
|
15 => 'ffffff', |
61
|
|
|
|
|
|
|
); |
62
|
|
|
|
|
|
|
my @revansi16; |
63
|
|
|
|
|
|
|
for my $idx (sort {$a<=>$b} keys %ansi16) { |
64
|
|
|
|
|
|
|
push @revansi16, [$ansi16{$idx}, $idx]; |
65
|
|
|
|
|
|
|
} |
66
|
|
|
|
|
|
|
|
67
|
|
|
|
|
|
|
my %ansi256 = ( |
68
|
|
|
|
|
|
|
%ansi16, |
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
16 => '000000', 17 => '00005f', 18 => '000087', 19 => '0000af', 20 => '0000d7', 21 => '0000ff', |
71
|
|
|
|
|
|
|
22 => '005f00', 23 => '005f5f', 24 => '005f87', 25 => '005faf', 26 => '005fd7', 27 => '005fff', |
72
|
|
|
|
|
|
|
28 => '008700', 29 => '00875f', 30 => '008787', 31 => '0087af', 32 => '0087d7', 33 => '0087ff', |
73
|
|
|
|
|
|
|
34 => '00af00', 35 => '00af5f', 36 => '00af87', 37 => '00afaf', 38 => '00afd7', 39 => '00afff', |
74
|
|
|
|
|
|
|
40 => '00d700', 41 => '00d75f', 42 => '00d787', 43 => '00d7af', 44 => '00d7d7', 45 => '00d7ff', |
75
|
|
|
|
|
|
|
46 => '00ff00', 47 => '00ff5f', 48 => '00ff87', 49 => '00ffaf', 50 => '00ffd7', 51 => '00ffff', |
76
|
|
|
|
|
|
|
52 => '5f0000', 53 => '5f005f', 54 => '5f0087', 55 => '5f00af', 56 => '5f00d7', 57 => '5f00ff', |
77
|
|
|
|
|
|
|
58 => '5f5f00', 59 => '5f5f5f', 60 => '5f5f87', 61 => '5f5faf', 62 => '5f5fd7', 63 => '5f5fff', |
78
|
|
|
|
|
|
|
64 => '5f8700', 65 => '5f875f', 66 => '5f8787', 67 => '5f87af', 68 => '5f87d7', 69 => '5f87ff', |
79
|
|
|
|
|
|
|
70 => '5faf00', 71 => '5faf5f', 72 => '5faf87', 73 => '5fafaf', 74 => '5fafd7', 75 => '5fafff', |
80
|
|
|
|
|
|
|
76 => '5fd700', 77 => '5fd75f', 78 => '5fd787', 79 => '5fd7af', 80 => '5fd7d7', 81 => '5fd7ff', |
81
|
|
|
|
|
|
|
82 => '5fff00', 83 => '5fff5f', 84 => '5fff87', 85 => '5fffaf', 86 => '5fffd7', 87 => '5fffff', |
82
|
|
|
|
|
|
|
88 => '870000', 89 => '87005f', 90 => '870087', 91 => '8700af', 92 => '8700d7', 93 => '8700ff', |
83
|
|
|
|
|
|
|
94 => '875f00', 95 => '875f5f', 96 => '875f87', 97 => '875faf', 98 => '875fd7', 99 => '875fff', |
84
|
|
|
|
|
|
|
100 => '878700', 101 => '87875f', 102 => '878787', 103 => '8787af', 104 => '8787d7', 105 => '8787ff', |
85
|
|
|
|
|
|
|
106 => '87af00', 107 => '87af5f', 108 => '87af87', 109 => '87afaf', 110 => '87afd7', 111 => '87afff', |
86
|
|
|
|
|
|
|
112 => '87d700', 113 => '87d75f', 114 => '87d787', 115 => '87d7af', 116 => '87d7d7', 117 => '87d7ff', |
87
|
|
|
|
|
|
|
118 => '87ff00', 119 => '87ff5f', 120 => '87ff87', 121 => '87ffaf', 122 => '87ffd7', 123 => '87ffff', |
88
|
|
|
|
|
|
|
124 => 'af0000', 125 => 'af005f', 126 => 'af0087', 127 => 'af00af', 128 => 'af00d7', 129 => 'af00ff', |
89
|
|
|
|
|
|
|
130 => 'af5f00', 131 => 'af5f5f', 132 => 'af5f87', 133 => 'af5faf', 134 => 'af5fd7', 135 => 'af5fff', |
90
|
|
|
|
|
|
|
136 => 'af8700', 137 => 'af875f', 138 => 'af8787', 139 => 'af87af', 140 => 'af87d7', 141 => 'af87ff', |
91
|
|
|
|
|
|
|
142 => 'afaf00', 143 => 'afaf5f', 144 => 'afaf87', 145 => 'afafaf', 146 => 'afafd7', 147 => 'afafff', |
92
|
|
|
|
|
|
|
148 => 'afd700', 149 => 'afd75f', 150 => 'afd787', 151 => 'afd7af', 152 => 'afd7d7', 153 => 'afd7ff', |
93
|
|
|
|
|
|
|
154 => 'afff00', 155 => 'afff5f', 156 => 'afff87', 157 => 'afffaf', 158 => 'afffd7', 159 => 'afffff', |
94
|
|
|
|
|
|
|
160 => 'd70000', 161 => 'd7005f', 162 => 'd70087', 163 => 'd700af', 164 => 'd700d7', 165 => 'd700ff', |
95
|
|
|
|
|
|
|
166 => 'd75f00', 167 => 'd75f5f', 168 => 'd75f87', 169 => 'd75faf', 170 => 'd75fd7', 171 => 'd75fff', |
96
|
|
|
|
|
|
|
172 => 'd78700', 173 => 'd7875f', 174 => 'd78787', 175 => 'd787af', 176 => 'd787d7', 177 => 'd787ff', |
97
|
|
|
|
|
|
|
178 => 'd7af00', 179 => 'd7af5f', 180 => 'd7af87', 181 => 'd7afaf', 182 => 'd7afd7', 183 => 'd7afff', |
98
|
|
|
|
|
|
|
184 => 'd7d700', 185 => 'd7d75f', 186 => 'd7d787', 187 => 'd7d7af', 188 => 'd7d7d7', 189 => 'd7d7ff', |
99
|
|
|
|
|
|
|
190 => 'd7ff00', 191 => 'd7ff5f', 192 => 'd7ff87', 193 => 'd7ffaf', 194 => 'd7ffd7', 195 => 'd7ffff', |
100
|
|
|
|
|
|
|
196 => 'ff0000', 197 => 'ff005f', 198 => 'ff0087', 199 => 'ff00af', 200 => 'ff00d7', 201 => 'ff00ff', |
101
|
|
|
|
|
|
|
202 => 'ff5f00', 203 => 'ff5f5f', 204 => 'ff5f87', 205 => 'ff5faf', 206 => 'ff5fd7', 207 => 'ff5fff', |
102
|
|
|
|
|
|
|
208 => 'ff8700', 209 => 'ff875f', 210 => 'ff8787', 211 => 'ff87af', 212 => 'ff87d7', 213 => 'ff87ff', |
103
|
|
|
|
|
|
|
214 => 'ffaf00', 215 => 'ffaf5f', 216 => 'ffaf87', 217 => 'ffafaf', 218 => 'ffafd7', 219 => 'ffafff', |
104
|
|
|
|
|
|
|
220 => 'ffd700', 221 => 'ffd75f', 222 => 'ffd787', 223 => 'ffd7af', 224 => 'ffd7d7', 225 => 'ffd7ff', |
105
|
|
|
|
|
|
|
226 => 'ffff00', 227 => 'ffff5f', 228 => 'ffff87', 229 => 'ffffaf', 230 => 'ffffd7', 231 => 'ffffff', |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
232 => '080808', 233 => '121212', 234 => '1c1c1c', 235 => '262626', 236 => '303030', 237 => '3a3a3a', |
108
|
|
|
|
|
|
|
238 => '444444', 239 => '4e4e4e', 240 => '585858', 241 => '606060', 242 => '666666', 243 => '767676', |
109
|
|
|
|
|
|
|
244 => '808080', 245 => '8a8a8a', 246 => '949494', 247 => '9e9e9e', 248 => 'a8a8a8', 249 => 'b2b2b2', |
110
|
|
|
|
|
|
|
250 => 'bcbcbc', 251 => 'c6c6c6', 252 => 'd0d0d0', 253 => 'dadada', 254 => 'e4e4e4', 255 => 'eeeeee', |
111
|
|
|
|
|
|
|
); |
112
|
|
|
|
|
|
|
my @revansi256; |
113
|
|
|
|
|
|
|
for my $idx (sort {$a<=>$b} keys %ansi256) { |
114
|
|
|
|
|
|
|
push @revansi256, [$ansi256{$idx}, $idx]; |
115
|
|
|
|
|
|
|
} |
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
$SPEC{ansi16_to_rgb} = { |
118
|
|
|
|
|
|
|
v => 1.1, |
119
|
|
|
|
|
|
|
summary => 'Convert ANSI-16 color to RGB', |
120
|
|
|
|
|
|
|
description => <<'_', |
121
|
|
|
|
|
|
|
|
122
|
|
|
|
|
|
|
Returns 6-hexdigit, e.g. 'ff00cc'. |
123
|
|
|
|
|
|
|
|
124
|
|
|
|
|
|
|
_ |
125
|
|
|
|
|
|
|
args => { |
126
|
|
|
|
|
|
|
color => { |
127
|
|
|
|
|
|
|
schema => 'color::ansi16*', |
128
|
|
|
|
|
|
|
req => 1, |
129
|
|
|
|
|
|
|
pos => 0, |
130
|
|
|
|
|
|
|
}, |
131
|
|
|
|
|
|
|
}, |
132
|
|
|
|
|
|
|
args_as => 'array', |
133
|
|
|
|
|
|
|
result => { |
134
|
|
|
|
|
|
|
schema => 'color::rgb24*', |
135
|
|
|
|
|
|
|
}, |
136
|
|
|
|
|
|
|
result_naked => 1, |
137
|
|
|
|
|
|
|
}; |
138
|
|
|
|
|
|
|
sub ansi16_to_rgb { |
139
|
4
|
|
|
4
|
1
|
1302
|
my ($input) = @_; |
140
|
|
|
|
|
|
|
|
141
|
4
|
100
|
|
|
|
33
|
if ($input =~ /^\d+$/) { |
|
|
50
|
|
|
|
|
|
142
|
2
|
50
|
33
|
|
|
12
|
if ($input >= 0 && $input <= 15) { |
143
|
2
|
|
|
|
|
12
|
return $ansi16{$input + 0}; # to remove prefix zero e.g. "06" |
144
|
|
|
|
|
|
|
} else { |
145
|
0
|
|
|
|
|
0
|
die "Invalid ANSI 16-color number '$input'"; |
146
|
|
|
|
|
|
|
} |
147
|
|
|
|
|
|
|
} elsif ($input =~ /^(?:(bold|bright) \s )?(black|red|green|yellow|blue|magenta|cyan|white)$/ix) { |
148
|
2
|
|
100
|
|
|
12
|
my ($bold, $col) = (lc($1 // ""), lc($2)); |
149
|
2
|
|
|
|
|
4
|
my $i; |
150
|
2
|
50
|
|
|
|
6
|
if ($col eq 'black') { |
|
|
50
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
151
|
0
|
|
|
|
|
0
|
$i = 0; |
152
|
|
|
|
|
|
|
} elsif ($col eq 'red') { |
153
|
2
|
|
|
|
|
2
|
$i = 1; |
154
|
|
|
|
|
|
|
} elsif ($col eq 'green') { |
155
|
0
|
|
|
|
|
0
|
$i = 2; |
156
|
|
|
|
|
|
|
} elsif ($col eq 'yellow') { |
157
|
0
|
|
|
|
|
0
|
$i = 3; |
158
|
|
|
|
|
|
|
} elsif ($col eq 'blue') { |
159
|
0
|
|
|
|
|
0
|
$i = 4; |
160
|
|
|
|
|
|
|
} elsif ($col eq 'magenta') { |
161
|
0
|
|
|
|
|
0
|
$i = 5; |
162
|
|
|
|
|
|
|
} elsif ($col eq 'cyan') { |
163
|
0
|
|
|
|
|
0
|
$i = 6; |
164
|
|
|
|
|
|
|
} elsif ($col eq 'white') { |
165
|
0
|
|
|
|
|
0
|
$i = 7; |
166
|
|
|
|
|
|
|
} |
167
|
2
|
100
|
|
|
|
5
|
$i += 8 if $bold; |
168
|
2
|
|
|
|
|
10
|
return $ansi16{$i}; |
169
|
|
|
|
|
|
|
} else { |
170
|
0
|
|
|
|
|
0
|
die "Invalid ANSI 16-color name '$input'"; |
171
|
|
|
|
|
|
|
} |
172
|
|
|
|
|
|
|
} |
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
sub _rgb_to_indexed { |
175
|
16
|
|
|
16
|
|
26
|
my ($rgb, $table) = @_; |
176
|
|
|
|
|
|
|
|
177
|
16
|
|
|
|
|
20
|
my ($smallest_diff, $res); |
178
|
16
|
|
|
|
|
31
|
for my $e (@$table) { |
179
|
730
|
|
|
|
|
1493
|
my $diff = rgb_diff($rgb, $e->[0], 'hsv_hue1'); |
180
|
|
|
|
|
|
|
# exact match, return immediately |
181
|
730
|
100
|
|
|
|
69110
|
return $e->[1] if $diff == 0; |
182
|
729
|
100
|
100
|
|
|
2167
|
if (!defined($smallest_diff) || $smallest_diff > $diff) { |
183
|
50
|
|
|
|
|
62
|
$smallest_diff = $diff; |
184
|
50
|
|
|
|
|
89
|
$res = $e->[1]; |
185
|
|
|
|
|
|
|
} |
186
|
|
|
|
|
|
|
} |
187
|
15
|
|
|
|
|
47
|
return $res; |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
$SPEC{ansi256_to_rgb} = { |
191
|
|
|
|
|
|
|
v => 1.1, |
192
|
|
|
|
|
|
|
summary => 'Convert ANSI-256 color to RGB', |
193
|
|
|
|
|
|
|
args => { |
194
|
|
|
|
|
|
|
color => { |
195
|
|
|
|
|
|
|
schema => 'color::ansi256*', |
196
|
|
|
|
|
|
|
req => 1, |
197
|
|
|
|
|
|
|
pos => 0, |
198
|
|
|
|
|
|
|
}, |
199
|
|
|
|
|
|
|
}, |
200
|
|
|
|
|
|
|
args_as => 'array', |
201
|
|
|
|
|
|
|
result => { |
202
|
|
|
|
|
|
|
schema => 'color::rgb24', |
203
|
|
|
|
|
|
|
}, |
204
|
|
|
|
|
|
|
result_naked => 1, |
205
|
|
|
|
|
|
|
}; |
206
|
|
|
|
|
|
|
sub ansi256_to_rgb { |
207
|
1
|
|
|
1
|
1
|
3565
|
my ($input) = @_; |
208
|
|
|
|
|
|
|
|
209
|
1
|
|
|
|
|
3
|
$input += 0; |
210
|
1
|
50
|
|
|
|
6
|
exists($ansi256{$input}) or die "Invalid ANSI 256-color index '$input'"; |
211
|
1
|
|
|
|
|
5
|
$ansi256{$input}; |
212
|
|
|
|
|
|
|
} |
213
|
|
|
|
|
|
|
|
214
|
|
|
|
|
|
|
$SPEC{rgb_to_ansi16} = { |
215
|
|
|
|
|
|
|
v => 1.1, |
216
|
|
|
|
|
|
|
summary => 'Convert RGB to ANSI-16 color', |
217
|
|
|
|
|
|
|
args => { |
218
|
|
|
|
|
|
|
color => { |
219
|
|
|
|
|
|
|
schema => 'color::rgb24*', |
220
|
|
|
|
|
|
|
req => 1, |
221
|
|
|
|
|
|
|
pos => 0, |
222
|
|
|
|
|
|
|
}, |
223
|
|
|
|
|
|
|
}, |
224
|
|
|
|
|
|
|
args_as => 'array', |
225
|
|
|
|
|
|
|
result => { |
226
|
|
|
|
|
|
|
schema => 'color::ansi16*', |
227
|
|
|
|
|
|
|
}, |
228
|
|
|
|
|
|
|
result_naked => 1, |
229
|
|
|
|
|
|
|
}; |
230
|
|
|
|
|
|
|
sub rgb_to_ansi16 { |
231
|
2
|
|
|
2
|
1
|
5
|
my ($input) = @_; |
232
|
2
|
|
|
|
|
6
|
_rgb_to_indexed($input, \@revansi16); |
233
|
|
|
|
|
|
|
} |
234
|
|
|
|
|
|
|
|
235
|
|
|
|
|
|
|
$SPEC{rgb_to_ansi256} = { |
236
|
|
|
|
|
|
|
v => 1.1, |
237
|
|
|
|
|
|
|
summary => 'Convert RGB to ANSI-256 color', |
238
|
|
|
|
|
|
|
args => { |
239
|
|
|
|
|
|
|
color => { |
240
|
|
|
|
|
|
|
schema => 'color::rgb24*', |
241
|
|
|
|
|
|
|
req => 1, |
242
|
|
|
|
|
|
|
pos => 0, |
243
|
|
|
|
|
|
|
}, |
244
|
|
|
|
|
|
|
}, |
245
|
|
|
|
|
|
|
args_as => 'array', |
246
|
|
|
|
|
|
|
result => { |
247
|
|
|
|
|
|
|
schema => 'color::ansi256*', |
248
|
|
|
|
|
|
|
}, |
249
|
|
|
|
|
|
|
result_naked => 1, |
250
|
|
|
|
|
|
|
}; |
251
|
|
|
|
|
|
|
sub rgb_to_ansi256 { |
252
|
3
|
|
|
3
|
1
|
8
|
my ($input) = @_; |
253
|
3
|
|
|
|
|
7
|
_rgb_to_indexed($input, \@revansi256); |
254
|
|
|
|
|
|
|
} |
255
|
|
|
|
|
|
|
|
256
|
|
|
|
|
|
|
$SPEC{rgb_to_ansi16_fg_code} = { |
257
|
|
|
|
|
|
|
v => 1.1, |
258
|
|
|
|
|
|
|
summary => 'Convert RGB to ANSI-16 color escape sequence to change foreground color', |
259
|
|
|
|
|
|
|
args => { |
260
|
|
|
|
|
|
|
color => { |
261
|
|
|
|
|
|
|
schema => 'color::rgb24*', |
262
|
|
|
|
|
|
|
req => 1, |
263
|
|
|
|
|
|
|
pos => 0, |
264
|
|
|
|
|
|
|
}, |
265
|
|
|
|
|
|
|
}, |
266
|
|
|
|
|
|
|
args_as => 'array', |
267
|
|
|
|
|
|
|
result => { |
268
|
|
|
|
|
|
|
schema => 'str*', |
269
|
|
|
|
|
|
|
}, |
270
|
|
|
|
|
|
|
result_naked => 1, |
271
|
|
|
|
|
|
|
}; |
272
|
|
|
|
|
|
|
sub rgb_to_ansi16_fg_code { |
273
|
4
|
|
|
4
|
1
|
10
|
my ($input) = @_; |
274
|
|
|
|
|
|
|
|
275
|
4
|
|
|
|
|
10
|
my $res = _rgb_to_indexed($input, \@revansi16); |
276
|
4
|
100
|
|
|
|
31
|
return "\e[" . ($res >= 8 ? ($res+30-8) . ";1" : ($res+30)) . "m"; |
277
|
|
|
|
|
|
|
} |
278
|
|
|
|
|
|
|
|
279
|
1
|
|
|
1
|
1
|
5
|
sub ansi16fg { goto &rgb_to_ansi16_fg_code } |
280
|
|
|
|
|
|
|
|
281
|
|
|
|
|
|
|
$SPEC{rgb_to_ansi16_bg_code} = { |
282
|
|
|
|
|
|
|
v => 1.1, |
283
|
|
|
|
|
|
|
summary => 'Convert RGB to ANSI-16 color escape sequence to change background color', |
284
|
|
|
|
|
|
|
args => { |
285
|
|
|
|
|
|
|
color => { |
286
|
|
|
|
|
|
|
schema => 'color::rgb24*', |
287
|
|
|
|
|
|
|
req => 1, |
288
|
|
|
|
|
|
|
pos => 0, |
289
|
|
|
|
|
|
|
}, |
290
|
|
|
|
|
|
|
}, |
291
|
|
|
|
|
|
|
args_as => 'array', |
292
|
|
|
|
|
|
|
result => { |
293
|
|
|
|
|
|
|
schema => 'str*', |
294
|
|
|
|
|
|
|
}, |
295
|
|
|
|
|
|
|
result_naked => 1, |
296
|
|
|
|
|
|
|
}; |
297
|
|
|
|
|
|
|
sub rgb_to_ansi16_bg_code { |
298
|
2
|
|
|
2
|
1
|
5
|
my ($input) = @_; |
299
|
|
|
|
|
|
|
|
300
|
2
|
|
|
|
|
5
|
my $res = _rgb_to_indexed($input, \@revansi16); |
301
|
2
|
100
|
|
|
|
14
|
return "\e[" . ($res >= 8 ? ($res+40-8) : ($res+40)) . "m"; |
302
|
|
|
|
|
|
|
} |
303
|
|
|
|
|
|
|
|
304
|
1
|
|
|
1
|
1
|
4
|
sub ansi16bg { goto &rgb_to_ansi16_bg_code } |
305
|
|
|
|
|
|
|
|
306
|
|
|
|
|
|
|
$SPEC{rgb_to_ansi256_fg_code} = { |
307
|
|
|
|
|
|
|
v => 1.1, |
308
|
|
|
|
|
|
|
summary => 'Convert RGB to ANSI-256 color escape sequence to change foreground color', |
309
|
|
|
|
|
|
|
args => { |
310
|
|
|
|
|
|
|
color => { |
311
|
|
|
|
|
|
|
schema => 'color::rgb24*', |
312
|
|
|
|
|
|
|
req => 1, |
313
|
|
|
|
|
|
|
pos => 0, |
314
|
|
|
|
|
|
|
}, |
315
|
|
|
|
|
|
|
}, |
316
|
|
|
|
|
|
|
args_as => 'array', |
317
|
|
|
|
|
|
|
result => { |
318
|
|
|
|
|
|
|
schema => 'str*', |
319
|
|
|
|
|
|
|
}, |
320
|
|
|
|
|
|
|
result_naked => 1, |
321
|
|
|
|
|
|
|
}; |
322
|
|
|
|
|
|
|
sub rgb_to_ansi256_fg_code { |
323
|
3
|
|
|
3
|
1
|
8
|
my ($input) = @_; |
324
|
|
|
|
|
|
|
|
325
|
3
|
|
|
|
|
10
|
my $res = _rgb_to_indexed($input, \@revansi16); |
326
|
3
|
|
|
|
|
17
|
return "\e[38;5;${res}m"; |
327
|
|
|
|
|
|
|
} |
328
|
|
|
|
|
|
|
|
329
|
1
|
|
|
1
|
1
|
6
|
sub ansi256fg { goto &rgb_to_ansi256_fg_code } |
330
|
|
|
|
|
|
|
|
331
|
|
|
|
|
|
|
$SPEC{rgb_to_ansi256_bg_code} = { |
332
|
|
|
|
|
|
|
v => 1.1, |
333
|
|
|
|
|
|
|
summary => 'Convert RGB to ANSI-256 color escape sequence to change background color', |
334
|
|
|
|
|
|
|
args => { |
335
|
|
|
|
|
|
|
color => { |
336
|
|
|
|
|
|
|
schema => 'color::rgb24*', |
337
|
|
|
|
|
|
|
req => 1, |
338
|
|
|
|
|
|
|
pos => 0, |
339
|
|
|
|
|
|
|
}, |
340
|
|
|
|
|
|
|
}, |
341
|
|
|
|
|
|
|
args_as => 'array', |
342
|
|
|
|
|
|
|
result => { |
343
|
|
|
|
|
|
|
schema => 'str*', |
344
|
|
|
|
|
|
|
}, |
345
|
|
|
|
|
|
|
result_naked => 1, |
346
|
|
|
|
|
|
|
}; |
347
|
|
|
|
|
|
|
sub rgb_to_ansi256_bg_code { |
348
|
2
|
|
|
2
|
1
|
5
|
my ($input) = @_; |
349
|
|
|
|
|
|
|
|
350
|
2
|
|
|
|
|
5
|
my $res = _rgb_to_indexed($input, \@revansi16); |
351
|
2
|
|
|
|
|
11
|
return "\e[48;5;${res}m"; |
352
|
|
|
|
|
|
|
} |
353
|
|
|
|
|
|
|
|
354
|
1
|
|
|
1
|
1
|
5
|
sub ansi256bg { goto &rgb_to_ansi256_bg_code } |
355
|
|
|
|
|
|
|
|
356
|
|
|
|
|
|
|
$SPEC{rgb_to_ansi24b_fg_code} = { |
357
|
|
|
|
|
|
|
v => 1.1, |
358
|
|
|
|
|
|
|
summary => 'Convert RGB to ANSI 24bit-color escape sequence to change foreground color', |
359
|
|
|
|
|
|
|
args => { |
360
|
|
|
|
|
|
|
color => { |
361
|
|
|
|
|
|
|
schema => 'color::rgb24*', |
362
|
|
|
|
|
|
|
req => 1, |
363
|
|
|
|
|
|
|
pos => 0, |
364
|
|
|
|
|
|
|
}, |
365
|
|
|
|
|
|
|
}, |
366
|
|
|
|
|
|
|
args_as => 'array', |
367
|
|
|
|
|
|
|
result => { |
368
|
|
|
|
|
|
|
schema => 'str*', |
369
|
|
|
|
|
|
|
}, |
370
|
|
|
|
|
|
|
result_naked => 1, |
371
|
|
|
|
|
|
|
}; |
372
|
|
|
|
|
|
|
sub rgb_to_ansi24b_fg_code { |
373
|
4
|
|
|
4
|
1
|
3326
|
my ($rgb) = @_; |
374
|
|
|
|
|
|
|
|
375
|
4
|
|
|
|
|
33
|
return sprintf("\e[38;2;%d;%d;%dm", |
376
|
|
|
|
|
|
|
hex(substr($rgb, 0, 2)), |
377
|
|
|
|
|
|
|
hex(substr($rgb, 2, 2)), |
378
|
|
|
|
|
|
|
hex(substr($rgb, 4, 2))); |
379
|
|
|
|
|
|
|
} |
380
|
|
|
|
|
|
|
|
381
|
1
|
|
|
1
|
1
|
5
|
sub ansi24bfg { goto &rgb_to_ansi24b_fg_code } |
382
|
|
|
|
|
|
|
|
383
|
|
|
|
|
|
|
$SPEC{rgb_to_ansi24b_bg_code} = { |
384
|
|
|
|
|
|
|
v => 1.1, |
385
|
|
|
|
|
|
|
summary => 'Convert RGB to ANSI 24bit-color escape sequence to change background color', |
386
|
|
|
|
|
|
|
args => { |
387
|
|
|
|
|
|
|
color => { |
388
|
|
|
|
|
|
|
schema => 'color::rgb24*', |
389
|
|
|
|
|
|
|
req => 1, |
390
|
|
|
|
|
|
|
pos => 0, |
391
|
|
|
|
|
|
|
}, |
392
|
|
|
|
|
|
|
}, |
393
|
|
|
|
|
|
|
args_as => 'array', |
394
|
|
|
|
|
|
|
result => { |
395
|
|
|
|
|
|
|
schema => 'str*', |
396
|
|
|
|
|
|
|
}, |
397
|
|
|
|
|
|
|
result_naked => 1, |
398
|
|
|
|
|
|
|
}; |
399
|
|
|
|
|
|
|
sub rgb_to_ansi24b_bg_code { |
400
|
2
|
|
|
2
|
1
|
5
|
my ($rgb) = @_; |
401
|
|
|
|
|
|
|
|
402
|
2
|
|
|
|
|
14
|
return sprintf("\e[48;2;%d;%d;%dm", |
403
|
|
|
|
|
|
|
hex(substr($rgb, 0, 2)), |
404
|
|
|
|
|
|
|
hex(substr($rgb, 2, 2)), |
405
|
|
|
|
|
|
|
hex(substr($rgb, 4, 2))); |
406
|
|
|
|
|
|
|
} |
407
|
|
|
|
|
|
|
|
408
|
1
|
|
|
1
|
1
|
4
|
sub ansi24bbg { goto &rgb_to_ansi24b_bg_code } |
409
|
|
|
|
|
|
|
|
410
|
|
|
|
|
|
|
our $_use_termdetsw = 1; |
411
|
|
|
|
|
|
|
our $_color_depth; # cache, can be set during testing |
412
|
|
|
|
|
|
|
sub _color_depth { |
413
|
7
|
50
|
|
7
|
|
17
|
unless (defined $_color_depth) { |
414
|
|
|
|
|
|
|
{ |
415
|
7
|
50
|
|
|
|
9
|
if (exists $ENV{NO_COLOR}) { |
|
7
|
|
|
|
|
15
|
|
416
|
0
|
|
|
|
|
0
|
$_color_depth = 0; |
417
|
0
|
|
|
|
|
0
|
last; |
418
|
|
|
|
|
|
|
} |
419
|
7
|
100
|
66
|
|
|
19
|
if (defined $ENV{COLOR} && !$ENV{COLOR}) { |
420
|
1
|
|
|
|
|
1
|
$_color_depth = 0; |
421
|
1
|
|
|
|
|
3
|
last; |
422
|
|
|
|
|
|
|
} |
423
|
6
|
100
|
|
|
|
13
|
if (defined $ENV{COLOR_DEPTH}) { |
424
|
4
|
|
|
|
|
8
|
$_color_depth = $ENV{COLOR_DEPTH}; |
425
|
4
|
|
|
|
|
6
|
last; |
426
|
|
|
|
|
|
|
} |
427
|
2
|
50
|
|
|
|
5
|
if ($_use_termdetsw) { |
428
|
0
|
|
|
|
|
0
|
eval { require Term::Detect::Software }; |
|
0
|
|
|
|
|
0
|
|
429
|
0
|
0
|
|
|
|
0
|
if (!$@) { |
430
|
0
|
|
|
|
|
0
|
$_color_depth = Term::Detect::Software::detect_terminal_cached()->{color_depth}; |
431
|
0
|
|
|
|
|
0
|
last; |
432
|
|
|
|
|
|
|
} |
433
|
|
|
|
|
|
|
} |
434
|
|
|
|
|
|
|
# simple heuristic |
435
|
2
|
100
|
|
|
|
6
|
if ($ENV{KONSOLE_DBUS_SERVICE}) { |
436
|
1
|
|
|
|
|
2
|
$_color_depth = 2**24; |
437
|
1
|
|
|
|
|
3
|
last; |
438
|
|
|
|
|
|
|
} |
439
|
|
|
|
|
|
|
# safe value |
440
|
1
|
|
|
|
|
2
|
$_color_depth = 16; |
441
|
|
|
|
|
|
|
} |
442
|
|
|
|
|
|
|
}; |
443
|
7
|
|
|
|
|
13
|
$_color_depth; |
444
|
|
|
|
|
|
|
} |
445
|
|
|
|
|
|
|
|
446
|
|
|
|
|
|
|
$SPEC{rgb_to_ansi_fg_code} = { |
447
|
|
|
|
|
|
|
v => 1.1, |
448
|
|
|
|
|
|
|
summary => 'Convert RGB to ANSI color escape sequence to change foreground color', |
449
|
|
|
|
|
|
|
description => <<'_', |
450
|
|
|
|
|
|
|
|
451
|
|
|
|
|
|
|
Autodetect terminal capability and can return either empty string, 16-color, |
452
|
|
|
|
|
|
|
256-color, or 24bit-code. |
453
|
|
|
|
|
|
|
|
454
|
|
|
|
|
|
|
Color depth used is determined by `COLOR_DEPTH` environment setting or from |
455
|
|
|
|
|
|
|
<pm:Term::Detect::Software> if that module is available. In other words, this |
456
|
|
|
|
|
|
|
function automatically chooses rgb_to_ansi{24b,256,16}_fg_code(). |
457
|
|
|
|
|
|
|
|
458
|
|
|
|
|
|
|
_ |
459
|
|
|
|
|
|
|
args => { |
460
|
|
|
|
|
|
|
color => { |
461
|
|
|
|
|
|
|
schema => 'color::rgb24*', |
462
|
|
|
|
|
|
|
req => 1, |
463
|
|
|
|
|
|
|
pos => 0, |
464
|
|
|
|
|
|
|
}, |
465
|
|
|
|
|
|
|
}, |
466
|
|
|
|
|
|
|
args_as => 'array', |
467
|
|
|
|
|
|
|
result => { |
468
|
|
|
|
|
|
|
schema => 'str*', |
469
|
|
|
|
|
|
|
}, |
470
|
|
|
|
|
|
|
result_naked => 1, |
471
|
|
|
|
|
|
|
}; |
472
|
|
|
|
|
|
|
sub rgb_to_ansi_fg_code { |
473
|
7
|
|
|
7
|
1
|
16
|
my ($rgb) = @_; |
474
|
7
|
|
|
|
|
16
|
my $cd = _color_depth(); |
475
|
7
|
100
|
|
|
|
23
|
if ($cd >= 2**24) { |
|
|
100
|
|
|
|
|
|
|
|
100
|
|
|
|
|
|
476
|
2
|
|
|
|
|
7
|
rgb_to_ansi24b_fg_code($rgb); |
477
|
|
|
|
|
|
|
} elsif ($cd >= 256) { |
478
|
1
|
|
|
|
|
5
|
rgb_to_ansi256_fg_code($rgb); |
479
|
|
|
|
|
|
|
} elsif ($cd >= 16) { |
480
|
2
|
|
|
|
|
7
|
rgb_to_ansi16_fg_code($rgb); |
481
|
|
|
|
|
|
|
} else { |
482
|
2
|
|
|
|
|
8
|
""; |
483
|
|
|
|
|
|
|
} |
484
|
|
|
|
|
|
|
} |
485
|
|
|
|
|
|
|
|
486
|
7
|
|
|
7
|
1
|
6254
|
sub ansifg { goto &rgb_to_ansi_fg_code } |
487
|
|
|
|
|
|
|
|
488
|
|
|
|
|
|
|
$SPEC{rgb_to_ansi_bg_code} = { |
489
|
|
|
|
|
|
|
v => 1.1, |
490
|
|
|
|
|
|
|
summary => 'Convert RGB to ANSI color escape sequence to change background color', |
491
|
|
|
|
|
|
|
description => <<'_', |
492
|
|
|
|
|
|
|
|
493
|
|
|
|
|
|
|
Autodetect terminal capability and can return either empty string, 16-color, |
494
|
|
|
|
|
|
|
256-color, or 24bit-code. |
495
|
|
|
|
|
|
|
|
496
|
|
|
|
|
|
|
Which color depth used is determined by `COLOR_DEPTH` environment setting or |
497
|
|
|
|
|
|
|
from <pm:Term::Detect::Software> if that module is available). In other words, |
498
|
|
|
|
|
|
|
this function automatically chooses rgb_to_ansi{24b,256,16}_bg_code(). |
499
|
|
|
|
|
|
|
|
500
|
|
|
|
|
|
|
_ |
501
|
|
|
|
|
|
|
args => { |
502
|
|
|
|
|
|
|
color => { |
503
|
|
|
|
|
|
|
schema => 'color::rgb24*', |
504
|
|
|
|
|
|
|
req => 1, |
505
|
|
|
|
|
|
|
pos => 0, |
506
|
|
|
|
|
|
|
}, |
507
|
|
|
|
|
|
|
}, |
508
|
|
|
|
|
|
|
args_as => 'array', |
509
|
|
|
|
|
|
|
result => { |
510
|
|
|
|
|
|
|
schema => 'str*', |
511
|
|
|
|
|
|
|
}, |
512
|
|
|
|
|
|
|
result_naked => 1, |
513
|
|
|
|
|
|
|
}; |
514
|
|
|
|
|
|
|
sub rgb_to_ansi_bg_code { |
515
|
0
|
|
|
0
|
1
|
|
my ($rgb) = @_; |
516
|
0
|
|
|
|
|
|
my $cd = _color_depth(); |
517
|
0
|
0
|
|
|
|
|
if ($cd >= 2**24) { |
|
|
0
|
|
|
|
|
|
518
|
0
|
|
|
|
|
|
rgb_to_ansi24b_bg_code($rgb); |
519
|
|
|
|
|
|
|
} elsif ($cd >= 256) { |
520
|
0
|
|
|
|
|
|
rgb_to_ansi256_bg_code($rgb); |
521
|
|
|
|
|
|
|
} else { |
522
|
0
|
|
|
|
|
|
rgb_to_ansi16_bg_code($rgb); |
523
|
|
|
|
|
|
|
} |
524
|
|
|
|
|
|
|
} |
525
|
|
|
|
|
|
|
|
526
|
0
|
|
|
0
|
1
|
|
sub ansibg { goto &rgb_to_ansi_bg_code } |
527
|
|
|
|
|
|
|
|
528
|
0
|
|
|
0
|
1
|
|
sub ansi_reset { "\e[0m" } |
529
|
|
|
|
|
|
|
|
530
|
|
|
|
|
|
|
1; |
531
|
|
|
|
|
|
|
# ABSTRACT: Routines for dealing with ANSI colors |
532
|
|
|
|
|
|
|
|
533
|
|
|
|
|
|
|
__END__ |
534
|
|
|
|
|
|
|
|
535
|
|
|
|
|
|
|
=pod |
536
|
|
|
|
|
|
|
|
537
|
|
|
|
|
|
|
=encoding UTF-8 |
538
|
|
|
|
|
|
|
|
539
|
|
|
|
|
|
|
=head1 NAME |
540
|
|
|
|
|
|
|
|
541
|
|
|
|
|
|
|
Color::ANSI::Util - Routines for dealing with ANSI colors |
542
|
|
|
|
|
|
|
|
543
|
|
|
|
|
|
|
=head1 VERSION |
544
|
|
|
|
|
|
|
|
545
|
|
|
|
|
|
|
This document describes version 0.163 of Color::ANSI::Util (from Perl distribution Color-ANSI-Util), released on 2019-08-20. |
546
|
|
|
|
|
|
|
|
547
|
|
|
|
|
|
|
=head1 SYNOPSIS |
548
|
|
|
|
|
|
|
|
549
|
|
|
|
|
|
|
use Color::ANSI::Util qw( |
550
|
|
|
|
|
|
|
ansifg |
551
|
|
|
|
|
|
|
ansibg |
552
|
|
|
|
|
|
|
); |
553
|
|
|
|
|
|
|
|
554
|
|
|
|
|
|
|
say ansifg("f0c010"); # => "\e[33;1m" (on 16-color terminal) |
555
|
|
|
|
|
|
|
# => "\e[38;5;11m" (on 256-color terminal) |
556
|
|
|
|
|
|
|
# => "\e[38;2;240;192;16m" (on 24-bit-color terminal) |
557
|
|
|
|
|
|
|
|
558
|
|
|
|
|
|
|
say ansibg("ff5f87"); # => "\e[47m" (on 16-color terminal) |
559
|
|
|
|
|
|
|
# => "\e[48;5;7m" (on 256-color terminal) |
560
|
|
|
|
|
|
|
# => "\e[48;2;255;95;135m" (on 24-bit-color terminal) |
561
|
|
|
|
|
|
|
|
562
|
|
|
|
|
|
|
There are a bunch of other exportable functions too, mostly for converting |
563
|
|
|
|
|
|
|
between RGB and ANSI color (16/256/24bit color depth). |
564
|
|
|
|
|
|
|
|
565
|
|
|
|
|
|
|
=head1 DESCRIPTION |
566
|
|
|
|
|
|
|
|
567
|
|
|
|
|
|
|
This module provides routines for dealing with ANSI colors. The two main |
568
|
|
|
|
|
|
|
functions are C<ansifg> and C<ansibg>. With those functions, you can specify |
569
|
|
|
|
|
|
|
colors in RGB and let it output the correct ANSI color escape code according to |
570
|
|
|
|
|
|
|
the color depth support of the terminal (whether 16-color, 256-color, or 24bit). |
571
|
|
|
|
|
|
|
There are other functions to convert RGB to ANSI in specific color depths, or |
572
|
|
|
|
|
|
|
reverse functions to convert from ANSI to RGB codes. |
573
|
|
|
|
|
|
|
|
574
|
|
|
|
|
|
|
Keywords: xterm, xterm-256color, terminal |
575
|
|
|
|
|
|
|
|
576
|
|
|
|
|
|
|
=head1 BUGS/NOTES |
577
|
|
|
|
|
|
|
|
578
|
|
|
|
|
|
|
Algorithm for finding closest indexed color from RGB color currently not very |
579
|
|
|
|
|
|
|
efficient. Probably can add some threshold square distance, below which we can |
580
|
|
|
|
|
|
|
shortcut to the final answer. |
581
|
|
|
|
|
|
|
|
582
|
|
|
|
|
|
|
=head1 FUNCTIONS |
583
|
|
|
|
|
|
|
|
584
|
|
|
|
|
|
|
|
585
|
|
|
|
|
|
|
=head2 ansi16_to_rgb |
586
|
|
|
|
|
|
|
|
587
|
|
|
|
|
|
|
Usage: |
588
|
|
|
|
|
|
|
|
589
|
|
|
|
|
|
|
ansi16_to_rgb($color) -> color::rgb24 |
590
|
|
|
|
|
|
|
|
591
|
|
|
|
|
|
|
Convert ANSI-16 color to RGB. |
592
|
|
|
|
|
|
|
|
593
|
|
|
|
|
|
|
Returns 6-hexdigit, e.g. 'ff00cc'. |
594
|
|
|
|
|
|
|
|
595
|
|
|
|
|
|
|
This function is not exported by default, but exportable. |
596
|
|
|
|
|
|
|
|
597
|
|
|
|
|
|
|
Arguments ('*' denotes required arguments): |
598
|
|
|
|
|
|
|
|
599
|
|
|
|
|
|
|
=over 4 |
600
|
|
|
|
|
|
|
|
601
|
|
|
|
|
|
|
=item * B<$color>* => I<color::ansi16> |
602
|
|
|
|
|
|
|
|
603
|
|
|
|
|
|
|
=back |
604
|
|
|
|
|
|
|
|
605
|
|
|
|
|
|
|
Return value: (color::rgb24) |
606
|
|
|
|
|
|
|
|
607
|
|
|
|
|
|
|
|
608
|
|
|
|
|
|
|
|
609
|
|
|
|
|
|
|
=head2 ansi256_to_rgb |
610
|
|
|
|
|
|
|
|
611
|
|
|
|
|
|
|
Usage: |
612
|
|
|
|
|
|
|
|
613
|
|
|
|
|
|
|
ansi256_to_rgb($color) -> color::rgb24 |
614
|
|
|
|
|
|
|
|
615
|
|
|
|
|
|
|
Convert ANSI-256 color to RGB. |
616
|
|
|
|
|
|
|
|
617
|
|
|
|
|
|
|
This function is not exported by default, but exportable. |
618
|
|
|
|
|
|
|
|
619
|
|
|
|
|
|
|
Arguments ('*' denotes required arguments): |
620
|
|
|
|
|
|
|
|
621
|
|
|
|
|
|
|
=over 4 |
622
|
|
|
|
|
|
|
|
623
|
|
|
|
|
|
|
=item * B<$color>* => I<color::ansi256> |
624
|
|
|
|
|
|
|
|
625
|
|
|
|
|
|
|
=back |
626
|
|
|
|
|
|
|
|
627
|
|
|
|
|
|
|
Return value: (color::rgb24) |
628
|
|
|
|
|
|
|
|
629
|
|
|
|
|
|
|
|
630
|
|
|
|
|
|
|
|
631
|
|
|
|
|
|
|
=head2 rgb_to_ansi16 |
632
|
|
|
|
|
|
|
|
633
|
|
|
|
|
|
|
Usage: |
634
|
|
|
|
|
|
|
|
635
|
|
|
|
|
|
|
rgb_to_ansi16($color) -> color::ansi16 |
636
|
|
|
|
|
|
|
|
637
|
|
|
|
|
|
|
Convert RGB to ANSI-16 color. |
638
|
|
|
|
|
|
|
|
639
|
|
|
|
|
|
|
This function is not exported by default, but exportable. |
640
|
|
|
|
|
|
|
|
641
|
|
|
|
|
|
|
Arguments ('*' denotes required arguments): |
642
|
|
|
|
|
|
|
|
643
|
|
|
|
|
|
|
=over 4 |
644
|
|
|
|
|
|
|
|
645
|
|
|
|
|
|
|
=item * B<$color>* => I<color::rgb24> |
646
|
|
|
|
|
|
|
|
647
|
|
|
|
|
|
|
=back |
648
|
|
|
|
|
|
|
|
649
|
|
|
|
|
|
|
Return value: (color::ansi16) |
650
|
|
|
|
|
|
|
|
651
|
|
|
|
|
|
|
|
652
|
|
|
|
|
|
|
|
653
|
|
|
|
|
|
|
=head2 rgb_to_ansi16_bg_code |
654
|
|
|
|
|
|
|
|
655
|
|
|
|
|
|
|
Usage: |
656
|
|
|
|
|
|
|
|
657
|
|
|
|
|
|
|
rgb_to_ansi16_bg_code($color) -> str |
658
|
|
|
|
|
|
|
|
659
|
|
|
|
|
|
|
Convert RGB to ANSI-16 color escape sequence to change background color. |
660
|
|
|
|
|
|
|
|
661
|
|
|
|
|
|
|
This function is not exported by default, but exportable. |
662
|
|
|
|
|
|
|
|
663
|
|
|
|
|
|
|
Arguments ('*' denotes required arguments): |
664
|
|
|
|
|
|
|
|
665
|
|
|
|
|
|
|
=over 4 |
666
|
|
|
|
|
|
|
|
667
|
|
|
|
|
|
|
=item * B<$color>* => I<color::rgb24> |
668
|
|
|
|
|
|
|
|
669
|
|
|
|
|
|
|
=back |
670
|
|
|
|
|
|
|
|
671
|
|
|
|
|
|
|
Return value: (str) |
672
|
|
|
|
|
|
|
|
673
|
|
|
|
|
|
|
|
674
|
|
|
|
|
|
|
|
675
|
|
|
|
|
|
|
=head2 rgb_to_ansi16_fg_code |
676
|
|
|
|
|
|
|
|
677
|
|
|
|
|
|
|
Usage: |
678
|
|
|
|
|
|
|
|
679
|
|
|
|
|
|
|
rgb_to_ansi16_fg_code($color) -> str |
680
|
|
|
|
|
|
|
|
681
|
|
|
|
|
|
|
Convert RGB to ANSI-16 color escape sequence to change foreground color. |
682
|
|
|
|
|
|
|
|
683
|
|
|
|
|
|
|
This function is not exported by default, but exportable. |
684
|
|
|
|
|
|
|
|
685
|
|
|
|
|
|
|
Arguments ('*' denotes required arguments): |
686
|
|
|
|
|
|
|
|
687
|
|
|
|
|
|
|
=over 4 |
688
|
|
|
|
|
|
|
|
689
|
|
|
|
|
|
|
=item * B<$color>* => I<color::rgb24> |
690
|
|
|
|
|
|
|
|
691
|
|
|
|
|
|
|
=back |
692
|
|
|
|
|
|
|
|
693
|
|
|
|
|
|
|
Return value: (str) |
694
|
|
|
|
|
|
|
|
695
|
|
|
|
|
|
|
|
696
|
|
|
|
|
|
|
|
697
|
|
|
|
|
|
|
=head2 rgb_to_ansi24b_bg_code |
698
|
|
|
|
|
|
|
|
699
|
|
|
|
|
|
|
Usage: |
700
|
|
|
|
|
|
|
|
701
|
|
|
|
|
|
|
rgb_to_ansi24b_bg_code($color) -> str |
702
|
|
|
|
|
|
|
|
703
|
|
|
|
|
|
|
Convert RGB to ANSI 24bit-color escape sequence to change background color. |
704
|
|
|
|
|
|
|
|
705
|
|
|
|
|
|
|
This function is not exported by default, but exportable. |
706
|
|
|
|
|
|
|
|
707
|
|
|
|
|
|
|
Arguments ('*' denotes required arguments): |
708
|
|
|
|
|
|
|
|
709
|
|
|
|
|
|
|
=over 4 |
710
|
|
|
|
|
|
|
|
711
|
|
|
|
|
|
|
=item * B<$color>* => I<color::rgb24> |
712
|
|
|
|
|
|
|
|
713
|
|
|
|
|
|
|
=back |
714
|
|
|
|
|
|
|
|
715
|
|
|
|
|
|
|
Return value: (str) |
716
|
|
|
|
|
|
|
|
717
|
|
|
|
|
|
|
|
718
|
|
|
|
|
|
|
|
719
|
|
|
|
|
|
|
=head2 rgb_to_ansi24b_fg_code |
720
|
|
|
|
|
|
|
|
721
|
|
|
|
|
|
|
Usage: |
722
|
|
|
|
|
|
|
|
723
|
|
|
|
|
|
|
rgb_to_ansi24b_fg_code($color) -> str |
724
|
|
|
|
|
|
|
|
725
|
|
|
|
|
|
|
Convert RGB to ANSI 24bit-color escape sequence to change foreground color. |
726
|
|
|
|
|
|
|
|
727
|
|
|
|
|
|
|
This function is not exported by default, but exportable. |
728
|
|
|
|
|
|
|
|
729
|
|
|
|
|
|
|
Arguments ('*' denotes required arguments): |
730
|
|
|
|
|
|
|
|
731
|
|
|
|
|
|
|
=over 4 |
732
|
|
|
|
|
|
|
|
733
|
|
|
|
|
|
|
=item * B<$color>* => I<color::rgb24> |
734
|
|
|
|
|
|
|
|
735
|
|
|
|
|
|
|
=back |
736
|
|
|
|
|
|
|
|
737
|
|
|
|
|
|
|
Return value: (str) |
738
|
|
|
|
|
|
|
|
739
|
|
|
|
|
|
|
|
740
|
|
|
|
|
|
|
|
741
|
|
|
|
|
|
|
=head2 rgb_to_ansi256 |
742
|
|
|
|
|
|
|
|
743
|
|
|
|
|
|
|
Usage: |
744
|
|
|
|
|
|
|
|
745
|
|
|
|
|
|
|
rgb_to_ansi256($color) -> color::ansi256 |
746
|
|
|
|
|
|
|
|
747
|
|
|
|
|
|
|
Convert RGB to ANSI-256 color. |
748
|
|
|
|
|
|
|
|
749
|
|
|
|
|
|
|
This function is not exported by default, but exportable. |
750
|
|
|
|
|
|
|
|
751
|
|
|
|
|
|
|
Arguments ('*' denotes required arguments): |
752
|
|
|
|
|
|
|
|
753
|
|
|
|
|
|
|
=over 4 |
754
|
|
|
|
|
|
|
|
755
|
|
|
|
|
|
|
=item * B<$color>* => I<color::rgb24> |
756
|
|
|
|
|
|
|
|
757
|
|
|
|
|
|
|
=back |
758
|
|
|
|
|
|
|
|
759
|
|
|
|
|
|
|
Return value: (color::ansi256) |
760
|
|
|
|
|
|
|
|
761
|
|
|
|
|
|
|
|
762
|
|
|
|
|
|
|
|
763
|
|
|
|
|
|
|
=head2 rgb_to_ansi256_bg_code |
764
|
|
|
|
|
|
|
|
765
|
|
|
|
|
|
|
Usage: |
766
|
|
|
|
|
|
|
|
767
|
|
|
|
|
|
|
rgb_to_ansi256_bg_code($color) -> str |
768
|
|
|
|
|
|
|
|
769
|
|
|
|
|
|
|
Convert RGB to ANSI-256 color escape sequence to change background color. |
770
|
|
|
|
|
|
|
|
771
|
|
|
|
|
|
|
This function is not exported by default, but exportable. |
772
|
|
|
|
|
|
|
|
773
|
|
|
|
|
|
|
Arguments ('*' denotes required arguments): |
774
|
|
|
|
|
|
|
|
775
|
|
|
|
|
|
|
=over 4 |
776
|
|
|
|
|
|
|
|
777
|
|
|
|
|
|
|
=item * B<$color>* => I<color::rgb24> |
778
|
|
|
|
|
|
|
|
779
|
|
|
|
|
|
|
=back |
780
|
|
|
|
|
|
|
|
781
|
|
|
|
|
|
|
Return value: (str) |
782
|
|
|
|
|
|
|
|
783
|
|
|
|
|
|
|
|
784
|
|
|
|
|
|
|
|
785
|
|
|
|
|
|
|
=head2 rgb_to_ansi256_fg_code |
786
|
|
|
|
|
|
|
|
787
|
|
|
|
|
|
|
Usage: |
788
|
|
|
|
|
|
|
|
789
|
|
|
|
|
|
|
rgb_to_ansi256_fg_code($color) -> str |
790
|
|
|
|
|
|
|
|
791
|
|
|
|
|
|
|
Convert RGB to ANSI-256 color escape sequence to change foreground color. |
792
|
|
|
|
|
|
|
|
793
|
|
|
|
|
|
|
This function is not exported by default, but exportable. |
794
|
|
|
|
|
|
|
|
795
|
|
|
|
|
|
|
Arguments ('*' denotes required arguments): |
796
|
|
|
|
|
|
|
|
797
|
|
|
|
|
|
|
=over 4 |
798
|
|
|
|
|
|
|
|
799
|
|
|
|
|
|
|
=item * B<$color>* => I<color::rgb24> |
800
|
|
|
|
|
|
|
|
801
|
|
|
|
|
|
|
=back |
802
|
|
|
|
|
|
|
|
803
|
|
|
|
|
|
|
Return value: (str) |
804
|
|
|
|
|
|
|
|
805
|
|
|
|
|
|
|
|
806
|
|
|
|
|
|
|
|
807
|
|
|
|
|
|
|
=head2 rgb_to_ansi_bg_code |
808
|
|
|
|
|
|
|
|
809
|
|
|
|
|
|
|
Usage: |
810
|
|
|
|
|
|
|
|
811
|
|
|
|
|
|
|
rgb_to_ansi_bg_code($color) -> str |
812
|
|
|
|
|
|
|
|
813
|
|
|
|
|
|
|
Convert RGB to ANSI color escape sequence to change background color. |
814
|
|
|
|
|
|
|
|
815
|
|
|
|
|
|
|
Autodetect terminal capability and can return either empty string, 16-color, |
816
|
|
|
|
|
|
|
256-color, or 24bit-code. |
817
|
|
|
|
|
|
|
|
818
|
|
|
|
|
|
|
Which color depth used is determined by C<COLOR_DEPTH> environment setting or |
819
|
|
|
|
|
|
|
from L<Term::Detect::Software> if that module is available). In other words, |
820
|
|
|
|
|
|
|
this function automatically chooses rgb_to_ansi{24b,256,16}I<bg>code(). |
821
|
|
|
|
|
|
|
|
822
|
|
|
|
|
|
|
This function is not exported by default, but exportable. |
823
|
|
|
|
|
|
|
|
824
|
|
|
|
|
|
|
Arguments ('*' denotes required arguments): |
825
|
|
|
|
|
|
|
|
826
|
|
|
|
|
|
|
=over 4 |
827
|
|
|
|
|
|
|
|
828
|
|
|
|
|
|
|
=item * B<$color>* => I<color::rgb24> |
829
|
|
|
|
|
|
|
|
830
|
|
|
|
|
|
|
=back |
831
|
|
|
|
|
|
|
|
832
|
|
|
|
|
|
|
Return value: (str) |
833
|
|
|
|
|
|
|
|
834
|
|
|
|
|
|
|
|
835
|
|
|
|
|
|
|
|
836
|
|
|
|
|
|
|
=head2 rgb_to_ansi_fg_code |
837
|
|
|
|
|
|
|
|
838
|
|
|
|
|
|
|
Usage: |
839
|
|
|
|
|
|
|
|
840
|
|
|
|
|
|
|
rgb_to_ansi_fg_code($color) -> str |
841
|
|
|
|
|
|
|
|
842
|
|
|
|
|
|
|
Convert RGB to ANSI color escape sequence to change foreground color. |
843
|
|
|
|
|
|
|
|
844
|
|
|
|
|
|
|
Autodetect terminal capability and can return either empty string, 16-color, |
845
|
|
|
|
|
|
|
256-color, or 24bit-code. |
846
|
|
|
|
|
|
|
|
847
|
|
|
|
|
|
|
Color depth used is determined by C<COLOR_DEPTH> environment setting or from |
848
|
|
|
|
|
|
|
L<Term::Detect::Software> if that module is available. In other words, this |
849
|
|
|
|
|
|
|
function automatically chooses rgb_to_ansi{24b,256,16}I<fg>code(). |
850
|
|
|
|
|
|
|
|
851
|
|
|
|
|
|
|
This function is not exported by default, but exportable. |
852
|
|
|
|
|
|
|
|
853
|
|
|
|
|
|
|
Arguments ('*' denotes required arguments): |
854
|
|
|
|
|
|
|
|
855
|
|
|
|
|
|
|
=over 4 |
856
|
|
|
|
|
|
|
|
857
|
|
|
|
|
|
|
=item * B<$color>* => I<color::rgb24> |
858
|
|
|
|
|
|
|
|
859
|
|
|
|
|
|
|
=back |
860
|
|
|
|
|
|
|
|
861
|
|
|
|
|
|
|
Return value: (str) |
862
|
|
|
|
|
|
|
|
863
|
|
|
|
|
|
|
|
864
|
|
|
|
|
|
|
=head2 ansi16fg($rgb) => STR |
865
|
|
|
|
|
|
|
|
866
|
|
|
|
|
|
|
Alias for rgb_to_ansi16_fg_code(). |
867
|
|
|
|
|
|
|
|
868
|
|
|
|
|
|
|
=head2 ansi16bg($rgb) => STR |
869
|
|
|
|
|
|
|
|
870
|
|
|
|
|
|
|
Alias for rgb_to_ansi16_bg_code(). |
871
|
|
|
|
|
|
|
|
872
|
|
|
|
|
|
|
=head2 ansi256fg($rgb) => STR |
873
|
|
|
|
|
|
|
|
874
|
|
|
|
|
|
|
Alias for rgb_to_ansi256_fg_code(). |
875
|
|
|
|
|
|
|
|
876
|
|
|
|
|
|
|
=head2 ansi256bg($rgb) => STR |
877
|
|
|
|
|
|
|
|
878
|
|
|
|
|
|
|
Alias for rgb_to_ansi256_bg_code(). |
879
|
|
|
|
|
|
|
|
880
|
|
|
|
|
|
|
=head2 ansi24bfg($rgb) => STR |
881
|
|
|
|
|
|
|
|
882
|
|
|
|
|
|
|
Alias for rgb_to_ansi24b_fg_code(). |
883
|
|
|
|
|
|
|
|
884
|
|
|
|
|
|
|
=head2 ansi24bbg($rgb) => STR |
885
|
|
|
|
|
|
|
|
886
|
|
|
|
|
|
|
Alias for rgb_to_ansi24b_bg_code(). |
887
|
|
|
|
|
|
|
|
888
|
|
|
|
|
|
|
=head2 rgb_to_ansi_fg_code($rgb) => STR |
889
|
|
|
|
|
|
|
|
890
|
|
|
|
|
|
|
=head2 ansifg($rgb) => STR |
891
|
|
|
|
|
|
|
|
892
|
|
|
|
|
|
|
Alias for rgb_to_ansi_fg_code(). |
893
|
|
|
|
|
|
|
|
894
|
|
|
|
|
|
|
=head2 ansibg($rgb) => STR |
895
|
|
|
|
|
|
|
|
896
|
|
|
|
|
|
|
Alias for rgb_to_ansi_bg_code(). |
897
|
|
|
|
|
|
|
|
898
|
|
|
|
|
|
|
=head2 ansi_reset |
899
|
|
|
|
|
|
|
|
900
|
|
|
|
|
|
|
Returns "\e[0m", which is the ANSI escape sequence to reset color. Normally you |
901
|
|
|
|
|
|
|
print this sequence after you print colored text. |
902
|
|
|
|
|
|
|
|
903
|
|
|
|
|
|
|
=head1 ENVIRONMENT |
904
|
|
|
|
|
|
|
|
905
|
|
|
|
|
|
|
=head2 NO_COLOR |
906
|
|
|
|
|
|
|
|
907
|
|
|
|
|
|
|
Can be used to explicitly disable color. See L<https://no-color.org> for more |
908
|
|
|
|
|
|
|
details. |
909
|
|
|
|
|
|
|
|
910
|
|
|
|
|
|
|
Observed by: ansi{fg,bg}. |
911
|
|
|
|
|
|
|
|
912
|
|
|
|
|
|
|
=head2 COLOR => bool |
913
|
|
|
|
|
|
|
|
914
|
|
|
|
|
|
|
Can be used to explicitly disable color by setting it to 0. |
915
|
|
|
|
|
|
|
|
916
|
|
|
|
|
|
|
Observed by: ansi{fg,bg}. |
917
|
|
|
|
|
|
|
|
918
|
|
|
|
|
|
|
=head2 COLOR_DEPTH => INT |
919
|
|
|
|
|
|
|
|
920
|
|
|
|
|
|
|
Can be used to explicitly set color depth instead of trying to detect |
921
|
|
|
|
|
|
|
appropriate color depth. |
922
|
|
|
|
|
|
|
|
923
|
|
|
|
|
|
|
Observed by: ansi{fg,bg}. |
924
|
|
|
|
|
|
|
|
925
|
|
|
|
|
|
|
=head1 HOMEPAGE |
926
|
|
|
|
|
|
|
|
927
|
|
|
|
|
|
|
Please visit the project's homepage at L<https://metacpan.org/release/Color-ANSI-Util>. |
928
|
|
|
|
|
|
|
|
929
|
|
|
|
|
|
|
=head1 SOURCE |
930
|
|
|
|
|
|
|
|
931
|
|
|
|
|
|
|
Source repository is at L<https://github.com/perlancar/perl-Color-ANSI-Util>. |
932
|
|
|
|
|
|
|
|
933
|
|
|
|
|
|
|
=head1 BUGS |
934
|
|
|
|
|
|
|
|
935
|
|
|
|
|
|
|
Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=Color-ANSI-Util> |
936
|
|
|
|
|
|
|
|
937
|
|
|
|
|
|
|
When submitting a bug or request, please include a test-file or a |
938
|
|
|
|
|
|
|
patch to an existing test-file that illustrates the bug or desired |
939
|
|
|
|
|
|
|
feature. |
940
|
|
|
|
|
|
|
|
941
|
|
|
|
|
|
|
=head1 SEE ALSO |
942
|
|
|
|
|
|
|
|
943
|
|
|
|
|
|
|
L<Term::ANSIColor> |
944
|
|
|
|
|
|
|
|
945
|
|
|
|
|
|
|
L<http://en.wikipedia.org/wiki/ANSI_escape_code> |
946
|
|
|
|
|
|
|
|
947
|
|
|
|
|
|
|
=head1 AUTHOR |
948
|
|
|
|
|
|
|
|
949
|
|
|
|
|
|
|
perlancar <perlancar@cpan.org> |
950
|
|
|
|
|
|
|
|
951
|
|
|
|
|
|
|
=head1 COPYRIGHT AND LICENSE |
952
|
|
|
|
|
|
|
|
953
|
|
|
|
|
|
|
This software is copyright (c) 2019, 2018, 2017, 2016, 2015, 2014, 2013 by perlancar@cpan.org. |
954
|
|
|
|
|
|
|
|
955
|
|
|
|
|
|
|
This is free software; you can redistribute it and/or modify it under |
956
|
|
|
|
|
|
|
the same terms as the Perl 5 programming language system itself. |
957
|
|
|
|
|
|
|
|
958
|
|
|
|
|
|
|
=cut |