File Coverage

blib/lib/Term/ANSIColor/Concise/Table.pm
Criterion Covered Total %
statement 20 72 27.7
branch 0 2 0.0
condition 0 2 0.0
subroutine 7 14 50.0
pod 4 5 80.0
total 31 95 32.6


line stmt bran cond sub pod time code
1             # -*- indent-tabs-mode: nil -*-
2              
3             package Term::ANSIColor::Concise::Table;
4              
5             our $VERSION = "3.02";
6              
7 1     1   1653 use v5.14;
  1         3  
8 1     1   4 use utf8;
  1         1  
  1         7  
9              
10 1     1   26 use Exporter 'import';
  1         2  
  1         66  
11             our @EXPORT = qw();
12             our @EXPORT_OK = qw(
13             colortable colortable6 colortable12 colortable24
14             );
15             our %EXPORT_TAGS = (all => [ @EXPORT_OK ]);
16              
17 1     1   4 use Carp;
  1         2  
  1         74  
18 1     1   5 use Data::Dumper;
  1         1  
  1         64  
19             $Data::Dumper::Sortkeys = 1;
20              
21 1     1   5 use Term::ANSIColor::Concise qw(ansi_color map_to_256);
  1         1  
  1         46  
22 1     1   4 use List::Util qw(min);
  1         1  
  1         2097  
23              
24             sub colortable6 {
25 0     0 1   colortableN(
26             step => 6,
27             string => " ",
28             line => 2,
29             x => 1, y => 1, z => 1,
30             @_
31             );
32             }
33              
34             sub colortable12 {
35 0     0 1   colortableN(
36             step => 12,
37             string => " ",
38             x => 1, y => 1, z => 2,
39             @_
40             );
41             }
42              
43             # use charnames ':full';
44              
45             sub colortable24 {
46 0     0 1   colortableN(
47             step => 24,
48             string => "\N{U+2580}", # "\N{UPPER HALF BLOCK}",
49             shift => 1,
50             x => 1, y => 2, z => 4,
51             @_
52             );
53             }
54              
55             sub colortableN {
56 0     0 0   my %arg = (
57             shift => 0,
58             line => 1,
59             row => 3,
60             @_);
61 0           my @combi = do {
62 0           my @default = qw( XYZ YZX ZXY YXZ XZY ZYX );
63 0 0         if (my @s = $arg{row} =~ /[xyz]{3}/ig) {
64 0           @s;
65             } else {
66 0           @default[0 .. $arg{row} - 1];
67             }
68             };
69             my @order = map {
70 0           my @ord = map { { X=>0, Y=>1, Z=>2 }->{$_} } /[XYZ]/g;
  0            
71 0     0     sub { @_[@ord] }
72 0           } map { uc } @combi;
  0            
  0            
73 0           binmode STDOUT, ":utf8";
74 0           for my $order (@order) {
75             my $rgb = sub {
76             sprintf "#%02x%02x%02x",
77 0     0     map { map_to_256($arg{step}, $_) } $order->(@_);
  0            
78 0           };
79 0           for (my $y = 0; $y < $arg{step}; $y += $arg{y}) {
80 0           my @out;
81 0           for (my $z = 0; $z < $arg{step}; $z += $arg{z}) {
82 0           for (my $x = 0; $x < $arg{step}; $x += $arg{x}) {
83 0           my $fg = $rgb->($x, $y, $z);
84 0           my $bg = $rgb->($x, $y + $arg{shift}, $z);
85 0           push @out, ansi_color "$fg/$bg", $arg{string};
86             }
87             }
88 0           print((@out, "\n") x $arg{line});
89             }
90             }
91             }
92              
93             sub colortable {
94 0   0 0 1   my $width = shift || 144;
95 0           my $column = min 6, $width / (4 * 6);
96 0           for my $c (0..5) {
97 0           for my $b (0..5) {
98 0           my @format =
99             ("%d$b$c", "$c%d$b", "$b$c%d", "$b%d$c", "$c$b%d", "%d$c$b")
100             [0 .. $column - 1];
101 0           for my $format (@format) {
102 0           for my $a (0..5) {
103 0           my $rgb = sprintf $format, $a;
104 0           print ansi_color "$rgb/$rgb", " $rgb";
105             }
106             }
107 0           print "\n";
108             }
109             }
110 0           for my $g (0..5) {
111 0           my $grey = $g x 3;
112 0           print ansi_color "$grey/$grey", sprintf(" %-19s", $grey);
113             }
114 0           print "\n";
115 0           for ('L00' .. 'L25') {
116 0           print ansi_color "$_/$_", " $_";
117             }
118 0           print "\n";
119 0           for my $rgb ("RGBCMYKW", "rgbcmykw") {
120 0           for my $c (split //, $rgb) {
121 0           print ansi_color "$c/$c", " $c ";
122             }
123 0           print "\n";
124             }
125 0           for my $rgb (qw(500 050 005 055 505 550 000 555)) {
126 0           print ansi_color "$rgb/$rgb", " $rgb";
127             }
128 0           print "\n";
129             }
130              
131             1;
132              
133             __END__