NAME Color::ANSI::Util - Routines for dealing with ANSI colors VERSION version 0.05 SYNOPSIS use Color::ANSI::Util qw( ansi16_to_rgb ansi256_to_rgb rgb_to_ansi16 rgb_to_ansi256 rgb_to_ansi16_fg_code ansi16fg rgb_to_ansi16_bg_code ansi16bg rgb_to_ansi256_fg_code ansi256fg rgb_to_ansi256_bg_code ansi256bg ); # convert ANSI 16-color index to RGB say ansi16_to_rgb(1); # => "800000" (red) say ansi16_to_rgb("red"); # => "800000" (ditto) say ansi16_to_rgb(9); # => "ff0000" (red bold) say ansi16_to_rgb("bold red"); # => "ff0000" (ditto) # convert RGB to ANSI 16-color index say ansi16_to_rgb("ac0405"); # => 1 (closest to red) say ansi16_to_rgb("f01010"); # => 9 (closest to bold red) # convert RGB to ANSI 16-color escape code for setting foreground color say rgb_to_ansi16_fg_code("ac0405"); # => "\e[31m" say ansi16fg("f01010"); # => "\e[31;1m" (shorter alias) # ditto but for background color (bgcolor actually only supports 0-7) say rgb_to_ansi16_bg_code("ac0405"); # => "\e[41m" say ansi16bg("f01010"); # => "\e[41m" (shorter alias) # convert ANSI 256-color index to RGB say ansi256_to_rgb(204); # => "ff5f87" # convert RGB to ANSI 256-color index say rgb_to_ansi256("ff5f88"); # => 204 (closest) # convert RGB to ANSI 256-color escape code for setting foreground color say rgb_to_ansi256_fg_code("ff5f87"); # => "\e[38;5;204m" say ansi256fg("ff5f87"); # => "\e[38;5;204m" (shorter alias) # ditto but for background color (bgcolor actually only supports 0-7) say rgb_to_ansi256_bg_code("ff5f87"); # => "\e[48;5;204m" say ansi256bg("ff5f87"); # => "\e[48;5;204m" (shorter alias) DESCRIPTION This module provides routines for dealing with ANSI colors. Keywords: xterm, xterm-256color, terminal FUNCTIONS ansi16_to_rgb($color) => STR Convert ANSI-16 color to RGB. $color is number from 0-15, or color names "black", "red", "green", "yellow", "blue", "magenta", "cyan", "white" with "bold" to indicate bold/bright. Return 6-hexdigit, e.g. "ff00cc". Die on invalid input. rgb_to_ansi16($color) => INT Convert RGB to ANSI-16 color. $color is 6-hexdigit RGB color like "ff00cc". Will pick the closest color. Return number from 0-15. Die on invalid input. ansi256_to_rgb($color) => STR Convert ANSI-256 color to RGB. $color is a number from 0-255. Return 6-hexdigit, e.g. "ff00cc". Die on invalid input. rgb_to_ansi256($color) => INT Convert RGB to ANSI-256 color. $color is 6-hexdigit RGB color like "ff00cc". Will pick the closest color. Return number between 0-255. Die on invalid input. rgb_to_ansi16_fg_code($rgb) => STR ansi16fg($rgb) => STR Alias for rgb_to_ansi16_fg_code(). rgb_to_ansi16_bg_code($rgb) => STR ansi16bg($rgb) => STR Alias for rgb_to_ansi16_bg_code(). rgb_to_ansi256_fg_code($rgb) => STR ansi256fg($rgb) => STR Alias for rgb_to_ansi256_fg_code(). rgb_to_ansi256_bg_code($rgb) => STR ansi256bg($rgb) => STR Alias for rgb_to_ansi256_bg_code(). rgb_to_ansi24b_fg_code($rgb) => STR Return ANSI escape code to set 24bit foreground color. Supported by Konsole and Yakuake. ansi24bfg($rgb) => STR Alias for rgb_to_ansi24b_fg_code(). rgb_to_ansi24b_bg_code($rgb) => STR Return ANSI escape code to set 24bit background color. Supported by Konsole and Yakuake. ansi24bbg($rgb) => STR Alias for rgb_to_ansi24b_bg_code(). rgb_to_ansi_fg_code($rgb) => STR Return ANSI escape code to set 24bit/256/16 foreground color (which color depth used is determined by "COLOR_DEPTH" environment setting or the latest detect_color_depth() result). In other words, this function automatically chooses rgb_to_ansi{24b,256,16}_fg_code(). ansifg($rgb) => STR Alias for rgb_to_ansi_fg_code(). rgb_to_ansi_bg_code($rgb) => STR Return ANSI escape code to set 24bit/256/16 background color (which color depth used is determined by "COLOR_DEPTH" environment setting or the latest detect_color_depth() result). In other words, this function automatically chooses rgb_to_ansi{24b,256,16}_bg_code(). ansibg($rgb) => STR Alias for rgb_to_ansi_bg_code(). detect_color_depth() => INT Detect color depth. Return either 2**24 (16777216), 256, or 16. Currently 24bit is determined by existence of Konsole environment variables, and 256 colors by detecting "TERM" value of "xterm-256color". Otherwise, 16 colors is assumed. ENVIRONMENT COLOR_DEPTH => INT Observed by: ansi{fg,bg}. 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. TODO Routine to convert ANSI escape code, e.g. "\e[31;1m" into RGB value (ff0000). SEE ALSO Term::ANSIColor http://en.wikipedia.org/wiki/ANSI_escape_code AUTHOR Steven Haryanto COPYRIGHT AND LICENSE This software is copyright (c) 2013 by Steven Haryanto. This is free software; you can redistribute it and/or modify it under the same terms as the Perl 5 programming language system itself.