#!/usr/bin/perl
# vim:ft=perl:et:
use vars qw($VERSION);
my $APP  = 'rgbterm';
$VERSION = '0.022';

use strict;
use Getopt::Long qw(GetOptions);
use Pod::Usage qw(pod2usage);
use Term::ExtendedColor qw(fg);
use Term::ExtendedColor::Xresources qw(get_xterm_color);


my($opt_color);
GetOptions(
  'c|color'   => \$opt_color,
  'h|help'    => sub { pod2usage(verbose => 1)  and exit; },
  'm|man'     => sub { pod2usage(verbose => 1)  and exit; },
  'v|version' => sub { print "$APP v$VERSION\n" and exit; },
);

my $colors = shift // 255;

$colors =~ /^--?h(?:elp)?/ and print "Usage: rgbterm (int) [-h]\n" and exit 0;

get_rgb($colors);

sub get_rgb {
  my $upper = shift;

  if( ($upper < 0) or ($upper > 255) ) {
    print STDERR "Need a value between 0 and 255, inclusive\n" and exit 1;
  }
  my $ansi = get_xterm_color({
      index => [0 .. $upper],
      type  => 'hex'
    });


  for my $index(0 .. $upper) {
    printf("%s\n",
      ($opt_color)
        ? sprintf("%s.color%03d: #%s",
            $ENV{HOSTNAME}, $index, fg($index, $ansi->{$index}->{rgb}))
        : sprintf("%s.color%03d: #%s",
            $ENV{HOSTNAME}, $index, $ansi->{$index}->{rgb})
      );
  }
}


__END__

=pod

=head1 NAME

rgbterm - show RGB values of defined terminal colors

=head1 OPTIONS

  -c, --color     show the color values in color

  -h, --help      show the help and exit
  -v, --version   show the version info and exit
  -m, --man       show the manual and exit

=head1 AUTHOR

  Magnus Woldrich
  CPAN ID: WOLDRICH
  magnus@trapd00r.se
  http://japh.se

=head1 CONTRIBUTORS

None required yet.

=head1 COPYRIGHT

Copyright 2010, 2011 B<rgbterm>s L</AUTHOR> and L</CONTRIBUTORS> as listed above.

=head1 LICENSE

This program is free software; you may redistribute it and/or modify it under
the same terms as Perl itself.

=cut
