Blame examples/generate-colors

Packit e6c7a3
#!/usr/bin/perl
Packit e6c7a3
#
Packit e6c7a3
# Generate 256-color test files.
Packit e6c7a3
#
Packit e6c7a3
# Takes one or more of six arguments: basic, bright, fg256 (foreground), bg256
Packit e6c7a3
# (background), grey, or ansi256.  Generates a test file for either basic ANSI
Packit e6c7a3
# colors or 256-color emulators, testing that region of colors.
Packit e6c7a3
#
Packit e6c7a3
# This script requires Term::ANSIColor 4.00 or later already be installed or
Packit e6c7a3
# that this script be run manually under Perl with flags pointing to where the
Packit e6c7a3
# module is.
Packit e6c7a3
#
Packit e6c7a3
# Copyright 2012 Kurt Starsinic <kstarsinic@gmail.com>
Packit e6c7a3
# Copyright 2012, 2014, 2016 Russ Allbery <rra@cpan.org>
Packit e6c7a3
#
Packit e6c7a3
# This program is free software; you may redistribute it and/or modify it
Packit e6c7a3
# under the same terms as Perl itself.
Packit e6c7a3
Packit e6c7a3
use 5.006;
Packit e6c7a3
use strict;
Packit e6c7a3
use warnings;
Packit e6c7a3
Packit e6c7a3
use Carp qw(croak);
Packit e6c7a3
use Term::ANSIColor 4.00 qw(color);
Packit e6c7a3
Packit e6c7a3
# Screen width for centering headings.
Packit e6c7a3
use constant SCREEN_WIDTH => 80;
Packit e6c7a3
Packit e6c7a3
# The basic attributes and eight colors.
Packit e6c7a3
use constant ATTRIBUTES => qw(bold dark italic underline blink concealed);
Packit e6c7a3
use constant COLORS     => qw(black red green yellow blue magenta cyan white);
Packit e6c7a3
Packit e6c7a3
# print and printf with error checking.  autodie unfortunately can't help us
Packit e6c7a3
# with these because they can't be prototyped and hence can't be overridden.
Packit e6c7a3
Packit e6c7a3
## no critic (Subroutines::RequireArgUnpacking)
Packit e6c7a3
sub print_checked  { print @_  or croak('print failed');  return }
Packit e6c7a3
sub printf_checked { printf @_ or croak('printf failed'); return }
Packit e6c7a3
## use critic
Packit e6c7a3
Packit e6c7a3
# The sample background or foreground colors for 256-color tests.
Packit e6c7a3
my @SAMPLES = qw(000 222 555);
Packit e6c7a3
Packit e6c7a3
# The list of all possible RGB values.
Packit e6c7a3
my @RGB;
Packit e6c7a3
for my $r (0 .. 5) {
Packit e6c7a3
    for my $g (0 .. 5) {
Packit e6c7a3
        push(@RGB, map { "$r$g$_" } 0 .. 5);
Packit e6c7a3
    }
Packit e6c7a3
}
Packit e6c7a3
Packit e6c7a3
# Center a text string with spaces.
Packit e6c7a3
#
Packit e6c7a3
# $text  - Text to center
Packit e6c7a3
# $width - Width in which to center the text
Packit e6c7a3
#
Packit e6c7a3
# Returns: Text centered within that width
Packit e6c7a3
sub center {
Packit e6c7a3
    my ($text, $width) = @_;
Packit e6c7a3
    my $padding  = $width - length($text);
Packit e6c7a3
    my $trailing = int($padding / 2);
Packit e6c7a3
    my $leading  = $padding - $trailing;
Packit e6c7a3
    return (q{ } x $leading) . $text . (q{ } x $trailing);
Packit e6c7a3
}
Packit e6c7a3
Packit e6c7a3
# Print out the test file that tries all the basic eight ANSI colors.
Packit e6c7a3
#
Packit e6c7a3
# Returns: undef
Packit e6c7a3
#  Throws: Text exception on I/O failure
Packit e6c7a3
sub print_basic_test {
Packit e6c7a3
    print_checked("Basic ANSI colors (eight-color, or dim)\n\n");
Packit e6c7a3
    for my $bg (COLORS) {
Packit e6c7a3
        printf_checked('%4s %-7s ', q{ }, $bg);
Packit e6c7a3
        for my $fg (COLORS) {
Packit e6c7a3
            print_checked(color($fg, "on_$bg"), center($fg, 8));
Packit e6c7a3
        }
Packit e6c7a3
        print_checked(color('reset'), "\n");
Packit e6c7a3
        printf_checked('%4s %-7s ', 'bold', $bg);
Packit e6c7a3
        for my $fg (COLORS) {
Packit e6c7a3
            print_checked(color($fg, 'bold', "on_$bg"), center($fg, 8));
Packit e6c7a3
        }
Packit e6c7a3
        print_checked(color('reset'), "\n");
Packit e6c7a3
    }
Packit e6c7a3
    print_checked("\nAttributes: ");
Packit e6c7a3
    for my $fg (ATTRIBUTES) {
Packit e6c7a3
        print_checked(center($fg, 10));
Packit e6c7a3
    }
Packit e6c7a3
    print_checked("\n", q{ } x 12);
Packit e6c7a3
    for my $fg (ATTRIBUTES) {
Packit e6c7a3
        print_checked(color($fg), center('testing', 10), color('reset'));
Packit e6c7a3
    }
Packit e6c7a3
    print_checked("\n\n");
Packit e6c7a3
    return;
Packit e6c7a3
}
Packit e6c7a3
Packit e6c7a3
# Print out the test file that tries all the bright colors from the
Packit e6c7a3
# sixteen-color palette.
Packit e6c7a3
#
Packit e6c7a3
# Returns: undef
Packit e6c7a3
#  Throws: Text exception on I/O failure
Packit e6c7a3
sub print_bright_test {
Packit e6c7a3
    print_checked("Bright ANSI colors (sixteen-color)\n\n");
Packit e6c7a3
    for my $bg (COLORS) {
Packit e6c7a3
        printf_checked('%6s %-7s ', 'dim', $bg);
Packit e6c7a3
        for my $fg (COLORS) {
Packit e6c7a3
            my $escape = color("bright_$fg", "on_$bg");
Packit e6c7a3
            print_checked($escape, center($fg, 8));
Packit e6c7a3
        }
Packit e6c7a3
        print_checked(color('reset'), "\n");
Packit e6c7a3
        printf_checked('%6s %-7s ', 'bright', $bg);
Packit e6c7a3
        for my $fg (COLORS) {
Packit e6c7a3
            my $escape = color("bright_$fg", "on_bright_$bg");
Packit e6c7a3
            print_checked($escape, center($fg, 8));
Packit e6c7a3
        }
Packit e6c7a3
        print_checked(color('reset'), "\n");
Packit e6c7a3
    }
Packit e6c7a3
    print_checked("\n");
Packit e6c7a3
    return;
Packit e6c7a3
}
Packit e6c7a3
Packit e6c7a3
# Print out the test file that tries all valid RGB foreground colors.
Packit e6c7a3
#
Packit e6c7a3
# Returns: undef
Packit e6c7a3
#  Throws: Text exception on I/O failure
Packit e6c7a3
sub print_fg256_test {
Packit e6c7a3
    print_checked("RGB000 - RGB555 from 256-color palette (foreground)\n");
Packit e6c7a3
    for my $bg (@SAMPLES) {
Packit e6c7a3
        for my $i (0 .. $#RGB) {
Packit e6c7a3
            if (($i % 18) == 0) {
Packit e6c7a3
                printf_checked("%s\nbg %03d %s",
Packit e6c7a3
                    color('reset'), $bg, color("on_rgb$bg"));
Packit e6c7a3
            }
Packit e6c7a3
            printf_checked('%s%03d ', color("rgb$RGB[$i]"), $RGB[$i]);
Packit e6c7a3
        }
Packit e6c7a3
    }
Packit e6c7a3
    print_checked(color('reset'), "\n\n");
Packit e6c7a3
    return;
Packit e6c7a3
}
Packit e6c7a3
Packit e6c7a3
# Print out the test file that tries all valid RGB background colors.
Packit e6c7a3
#
Packit e6c7a3
# Returns: undef
Packit e6c7a3
#  Throws: Text exception on I/O failure
Packit e6c7a3
sub print_bg256_test {
Packit e6c7a3
    print_checked("RGB000 - RGB555 from 256-color palette (background)\n");
Packit e6c7a3
    for my $fg (@SAMPLES) {
Packit e6c7a3
        for my $i (0 .. $#RGB) {
Packit e6c7a3
            if (($i % 18) == 0) {
Packit e6c7a3
                printf_checked("%s\nfg %03d %s",
Packit e6c7a3
                    color('reset'), $fg, color("rgb$fg"));
Packit e6c7a3
            }
Packit e6c7a3
            printf_checked('%s%03d ', color("on_rgb$RGB[$i]"), $RGB[$i]);
Packit e6c7a3
        }
Packit e6c7a3
    }
Packit e6c7a3
    print_checked(color('reset'), "\n\n");
Packit e6c7a3
    return;
Packit e6c7a3
}
Packit e6c7a3
Packit e6c7a3
# Print out the test file that shows all valid grey-scale colors.
Packit e6c7a3
#
Packit e6c7a3
# Returns: undef
Packit e6c7a3
#  Throws: Text exception on I/O failure
Packit e6c7a3
sub print_grey_test {
Packit e6c7a3
    print_checked("Grey0 - Grey23 from 256-color palette\n\n");
Packit e6c7a3
    for my $bg (0 .. 23) {
Packit e6c7a3
        printf_checked('%2d %s', $bg, color("on_grey$bg"));
Packit e6c7a3
        for my $fg (0 .. 23) {
Packit e6c7a3
            printf_checked('%s%d ', color("grey$fg"), $fg);
Packit e6c7a3
        }
Packit e6c7a3
        print_checked(color('reset'), "\n");
Packit e6c7a3
    }
Packit e6c7a3
    print_checked("\n");
Packit e6c7a3
    return;
Packit e6c7a3
}
Packit e6c7a3
Packit e6c7a3
# Print out the test file that shows the 16 ANSI colors from the 256-color
Packit e6c7a3
# palette.
Packit e6c7a3
#
Packit e6c7a3
# Returns: undef
Packit e6c7a3
#  Throws: Text exception on I/O failure
Packit e6c7a3
sub print_ansi256_test {
Packit e6c7a3
    print_checked("ANSI colors 0 - 15 from 256-color palette\n\n");
Packit e6c7a3
    for my $bg (0 .. 15) {
Packit e6c7a3
        printf_checked('%2d %s', $bg, color("on_ansi$bg"));
Packit e6c7a3
        for my $fg (0 .. 15) {
Packit e6c7a3
            printf_checked('%s%d ', color("ansi$fg"), $fg);
Packit e6c7a3
        }
Packit e6c7a3
        print_checked(color('reset'), "\n");
Packit e6c7a3
    }
Packit e6c7a3
    print_checked("\n");
Packit e6c7a3
    return;
Packit e6c7a3
}
Packit e6c7a3
Packit e6c7a3
# Main routine.  Scan @ARGV for which test files to print out.
Packit e6c7a3
my %tests = (
Packit e6c7a3
    basic   => \&print_basic_test,
Packit e6c7a3
    bright  => \&print_bright_test,
Packit e6c7a3
    fg256   => \&print_fg256_test,
Packit e6c7a3
    bg256   => \&print_bg256_test,
Packit e6c7a3
    grey    => \&print_grey_test,
Packit e6c7a3
    ansi256 => \&print_ansi256_test,
Packit e6c7a3
);
Packit e6c7a3
for my $file (@ARGV) {
Packit e6c7a3
    if ($tests{$file}) {
Packit e6c7a3
        $tests{$file}->();
Packit e6c7a3
    } else {
Packit e6c7a3
        die "Unknown test file: $file\n";
Packit e6c7a3
    }
Packit e6c7a3
}
Packit e6c7a3
Packit e6c7a3
__END__
Packit e6c7a3
Packit e6c7a3
=for stopwords
Packit e6c7a3
fg256 bg256 RGB rgb000 rgb222 rgb555 ansi256 CPAN Starsinic Allbery grey
Packit e6c7a3
grey-scale
Packit e6c7a3
Packit e6c7a3
=head1 NAME
Packit e6c7a3
Packit e6c7a3
generate-colors - Generate color test patterns for ANSI terminal support
Packit e6c7a3
Packit e6c7a3
=head1 SYNOPSIS
Packit e6c7a3
Packit e6c7a3
B<generate-colors> I<type> [I<type> ...]
Packit e6c7a3
Packit e6c7a3
=head1 REQUIREMENTS
Packit e6c7a3
Packit e6c7a3
Perl 5.6 and Term::ANSIColor 4.00 or later.
Packit e6c7a3
Packit e6c7a3
=head1 DESCRIPTION
Packit e6c7a3
Packit e6c7a3
B<generate-colors> generates test and demonstration tables for ANSI color
Packit e6c7a3
and text attribute support for eight-color, sixteen-color, and 256-color
Packit e6c7a3
terminal emulators.  The I<type> command-line argument specifies a table
Packit e6c7a3
to print to standard output.  Multiple I<type> arguments can be specified,
Packit e6c7a3
and each of those tables will be printed in the order given.
Packit e6c7a3
Packit e6c7a3
The supported values of I<type> are:
Packit e6c7a3
Packit e6c7a3
=over 8
Packit e6c7a3
Packit e6c7a3
=item basic
Packit e6c7a3
Packit e6c7a3
The basic eight ANSI colors as both foreground and background, as well as
Packit e6c7a3
examples of bold for each color and a separate table of the non-color text
Packit e6c7a3
attributes supported by Term::ANSIColor.
Packit e6c7a3
Packit e6c7a3
=item bright
Packit e6c7a3
Packit e6c7a3
The "bright" ANSI colors from the sixteen-color palette (colors 8 through
Packit e6c7a3
15) on all possible color backgrounds (colors 0 through 15).
Packit e6c7a3
Packit e6c7a3
=item fg256
Packit e6c7a3
Packit e6c7a3
All of the 216 colors in the 256-color palette that are specified by three
Packit e6c7a3
RGB values (each from 0 to 5) as foreground colors, shown against three
Packit e6c7a3
possible backgrounds (rgb000, rgb222, and rgb555).
Packit e6c7a3
Packit e6c7a3
=item bg256
Packit e6c7a3
Packit e6c7a3
The same as C<fg256> except showing all of the background colors for three
Packit e6c7a3
different possible foreground colors (rgb000, rgb222, and rgb555).
Packit e6c7a3
Packit e6c7a3
=item grey
Packit e6c7a3
Packit e6c7a3
The 24 grey-scale colors in the 256-color palette, shown as both foreground
Packit e6c7a3
and background.
Packit e6c7a3
Packit e6c7a3
=item ansi256
Packit e6c7a3
Packit e6c7a3
The 256-color palette devotes the lowest 16 colors to duplicating the colors
Packit e6c7a3
from the sixteen-color palette.  This test table shows all sixteen as both
Packit e6c7a3
foreground and background colors, but using the 256-color escape sequence
Packit e6c7a3
format to specify them.  It's possible that this test will not work with
Packit e6c7a3
some emulators that support C<basic> and C<bright> if 256-color support is
Packit e6c7a3
not implemented.
Packit e6c7a3
Packit e6c7a3
=back
Packit e6c7a3
Packit e6c7a3
=head1 SEE ALSO
Packit e6c7a3
Packit e6c7a3
L<Term::ANSIColor>
Packit e6c7a3
Packit e6c7a3
This script is an example in the Term::ANSIColor distribution, available
Packit e6c7a3
from its web site at L<https://www.eyrie.org/~eagle/software/ansicolor/>
Packit e6c7a3
or from CPAN.
Packit e6c7a3
Packit e6c7a3
=head1 AUTHORS
Packit e6c7a3
Packit e6c7a3
Original script written by Kurt Starsinic.  It was restructured and updated
Packit e6c7a3
by Russ Allbery to add the C<basic> and C<bright> test tables.
Packit e6c7a3
Packit e6c7a3
=head1 COPYRIGHT AND LICENSE
Packit e6c7a3
Packit e6c7a3
Copyright 2012 Russ Allbery <rra@cpan.org>.  Copyright 2012 Kurt Starsinic
Packit e6c7a3
<kstarsinic@gmail.com>.  This program is free software; you may
Packit e6c7a3
redistribute it and/or modify it under the same terms as Perl itself.
Packit e6c7a3
Packit e6c7a3
=cut