]> git.nbdom.net Git - nb.git/commitdiff
Create
authorNicolas Boisselier <nicolas.boisselier@gmail.com>
Thu, 20 Apr 2023 21:08:16 +0000 (23:08 +0200)
committerNicolas Boisselier <nicolas.boisselier@gmail.com>
Thu, 20 Apr 2023 21:08:16 +0000 (23:08 +0200)
lib/perl/NB/Color.pm [new file with mode: 0644]

diff --git a/lib/perl/NB/Color.pm b/lib/perl/NB/Color.pm
new file mode 100644 (file)
index 0000000..246fa52
--- /dev/null
@@ -0,0 +1,99 @@
+package NB::Color;
+use strict;
+#use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION $VAR1);
+our $VERSION = '0.01';
+require Exporter;
+our @ISA = qw(Exporter);  # inherit all of Exporter's methods
+
+our @EXPORT_OK = qw/
+       color
+/;
+our @EXPORT = qw//;
+push(@EXPORT, @EXPORT_OK);
+our %EXPORT_TAGS;
+$EXPORT_TAGS{all} = [@EXPORT_OK];
+
+our %BG = (
+       'black'   => 40,
+       'red'     => 41,
+       'green'   => 42,
+       'yellow'  => 43,
+       'blue'    => 44,
+       'magenta' => 45,
+       'cyan'    => 46,
+       'white'   => 47,
+       'default' => 49,
+);
+
+our %FG = (
+       'black'   => 30,
+       'red'     => 31,
+       'green'   => 32,
+       'yellow'  => 33,
+       'blue'    => 34,
+       'magenta' => 35,
+       'cyan'    => 36,
+       'white'   => 37,
+       'default' => 39,
+);
+
+our %EFFECT = (
+       'none'          => 0, 
+       'bold'          => 1, 
+       'pale'          => 2, 
+       'evidence'      => 3, 
+       'underline'     => 4, 
+       'blink'         => 5, 
+       'invert'        => 7, 
+       'cache'         => 8, 
+       'normal'        => 22,
+       'no_evidence'   => 23,
+       'no_souligne'   => 24,
+       'no_underline'  => 24,
+       'no_clignotant' => 25,
+       'no_blink'      => 25,
+       'no_inverse'    => 27,
+       'no_invert'     => 27,
+);
+
+sub color {
+#------------------------------------------------------------------------------
+# NB 18.11.01 
+#------------------------------------------------------------------------------
+my ($txt,$hparam) = @_;
+
+       my $new = "";
+       $hparam->{bg} ||= 'default';
+       $hparam->{fg} ||= 'default';
+       $hparam->{effect} ||= 'normal';
+       #die $hparam->{num};
+
+  if (!$hparam->{num} or $hparam->{num} == 2) {
+
+    $new .= 
+      "\e["
+      . $BG{$hparam->{bg}}
+      . ";".$FG{$hparam->{fg}}
+      . ";".$EFFECT{$hparam->{effect}}
+      . "m".$txt
+      . "\e[0m"
+    ;
+       $new .= " | " if $hparam->{num} == 2;
+
+  }
+
+  if ($hparam->{num}) {
+
+    $new .= 
+      '\e['
+      . $BG{$hparam->{bg}}
+      . ";".$FG{$hparam->{fg}}
+      . ";".$EFFECT{$hparam->{effect}}
+      . "m".$txt
+      . '\e[0m'
+    ;
+
+  }
+
+       return $new;
+}