简体   繁体   中英

Print more than ANSI color values in Perl

I love Perl's Term::ANSIColor module but is it possible to print out colours other than the ones provided?

I'm trying to print out words with a range between deep red and bright green, with a decent number of steps between them. Is there a way to supply an RGB value or something to change the color of the text?

You use Term::ExtendedColor . You can use 256 colors by this module.

A few terminals even accept full 8-bit RGB colour specifications.

$ perl -E 'say "\e[38:2:255:100:80mHello\e[m"'
Hello

This may be printed in rgb(255,100,80) colour pink. Depends on your terminal.

As a way to obtain xterm256 colour values out of arbitrary RGB combinations, you might also like Convert::Color

use strict;
use warnings;

use Convert::Color;
use Convert::Color::XTerm;

foreach my $hue ( map { $_ * 15 } 0 .. 120/15 ) {
   my $c = Convert::Color->new( "hsv:$hue,1,1" );
   my $index = $c->as_xterm->index;
   print "\e[38:5:${index}mHue=$hue\e[m\n";
}

I'd paste the output here but it's hard to convey the colours in a comment :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM