From 5306ea72904dd1fcaf896a167db13f8765daf046 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lars=20D=C9=AA=E1=B4=87=E1=B4=84=E1=B4=8B=E1=B4=8F?= =?UTF-8?q?=E1=B4=A1=20=E8=BF=AA=E6=8B=89=E6=96=AF?= <daxim@cpan.org> Date: Sat, 13 Jul 2019 13:02:45 +0200 Subject: [PATCH] True Color support --- Changes | 2 ++ lib/Term/ANSIColor.pm | 42 ++++++++++++++++++++++++++++++++++++++---- t/module/truecolor.t | 6 ++++++ 3 files changed, 46 insertions(+), 4 deletions(-) create mode 100644 t/module/truecolor.t diff --git a/Changes b/Changes index 28dc307..5721c03 100644 --- a/Changes +++ b/Changes @@ -17,6 +17,8 @@ Term::ANSIColor 4.07 (unreleased) * Update perlcritic configuration. * Add SPDX-License-Identifier headers to all significant files. + True Color support + Term::ANSIColor 4.06 (2016-10-28) Add aliases ansi16 through ansi255 and on_ansi16 through on_ansi255 diff --git a/lib/Term/ANSIColor.pm b/lib/Term/ANSIColor.pm index d08313b..3279e8f 100644 --- a/lib/Term/ANSIColor.pm +++ b/lib/Term/ANSIColor.pm @@ -68,7 +68,7 @@ BEGIN { # Exported symbol configuration. @ISA = qw(Exporter); - @EXPORT = qw(color colored); + @EXPORT = qw(color colored truecolor on_truecolor); @EXPORT_OK = qw(uncolor colorstrip colorvalid coloralias); %EXPORT_TAGS = ( constants => \@colorlist, @@ -414,6 +414,26 @@ sub color { return ($attribute ne q{}) ? "\e[${attribute}m" : undef; } +# Given three numbers, interpret as RGB true color triple for foreground. +# +# @rgb - The list of numbers +# +# Returns: ANSI color escape sequence +sub truecolor { + my (@rgb) = @_; + return sprintf "\e[38;2;%u;%u;%um", @rgb; +} + +# Given three numbers, interpret as RGB true color triple for background. +# +# @rgb - The list of numbers +# +# Returns: ANSI color escape sequence +sub on_truecolor { + my (@rgb) = @_; + return sprintf "\e[48;2;%u;%u;%um", @rgb; +} + # Return a list of named color attributes for a given set of escape codes. # Escape sequences can be given with or without enclosing "\e[" and "m". The # empty escape sequence '' or "\e[m" gives an empty list of attrs. @@ -607,6 +627,8 @@ undef CLICOLOR print colored(['yellow on_magenta'], 'Yellow on magenta.', "

"); print colored(['red on_bright_yellow'], 'Red on bright yellow.', "

"); print colored(['bright_red on_black'], 'Bright red on black.', "

"); + print truecolor(0xba,0xda,0x55), on_truecolor(0xc0,0xff,0xee), + 'olive green on light cyan'; print "

"; # Map escape sequences back to color names. @@ -668,9 +690,10 @@ particular features and the versions of Perl that included them. =head2 Supported Colors -Terminal emulators that support color divide into three types: ones that -support only eight colors, ones that support sixteen, and ones that -support 256. This module provides the ANSI escape codes for all of them. +Terminal emulators that support color divide into four types: ones that +support only eight colors, ones that support sixteen, ones that +support 256 and ones that support 16.7 million, also known as True Color. +This module provides the ANSI escape codes for all of them. These colors are referred to as ANSI colors 0 through 7 (normal), 8 through 15 (16-color), and 16 through 255 (256-color). @@ -812,6 +835,17 @@ the code to reset the color may unexpectedly sit in the standard output buffer rather than going to the display, causing standard error output to appear in the wrong color. +=item truecolor(NUM, NUM, NUM) + +=item on_truecolor(NUM, NUM, NUM) + +since: v4.07 + +Similar to L</color>, returns an escape sequence for an arbitrary color +in the foreground or background, respectively. Pass three numbers, they +will be coerced to unsigned integers and are interpreted as a triple +of red, green, blue. The specification implies that only values from +0..255 make sense, but the module does not check validity. + =item uncolor(ESCAPE) uncolor() performs the opposite translation as color(), turning escape diff --git a/t/module/truecolor.t b/t/module/truecolor.t new file mode 100644 index 0000000..f48241e --- /dev/null +++ b/t/module/truecolor.t @@ -0,0 +1,6 @@ +use strict; +use warnings; +use Test::More tests => 3; +use_ok 'Term::ANSIColor', qw(truecolor on_truecolor); +is truecolor(0xba,0xda,0x55), "\e[38;2;186;218;85m", 'sub truecolor'; +is on_truecolor(0xc0,0xff,0xee), "\e[48;2;192;255;238m", 'sub on_truecolor'; -- 2.22.0