#!/usr/bin/perl
#
# AsciiColor2 - ascii art colorizer
# (c) polprog 2019 - https://polprog.net
# released on 3-clause BSD license

use warnings;
use strict;
use utf8;

unless ($#ARGV >=0 ){
    print "Usage: $0 [-bmrh] <artfile>\n";
    exit 1;
}

if($#ARGV == 0 && $ARGV[0] =~ /^-/ && !($ARGV[0] =~ /^-h/)){
    print "No file specified.\n";
    exit 1;
}


# +------+------------+
# | DARK | LIGHT/BOLD |
# +------+------------+
# | 0    | 8 black    |
# | 1    | 9 red      |
# | 2    | A green    |
# | 3    | B yellow   |
# | 4    | C blue     |
# | 5    | D magenta  |
# | 6    | E cyan     |
# | 7    | F white    |
# +------+------------+

my $artname;
if($ARGV[0] =~ /^-/){
    $artname = $ARGV[1];
}else{
    $artname = $ARGV[0];
}

my $bold = 0;
my $debug = 0;
my $reverse = 0;
my $mode = "ansi";

if($ARGV[0] =~ /-.*d/){
    print "debug on\n";
    $debug = 1;
}

if($ARGV[0] =~ /-.*h/){
    print "";
    print "Usage: $0 [-dhbmarw] <artfile>\n";
    print "The script takes artfile.txt and artfile.map files as argument\n";
    print "and prints out the ascii art in color.\n";
    print "the map file name is generated from the txt parameter\n";
    print "The map file contains numbers 0-9 and letters a-f\n";
    print "that define colour, in place of the ascii art characters\n";
    print "By default ANSI escape codes are used\n";
    print "Options are checked in the order listed\n";
    print "  -a\tUse ANSI escape codes (default)\n";
    print "  -b\tUse bold text for light colours (mIRC)\n";
    print "  -h\tPrint help and exit\n";
    print "  -m\tUse mIRC formatting codes\n";
    print "  -r\tEnable reverse video\n";
    print "  -d\tEnable debug info\n";
    print "  -w\tEnable web mode (outputs partial html)\n";
    print "Map file color values:
   +------+------------+
   | DARK | LIGHT/BOLD |
   +------+------------+
   | 0    | 8 black    |
   | 1    | 9 red      |
   | 2    | A green    |
   | 3    | B yellow   |
   | 4    | C blue     |
   | 5    | D magenta  |
   | 6    | E cyan     |
   | 7    | F white    |
   +------+------------+\n";
    print "AsciiColor v4.0 by polprog, 2019\n";
    print "https://polprog.net/ released on 3BSD\n";
    exit 0;
}


if($ARGV[0] =~ /-.*b/){
    print "bold on\n" if $debug;
    $bold = 1;
}

if($ARGV[0] =~ /-.*m/){
    print "mirc on\n" if $debug;
    $mode = "mirc";
}


if($ARGV[0] =~ /-.*w/){
    print "web on\n" if $debug;
    $mode = "web";
}


if($ARGV[0] =~ /-.*r/){
    print "reverse video on\n" if $debug;
    $reverse = 1;
}


print "artname = $artname\n" if $debug;

my @ansi_colors = (30, 31, 32, 33, 34, 35, 36, 37,  #normal
		   90, 91, 92, 93, 94, 95, 96, 97); #bright/bold

my @web_colors = 
("black", "maroon", "green", "darkgoldenrod", "navy", "purple", "cyan", "silver",
 "gray", "red", "lime", "yellow", "blue", "fuchsia", "aqua", "white");

print "Opening map file: " . $artname =~ s/\.txt$/\.map/r . " ...\n" if $debug;
open(my $mapfile, '<', $artname =~ s/\.txt$/\.map/r ) or die $!;
open(my $artfile, '<', $artname) or die $!;


print "<div 
style='display:block; font-family: Fixedsys,monospace; white-space: pre;
 background-color: black; padding: 10px;'><span>" if $mode eq "web";

while(my $artl = <$artfile>, my $colorl = <$mapfile>){
    chomp $artl;
    chomp $colorl;
    my $lastcolor = -1;
    #iterate char by char over both files
    while($artl =~ /(.)/g){
	my $artchar = $1;
	$colorl =~ /(.)/g;
	my $colorchar = $1;
	my $color;
	
	if($mode eq "ansi"){
	    if ($colorchar =~/[0-9a-fA-F]/) {
		$color = $ansi_colors[hex($colorchar)];
		$color += 10 if $reverse;
	    }else { $color = $lastcolor;}
	    
	    
	    unless($color == $lastcolor){
		
		# ANSI MODE
		if($reverse){
		    if($bold && $color >= 90){
			print("\033[1;30;" . $color . "m");    
		    }else{
			print("\033[0;30;" . $color . "m");
		    }
		}else{
		    if($bold && $color >= 90){
			print("\033[1;" . $color . "m");
		    }else{
			print("\033[0;" . $color . "m");
		    }
		}
		
		$lastcolor = $color;
	    }
	    print($artchar);
	} elsif($mode eq "mirc") {
	    if ($colorchar =~/[0-9a-fA-F]/) {
		$color = hex($colorchar);
	    }else { $color = $lastcolor;}
	    
	    
	    unless($color == $lastcolor){
		
		# MIRC MODE
		if($bold && $color >= 90){
		    if($reverse){
			print("\003B\0031," . $color);
		    }else{
			print("\003B\003" . $color);
		    }
		}else{
		    if($reverse){
			print("\0031," . $color);
		    }else{
			print("\003" . $color);
		    }
		}
		
		$lastcolor = $color;
	    }
	    print($artchar);
	} elsif ($mode eq "web"){
	    #Generating HTML with perl, aaaaaaa!!!
	    if ($colorchar =~/[0-9a-fA-F]/) {
		$color = $web_colors[hex($colorchar)];
	    }else { $color = $lastcolor;}
	    
	    
	    unless($color eq $lastcolor){
		print("</span>");
		# web MODE
		print("<span ");
		if($reverse){
		    print("style='background-color: $color; color: black;");
		}else{
		    print("style='color: $color; background-color: black;");
		}
		print("font-weight: bold;") if $bold;
		print("'>");
		$lastcolor = $color;
	    }
	    print($artchar);

	}
    }
    
    print "\n";
}

print "</span></div>" if $mode eq "web";

