| 1 |
NAME |
| 2 |
Color::ANSI::Util - Routines for dealing with ANSI colors |
| 3 |
|
| 4 |
VERSION |
| 5 |
This document describes version 0.165 of Color::ANSI::Util (from Perl |
| 6 |
distribution Color-ANSI-Util), released on 2024-02-23. |
| 7 |
|
| 8 |
SYNOPSIS |
| 9 |
use Color::ANSI::Util qw( |
| 10 |
ansifg |
| 11 |
ansibg |
| 12 |
); |
| 13 |
|
| 14 |
say ansifg("f0c010"); # => "\e[33;1m" (on 16-color terminal) |
| 15 |
# => "\e[38;5;11m" (on 256-color terminal) |
| 16 |
# => "\e[38;2;240;192;16m" (on 24-bit-color terminal) |
| 17 |
|
| 18 |
say ansibg("ff5f87"); # => "\e[47m" (on 16-color terminal) |
| 19 |
# => "\e[48;5;7m" (on 256-color terminal) |
| 20 |
# => "\e[48;2;255;95;135m" (on 24-bit-color terminal) |
| 21 |
|
| 22 |
There are a bunch of other exportable functions too, mostly for |
| 23 |
converting between RGB and ANSI color (16/256/24bit color depth). |
| 24 |
|
| 25 |
DESCRIPTION |
| 26 |
This module provides routines for dealing with ANSI colors. The two main |
| 27 |
functions are "ansifg" and "ansibg". With those functions, you can |
| 28 |
specify colors in RGB and let it output the correct ANSI color escape |
| 29 |
code according to the color depth support of the terminal (whether |
| 30 |
16-color, 256-color, or 24bit). There are other functions to convert RGB |
| 31 |
to ANSI in specific color depths, or reverse functions to convert from |
| 32 |
ANSI to RGB codes. |
| 33 |
|
| 34 |
Keywords: xterm, xterm-256color, terminal |
| 35 |
|
| 36 |
BUGS/NOTES |
| 37 |
Algorithm for finding closest indexed color from RGB color currently not |
| 38 |
very efficient. Probably can add some threshold square distance, below |
| 39 |
which we can shortcut to the final answer. |
| 40 |
|
| 41 |
FUNCTIONS |
| 42 |
ansi16_to_rgb |
| 43 |
Usage: |
| 44 |
|
| 45 |
ansi16_to_rgb($color) -> color::rgb24 |
| 46 |
|
| 47 |
Convert ANSI-16 color to RGB. |
| 48 |
|
| 49 |
Returns 6-hexdigit, e.g. 'ff00cc'. |
| 50 |
|
| 51 |
This function is not exported by default, but exportable. |
| 52 |
|
| 53 |
Arguments ('*' denotes required arguments): |
| 54 |
|
| 55 |
* $color* => *color::ansi16* |
| 56 |
|
| 57 |
(No description) |
| 58 |
|
| 59 |
Return value: (color::rgb24) |
| 60 |
|
| 61 |
ansi256_to_rgb |
| 62 |
Usage: |
| 63 |
|
| 64 |
ansi256_to_rgb($color) -> color::rgb24 |
| 65 |
|
| 66 |
Convert ANSI-256 color to RGB. |
| 67 |
|
| 68 |
This function is not exported by default, but exportable. |
| 69 |
|
| 70 |
Arguments ('*' denotes required arguments): |
| 71 |
|
| 72 |
* $color* => *color::ansi256* |
| 73 |
|
| 74 |
(No description) |
| 75 |
|
| 76 |
Return value: (color::rgb24) |
| 77 |
|
| 78 |
rgb_to_ansi16 |
| 79 |
Usage: |
| 80 |
|
| 81 |
rgb_to_ansi16($color) -> color::ansi16 |
| 82 |
|
| 83 |
Convert RGB to ANSI-16 color. |
| 84 |
|
| 85 |
This function is not exported by default, but exportable. |
| 86 |
|
| 87 |
Arguments ('*' denotes required arguments): |
| 88 |
|
| 89 |
* $color* => *color::rgb24* |
| 90 |
|
| 91 |
(No description) |
| 92 |
|
| 93 |
Return value: (color::ansi16) |
| 94 |
|
| 95 |
rgb_to_ansi16_bg_code |
| 96 |
Usage: |
| 97 |
|
| 98 |
rgb_to_ansi16_bg_code($color) -> str |
| 99 |
|
| 100 |
Convert RGB to ANSI-16 color escape sequence to change background color. |
| 101 |
|
| 102 |
This function is not exported by default, but exportable. |
| 103 |
|
| 104 |
Arguments ('*' denotes required arguments): |
| 105 |
|
| 106 |
* $color* => *color::rgb24* |
| 107 |
|
| 108 |
(No description) |
| 109 |
|
| 110 |
Return value: (str) |
| 111 |
|
| 112 |
rgb_to_ansi16_fg_code |
| 113 |
Usage: |
| 114 |
|
| 115 |
rgb_to_ansi16_fg_code($color) -> str |
| 116 |
|
| 117 |
Convert RGB to ANSI-16 color escape sequence to change foreground color. |
| 118 |
|
| 119 |
This function is not exported by default, but exportable. |
| 120 |
|
| 121 |
Arguments ('*' denotes required arguments): |
| 122 |
|
| 123 |
* $color* => *color::rgb24* |
| 124 |
|
| 125 |
(No description) |
| 126 |
|
| 127 |
Return value: (str) |
| 128 |
|
| 129 |
rgb_to_ansi24b_bg_code |
| 130 |
Usage: |
| 131 |
|
| 132 |
rgb_to_ansi24b_bg_code($color) -> str |
| 133 |
|
| 134 |
Convert RGB to ANSI 24bit-color escape sequence to change background |
| 135 |
color. |
| 136 |
|
| 137 |
This function is not exported by default, but exportable. |
| 138 |
|
| 139 |
Arguments ('*' denotes required arguments): |
| 140 |
|
| 141 |
* $color* => *color::rgb24* |
| 142 |
|
| 143 |
(No description) |
| 144 |
|
| 145 |
Return value: (str) |
| 146 |
|
| 147 |
rgb_to_ansi24b_fg_code |
| 148 |
Usage: |
| 149 |
|
| 150 |
rgb_to_ansi24b_fg_code($color) -> str |
| 151 |
|
| 152 |
Convert RGB to ANSI 24bit-color escape sequence to change foreground |
| 153 |
color. |
| 154 |
|
| 155 |
This function is not exported by default, but exportable. |
| 156 |
|
| 157 |
Arguments ('*' denotes required arguments): |
| 158 |
|
| 159 |
* $color* => *color::rgb24* |
| 160 |
|
| 161 |
(No description) |
| 162 |
|
| 163 |
Return value: (str) |
| 164 |
|
| 165 |
rgb_to_ansi256 |
| 166 |
Usage: |
| 167 |
|
| 168 |
rgb_to_ansi256($color) -> color::ansi256 |
| 169 |
|
| 170 |
Convert RGB to ANSI-256 color. |
| 171 |
|
| 172 |
This function is not exported by default, but exportable. |
| 173 |
|
| 174 |
Arguments ('*' denotes required arguments): |
| 175 |
|
| 176 |
* $color* => *color::rgb24* |
| 177 |
|
| 178 |
(No description) |
| 179 |
|
| 180 |
Return value: (color::ansi256) |
| 181 |
|
| 182 |
rgb_to_ansi256_bg_code |
| 183 |
Usage: |
| 184 |
|
| 185 |
rgb_to_ansi256_bg_code($color) -> str |
| 186 |
|
| 187 |
Convert RGB to ANSI-256 color escape sequence to change background |
| 188 |
color. |
| 189 |
|
| 190 |
This function is not exported by default, but exportable. |
| 191 |
|
| 192 |
Arguments ('*' denotes required arguments): |
| 193 |
|
| 194 |
* $color* => *color::rgb24* |
| 195 |
|
| 196 |
(No description) |
| 197 |
|
| 198 |
Return value: (str) |
| 199 |
|
| 200 |
rgb_to_ansi256_fg_code |
| 201 |
Usage: |
| 202 |
|
| 203 |
rgb_to_ansi256_fg_code($color) -> str |
| 204 |
|
| 205 |
Convert RGB to ANSI-256 color escape sequence to change foreground |
| 206 |
color. |
| 207 |
|
| 208 |
This function is not exported by default, but exportable. |
| 209 |
|
| 210 |
Arguments ('*' denotes required arguments): |
| 211 |
|
| 212 |
* $color* => *color::rgb24* |
| 213 |
|
| 214 |
(No description) |
| 215 |
|
| 216 |
Return value: (str) |
| 217 |
|
| 218 |
rgb_to_ansi_bg_code |
| 219 |
Usage: |
| 220 |
|
| 221 |
rgb_to_ansi_bg_code($color) -> str |
| 222 |
|
| 223 |
Convert RGB to ANSI color escape sequence to change background color. |
| 224 |
|
| 225 |
Autodetect terminal capability and can return either empty string, |
| 226 |
16-color, 256-color, or 24bit-code. |
| 227 |
|
| 228 |
Which color depth used is determined by "COLOR_DEPTH" environment |
| 229 |
setting or from Term::Detect::Software if that module is available). In |
| 230 |
other words, this function automatically chooses |
| 231 |
rgb_to_ansi{24b,256,16}*bg*code(). |
| 232 |
|
| 233 |
This function is not exported by default, but exportable. |
| 234 |
|
| 235 |
Arguments ('*' denotes required arguments): |
| 236 |
|
| 237 |
* $color* => *color::rgb24* |
| 238 |
|
| 239 |
(No description) |
| 240 |
|
| 241 |
Return value: (str) |
| 242 |
|
| 243 |
rgb_to_ansi_fg_code |
| 244 |
Usage: |
| 245 |
|
| 246 |
rgb_to_ansi_fg_code($color) -> str |
| 247 |
|
| 248 |
Convert RGB to ANSI color escape sequence to change foreground color. |
| 249 |
|
| 250 |
Autodetect terminal capability and can return either empty string, |
| 251 |
16-color, 256-color, or 24bit-code. |
| 252 |
|
| 253 |
Color depth used is determined by "COLOR_DEPTH" environment setting or |
| 254 |
from Term::Detect::Software if that module is available. In other words, |
| 255 |
this function automatically chooses rgb_to_ansi{24b,256,16}*fg*code(). |
| 256 |
|
| 257 |
This function is not exported by default, but exportable. |
| 258 |
|
| 259 |
Arguments ('*' denotes required arguments): |
| 260 |
|
| 261 |
* $color* => *color::rgb24* |
| 262 |
|
| 263 |
(No description) |
| 264 |
|
| 265 |
Return value: (str) |
| 266 |
|
| 267 |
ansi16fg($rgb) => STR |
| 268 |
Alias for rgb_to_ansi16_fg_code(). |
| 269 |
|
| 270 |
ansi16bg($rgb) => STR |
| 271 |
Alias for rgb_to_ansi16_bg_code(). |
| 272 |
|
| 273 |
ansi256fg($rgb) => STR |
| 274 |
Alias for rgb_to_ansi256_fg_code(). |
| 275 |
|
| 276 |
ansi256bg($rgb) => STR |
| 277 |
Alias for rgb_to_ansi256_bg_code(). |
| 278 |
|
| 279 |
ansi24bfg($rgb) => STR |
| 280 |
Alias for rgb_to_ansi24b_fg_code(). |
| 281 |
|
| 282 |
ansi24bbg($rgb) => STR |
| 283 |
Alias for rgb_to_ansi24b_bg_code(). |
| 284 |
|
| 285 |
rgb_to_ansi_fg_code($rgb) => STR |
| 286 |
ansifg($rgb) => STR |
| 287 |
Alias for rgb_to_ansi_fg_code(). |
| 288 |
|
| 289 |
ansibg($rgb) => STR |
| 290 |
Alias for rgb_to_ansi_bg_code(). |
| 291 |
|
| 292 |
ansi_reset( [ $conditional ]) |
| 293 |
Returns "\e[0m", which is the ANSI escape sequence to reset color. |
| 294 |
Normally you print this sequence after you print colored text. |
| 295 |
|
| 296 |
If $conditional is set to true, then ansi_reset() will return "" if |
| 297 |
color is disabled. |
| 298 |
|
| 299 |
ENVIRONMENT |
| 300 |
NO_COLOR |
| 301 |
Can be used to explicitly disable color. See <https://no-color.org> for |
| 302 |
more details. |
| 303 |
|
| 304 |
Observed by: ansi{fg,bg}. |
| 305 |
|
| 306 |
COLOR => bool |
| 307 |
Can be used to explicitly disable color by setting it to 0. |
| 308 |
|
| 309 |
Observed by: ansi{fg,bg}. |
| 310 |
|
| 311 |
COLOR_DEPTH => INT |
| 312 |
Can be used to explicitly set color depth instead of trying to detect |
| 313 |
appropriate color depth. |
| 314 |
|
| 315 |
Observed by: ansi{fg,bg}. |
| 316 |
|
| 317 |
HOMEPAGE |
| 318 |
Please visit the project's homepage at |
| 319 |
<https://metacpan.org/release/Color-ANSI-Util>. |
| 320 |
|
| 321 |
SOURCE |
| 322 |
Source repository is at |
| 323 |
<https://github.com/perlancar/perl-Color-ANSI-Util>. |
| 324 |
|
| 325 |
SEE ALSO |
| 326 |
Term::ANSIColor |
| 327 |
|
| 328 |
<http://en.wikipedia.org/wiki/ANSI_escape_code> |
| 329 |
|
| 330 |
AUTHOR |
| 331 |
perlancar <[email protected]> |
| 332 |
|
| 333 |
CONTRIBUTOR |
| 334 |
Steven Haryanto <[email protected]> |
| 335 |
|
| 336 |
CONTRIBUTING |
| 337 |
To contribute, you can send patches by email/via RT, or send pull |
| 338 |
requests on GitHub. |
| 339 |
|
| 340 |
Most of the time, you don't need to build the distribution yourself. You |
| 341 |
can simply modify the code, then test via: |
| 342 |
|
| 343 |
% prove -l |
| 344 |
|
| 345 |
If you want to build the distribution (e.g. to try to install it locally |
| 346 |
on your system), you can install Dist::Zilla, |
| 347 |
Dist::Zilla::PluginBundle::Author::PERLANCAR, |
| 348 |
Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two |
| 349 |
other Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps |
| 350 |
required beyond that are considered a bug and can be reported to me. |
| 351 |
|
| 352 |
COPYRIGHT AND LICENSE |
| 353 |
This software is copyright (c) 2024, 2020, 2019, 2018, 2017, 2016, 2015, |
| 354 |
2014, 2013 by perlancar <[email protected]>. |
| 355 |
|
| 356 |
This is free software; you can redistribute it and/or modify it under |
| 357 |
the same terms as the Perl 5 programming language system itself. |
| 358 |
|
| 359 |
BUGS |
| 360 |
Please report any bugs or feature requests on the bugtracker website |
| 361 |
<https://rt.cpan.org/Public/Dist/Display.html?Name=Color-ANSI-Util> |
| 362 |
|
| 363 |
When submitting a bug or request, please include a test-file or a patch |
| 364 |
to an existing test-file that illustrates the bug or desired feature. |
| 365 |
|