Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

Exploring the color palette

by pbeckingham (Parson)
on Nov 18, 2005 at 15:37 UTC ( #509827=CUFP: print w/replies, xml ) Need Help??

I have been using this little script for a while to help me develop and diagnose Perl programs that make extensive use of color in a terminal window. It displays all (?) the colors that your terminal supports. I find it useful - perhaps you would too.

#! /usr/bin/perl use strict; use warnings; my @fgColors = ( 'default', 'bold', 'black', 'red', 'blue', 'yellow', 'green', 'majenta', 'cyan', 'white', 'bold black', 'bold red', 'bold blue', 'bold yellow', 'bold green', 'bold majenta', 'bold cyan', 'bold whit +e'); my @bgColors = ( 'default', 'black', 'red', 'blue', 'yellow', 'green', 'majenta', 'cy +an', 'white'); my %fg = ( 'default' => "", 'bold' => "\e[1m", 'black' => "\e[30m", 'red' => "\e[31m", 'blue' => "\e[32m", 'yellow' => "\e[33m", 'green' => "\e[34m", 'majenta' => "\e[35m", 'cyan' => "\e[36m", 'white' => "\e[37m", 'bold black' => "\e[1;30m", 'bold red' => "\e[1;31m", 'bold blue' => "\e[1;32m", 'bold yellow' => "\e[1;33m", 'bold green' => "\e[1;34m", 'bold majenta' => "\e[1;35m", 'bold cyan' => "\e[1;36m", 'bold white' => "\e[1;37m", ); my %bg = ( 'default' => "", 'black' => "\e[40m", 'red' => "\e[41m", 'blue' => "\e[42m", 'yellow' => "\e[43m", 'green' => "\e[44m", 'majenta' => "\e[45m", 'cyan' => "\e[46m", 'white' => "\e[47m"); print " e[40m e[41m e[42m e[43m e[44m e[45m e[46m e[47m +\n"; foreach my $fgc (@fgColors) { my $printable = $fg{$fgc}; $printable =~ s/\e/e/; printf "%9s ", $printable; print "$fg{$fgc}$bg{$_} Text \e[0m" for @bgColors; print "\n"; } # Xterm extended 256-color. print "\e[0m\n 0-15 "; print "\e[38;5;${_}m\e[48;5;${_}m " for 0 .. 15; print "\e[0m\n 240-255 "; print "\e[38;5;${_}m\e[48;5;${_}m " for 232 .. 255; print "\e[0m\n"; for my $i (16 .. 231) { print "\e[0m\n " if $i && ($i + 2) % 6 == 0; print "\e[38;5;${i}m\e[48;5;${i}m "; } print "\e[0m\n\n"; exit 0;

pbeckingham - typist, perishable vertebrate.

Replies are listed 'Best First'.
Re: Exploring the color palette
by jdhedden (Deacon) on Nov 18, 2005 at 16:49 UTC
    Here's my version using Term::ANSIColor complete with the other color modifiers.
    #!/usr/bin/perl use strict; use warnings; use Term::ANSIColor; # Color modifiers my @mods = ('', 'bold', 'dark', 'underline', 'blink', 'reverse', 'concealed'); # Foreground colors my @fgs = ('black', 'red', 'blue', 'yellow', 'green', 'magenta', 'cyan', 'white'); # Background colors my @bgs = ('', 'on_black', 'on_red', 'on_blue', 'on_yellow', 'on_green', 'on_magenta', 'on_cyan', 'on_white'); MAIN: { print "\n \\ on_ black red blue yellow green m +agenta cyan white\n"; # Modifiers only foreach my $mod (@mods) { colored_line($mod, ''); } # Foreground colors combined with modifiers foreach my $mod (@mods) { foreach my $fg (@fgs) { colored_line($mod, $fg); } } } exit(0); ### Subroutines ### sub colored_line { my ($mod, $fg) = @_; # Line label printf(" %9s %-7s ", $mod, $fg); # Segments with background colors for my $bg (@bgs) { print(colored([$mod, $fg, $bg], ' Text ')); } print("\n"); } # EOF

    Remember: There's always one more bug.
Re: Exploring the color palette
by Aristotle (Chancellor) on Nov 18, 2005 at 15:39 UTC

    Are you aware of Term::ANSIColor? It makes dealing with coloured output a lot nicer.

    Makeshifts last the longest.

      Yes, I am aware of Term::ANSIColor, but I don't use it for two reasons - firstly, the module is not available, and secondly, I want to iterate through the possibilities and show up the terminal deficiencies. Fixing problems with color use is not always easy, and having those escape sequences helps me.

      pbeckingham - typist, perishable vertebrate.

        Fair enough, even though I can never fathom why people mention “it’s not installed” for pure-Perl modules.

        Makeshifts last the longest.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://509827]
Approved by Albannach
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (8)
As of 2021-05-12 20:26 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (132 votes). Check out past polls.