File Coverage

blib/lib/Term/ExtendedColor/TTY.pm
Criterion Covered Total %
statement 24 27 88.8
branch 3 6 50.0
condition 1 3 33.3
subroutine 6 6 100.0
pod 1 1 100.0
total 35 43 81.4


line stmt bran cond sub pod time code
1             package Term::ExtendedColor::TTY;
2 2     2   135675 use strict;
  2         15  
  2         77  
3              
4             BEGIN {
5 2     2   14 use Exporter;
  2         4  
  2         103  
6 2     2   15 use vars qw($VERSION @ISA @EXPORT_OK);
  2         4  
  2         251  
7              
8 2     2   8 $VERSION = '0.030';
9 2         59 @ISA = qw(Exporter);
10              
11 2         86 @EXPORT_OK = qw(
12             set_tty_color
13             );
14             }
15 2     2   14 use Carp qw(croak);
  2         2  
  2         753  
16              
17              
18             my %color_indexes = (
19             0 => 'P0', #black
20             1 => 'P8', # dark grey
21             2 => 'P1', # dark red
22             3 => 'P9', # red
23             4 => 'P2', # dark green
24             5 => 'PA', # green
25             6 => 'P3', # brown
26             7 => 'PB', # yellow
27             8 => 'P4', # darkblue
28             9 => 'PC', # blue
29             10 => 'P5', # dark magenta
30             11 => 'PD', # magenta
31             12 => 'P6', # dark cyan
32             13 => 'PE', # cyan
33             14 => 'P7', # dark white
34             15 => 'PF', # white
35             );
36              
37              
38             sub set_tty_color {
39 1     1 1 94 my $map = shift;
40              
41 1 50       6 ref($map) eq 'HASH' or croak("set_tty_color() expects an hashref");
42              
43 1         3 my %results;
44              
45 1         2 for my $index(sort{ $a <=> $b } keys(%{$map})) {
  0         0  
  1         5  
46 1 50 33     9 if( ($index < 0) or ($index > 15) ) {
47 0         0 croak("'$index': color index must be between 0 and 15, inclusive\n");
48             }
49 1 50       7 if($map->{$index} !~ m/^(?:[A-Za-z0-9]{6}$)/) {
50 0         0 croak(
51             "'$map->{$index}' is not a valid hexadecimal color representation\n"
52             );
53             }
54              
55             $results{$index} = sprintf("\e]%s",
56             $color_indexes{$index}
57 1         8 . $map->{$index}, #P0ffffff
58             );
59             }
60              
61 1         3 return \%results;
62             }
63              
64              
65             1;
66              
67              
68             __END__