Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 767
benui Avatar asked Feb 24 '12 08:02

benui


2 Answers

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

like image 166
syohex Avatar answered Nov 20 '22 05:11

syohex


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 :)

like image 26
LeoNerd Avatar answered Nov 20 '22 03:11

LeoNerd