Color:...I::Util(3pm)

πŸ’… Color::ANSI::Util β€” the perl doc, bestie edition

okay so we're reading Color::ANSI::Util(3pm), straight outta the User Contributed Perl Documentation, and I'm gonna walk you through it like we're texting at 2am 🫢 buckle up.


✨ NAME ✨

Color::ANSI::Util β€” Routines for dealing with ANSI colors 🌈


πŸŽ€ VERSION πŸŽ€

This document describes version 0.165 of Color::ANSI::Util (from Perl distribution Color-ANSI-Util), released on 2024-02-23. iconic, no notes. πŸ‘‘


πŸ¦‹ SYNOPSIS πŸ¦‹

use Color::ANSI::Util qw(
    ansifg
    ansibg
);

say ansifg("f0c010"); # => "\e[33;1m" (on 16-color terminal)
                      # => "\e[38;5;11m" (on 256-color terminal)
                      # => "\e[38;2;240;192;16m" (on 24-bit-color terminal)

say ansibg("ff5f87"); # => "\e[47m" (on 16-color terminal)
                      # => "\e[48;5;7m" (on 256-color terminal)
                      # => "\e[48;2;255;95;135m" (on 24-bit-color terminal)

and there are a BUNCH of other exportable functions too, mostly for converting between RGB and ANSI color (16/256/24bit color depth). we are so spoiled fr fr πŸ’–


πŸ’œ DESCRIPTION πŸ’œ

This module provides routines for dealing with ANSI colors. The two main characters β€” sorry, main functions β€” are ansifg and ansibg. With those functions, you can specify colors in RGB and let it output the correct ANSI color escape code according to the color depth support of the terminal (whether 16-color, 256-color, or 24bit). Literally it just knows. ✨

There are other functions to convert RGB to ANSI in specific color depths, or reverse functions to convert from ANSI to RGB codes. the way that it goes both directions… obsessed. πŸ¦‹

Keywords: xterm, xterm-256color, terminal


😭 BUGS/NOTES 😭

Algorithm for finding closest indexed color from RGB color currently not very efficient. Probably can add some threshold square distance, below which we can shortcut to the final answer. bestie it's a little slow rn, we know, it's on the list πŸ’€


πŸ‘‘ FUNCTIONS πŸ‘‘


🌟 ansi16_to_rgb

Usage:

ansi16_to_rgb($color) -> color::rgb24

Convert ANSI-16 color to RGB.

Returns 6-hexdigit, e.g. 'ff00cc'.

This function is not exported by default, but exportable.

Arguments ('*' denotes required arguments):

  • $color* => color::ansi16 β€” (No description)

Return value: (color::rgb24)


🌟 ansi256_to_rgb

Usage:

ansi256_to_rgb($color) -> color::rgb24

Convert ANSI-256 color to RGB.

This function is not exported by default, but exportable.

Arguments ('*' denotes required arguments):

  • $color* => color::ansi256 β€” (No description)

Return value: (color::rgb24)


🌟 rgb_to_ansi16

Usage:

rgb_to_ansi16($color) -> color::ansi16

Convert RGB to ANSI-16 color.

This function is not exported by default, but exportable.

Arguments ('*' denotes required arguments):

  • $color* => color::rgb24 β€” (No description)

Return value: (color::ansi16)


🌟 rgb_to_ansi16_bg_code

Usage:

rgb_to_ansi16_bg_code($color) -> str

Convert RGB to ANSI-16 color escape sequence to change background color.

This function is not exported by default, but exportable.

Arguments ('*' denotes required arguments):

  • $color* => color::rgb24 β€” (No description)

Return value: (str)


🌟 rgb_to_ansi16_fg_code

Usage:

rgb_to_ansi16_fg_code($color) -> str

Convert RGB to ANSI-16 color escape sequence to change foreground color.

This function is not exported by default, but exportable.

Arguments ('*' denotes required arguments):

  • $color* => color::rgb24 β€” (No description)

Return value: (str)


🌟 rgb_to_ansi24b_bg_code

Usage:

rgb_to_ansi24b_bg_code($color) -> str

Convert RGB to ANSI 24bit-color escape sequence to change background color.

This function is not exported by default, but exportable.

Arguments ('*' denotes required arguments):

  • $color* => color::rgb24 β€” (No description)

Return value: (str)


🌟 rgb_to_ansi24b_fg_code

Usage:

rgb_to_ansi24b_fg_code($color) -> str

Convert RGB to ANSI 24bit-color escape sequence to change foreground color.

This function is not exported by default, but exportable.

Arguments ('*' denotes required arguments):

  • $color* => color::rgb24 β€” (No description)

Return value: (str)


🌟 rgb_to_ansi256

Usage:

rgb_to_ansi256($color) -> color::ansi256

Convert RGB to ANSI-256 color.

This function is not exported by default, but exportable.

Arguments ('*' denotes required arguments):

  • $color* => color::rgb24 β€” (No description)

Return value: (color::ansi256)


🌟 rgb_to_ansi256_bg_code

Usage:

rgb_to_ansi256_bg_code($color) -> str

Convert RGB to ANSI-256 color escape sequence to change background color.

This function is not exported by default, but exportable.

Arguments ('*' denotes required arguments):

  • $color* => color::rgb24 β€” (No description)

Return value: (str)


🌟 rgb_to_ansi256_fg_code

Usage:

rgb_to_ansi256_fg_code($color) -> str

Convert RGB to ANSI-256 color escape sequence to change foreground color.

This function is not exported by default, but exportable.

Arguments ('*' denotes required arguments):

  • $color* => color::rgb24 β€” (No description)

Return value: (str)


🌟 rgb_to_ansi_bg_code

Usage:

rgb_to_ansi_bg_code($color) -> str

Convert RGB to ANSI color escape sequence to change background color.

Autodetect terminal capability and can return either empty string, 16-color, 256-color, or 24bit-code. ✨

Which color depth used is determined by "COLOR_DEPTH" environment setting or from Term::Detect::Software if that module is available). In other words, this function automatically chooses rgb_to_ansi{24b,256,16}bgcode().

This function is not exported by default, but exportable.

Arguments ('*' denotes required arguments):

  • $color* => color::rgb24 β€” (No description)

Return value: (str)


🌟 rgb_to_ansi_fg_code

Usage:

rgb_to_ansi_fg_code($color) -> str

Convert RGB to ANSI color escape sequence to change foreground color.

Autodetect terminal capability and can return either empty string, 16-color, 256-color, or 24bit-code. ✨

Color depth used is determined by "COLOR_DEPTH" environment setting or from Term::Detect::Software if that module is available. In other words, this function automatically chooses rgb_to_ansi{24b,256,16}fgcode().

This function is not exported by default, but exportable.

Arguments ('*' denotes required arguments):

  • $color* => color::rgb24 β€” (No description)

Return value: (str)


🌟 ansi16fg($rgb) => STR

Alias for rgb_to_ansi16_fg_code().


🌟 ansi16bg($rgb) => STR

Alias for rgb_to_ansi16_bg_code().


🌟 ansi256fg($rgb) => STR

Alias for rgb_to_ansi256_fg_code().


🌟 ansi256bg($rgb) => STR

Alias for rgb_to_ansi256_bg_code().


🌟 ansi24bfg($rgb) => STR

Alias for rgb_to_ansi24b_fg_code().


🌟 ansi24bbg($rgb) => STR

Alias for rgb_to_ansi24b_bg_code().


🌟 rgb_to_ansi_fg_code($rgb) => STR

🌟 ansifg($rgb) => STR

Alias for rgb_to_ansi_fg_code().


🌟 ansibg($rgb) => STR

Alias for rgb_to_ansi_bg_code().


🌟 ansi_reset( [ $conditional ])

Returns "\e[0m", which is the ANSI escape sequence to reset color. Normally you print this sequence after you print colored text. pls don't forget the reset, bestie, nobody wants the rest of their terminal stained πŸ’…

If $conditional is set to true, then ansi_reset() will return "" if color is disabled.


🌈 ENVIRONMENT 🌈


πŸ’– NO_COLOR

Can be used to explicitly disable color. See https://no-color.org for more details.

Observed by: ansi{fg,bg}.


πŸ’– COLOR => bool

Can be used to explicitly disable color by setting it to 0.

Observed by: ansi{fg,bg}.


πŸ’– COLOR_DEPTH => INT

Can be used to explicitly set color depth instead of trying to detect appropriate color depth.

Observed by: ansi{fg,bg}.


🏠 HOMEPAGE 🏠

Please visit the project's homepage at https://metacpan.org/release/Color-ANSI-Util.


🧚 SOURCE 🧚

Source repository is at https://github.com/perlancar/perl-Color-ANSI-Util.


⭐ SEE ALSO ⭐

Term::ANSIColor

http://en.wikipedia.org/wiki/ANSI_escape_code


🌸 AUTHOR 🌸

perlancar <perlancar@cpan.org>


🌸 CONTRIBUTOR 🌸

Steven Haryanto <stevenharyanto@gmail.com>


πŸŽ€ CONTRIBUTING πŸŽ€

To contribute, you can send patches by email/via RT, or send pull requests on GitHub. we love a contributor era πŸ’–

Most of the time, you don't need to build the distribution yourself. You can simply modify the code, then test via:

% prove -l

If you want to build the distribution (e.g. to try to install it locally on your system), you can install Dist::Zilla, Dist::Zilla::PluginBundle::Author::PERLANCAR, Pod::Weaver::PluginBundle::Author::PERLANCAR, and sometimes one or two other Dist::Zilla- and/or Pod::Weaver plugins. Any additional steps required beyond that are considered a bug and can be reported to me. πŸ™ˆ


This software is copyright (c) 2024, 2020, 2019, 2018, 2017, 2016, 2015, 2014, 2013 by perlancar <perlancar@cpan.org>.

This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.


πŸ› BUGS πŸ›

Please report any bugs or feature requests on the bugtracker website https://rt.cpan.org/Public/Dist/Display.html?Name=Color-ANSI-Util

When submitting a bug or request, please include a test-file or a patch to an existing test-file that illustrates the bug or desired feature.


okay that's the whole thing, you ate it up 🌈 this was perl v5.40.1, dated 2025-05-04, and that's the vibe check, periodt πŸ’…βœ¨πŸ’–

πŸ“¦ where this page comes from