#!/usr/bin/perl
#
# $Revision: 1.10 $ $Date: 2019-04-29 16:14:20-04 $
# $Source: /doc/html/htdocs/color/RCS/mkcolor,v $
# $Host: furbag.home.arpa $
# $UUID: 68203a34-c2b7-4b05-96ce-07ab6b451129 $
#
# Read list of colors, create an HTML page displaying them in a table.
# Table has three sets of three columns, holding the hex definition,
# the color name, and a sample.  We use three columns in case the color
# sample would make any text too hard to read.

use strict;
use warnings;
use File::Basename;

# Arguments, variables.
$ENV{'PATH'} = join ":", qw(/bin /usr/bin /usr/local/bin /opt/sfw/bin);

my $myname = basename($0);
$myname =~ s/\.\w*$//;    # strip any extension

my $file = shift(@ARGV) || die "need a list of colors\n";
my $fh;
my ($h, $i, $inc, $j, $k, $n, $total, $y);
my (@color, @hexno, @name);

# Real work starts here.
open($fh, "$file") || die "$file: $!\n";

print <<"EndHeader";
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
  <title>Color Samples</title>
  <style type="text/css">
    /*<![CDATA[*/
    body    { width: 90%; margin-left: 5%; }
    span.c1 { font-family: Verdana, Arial, Helvetica; font-size: 120%; }
    /*]]>*/
  </style>
  </head>
  <body>
  <h2>Color Samples</h2>
  <p>Colors in bold can be displayed using the color name as
  well as the hex value.</p>
  <hr />
  <table summary="color" border="0" width="100%" align="left" cellpadding="5">
EndHeader

# Colors printed here.
$k = 0;
while (<$fh>) {
    $k++;
    ($h, $n) = (split)[0,4];

    if ($n =~ s/\*//) {
        $name[$k] = "<strong>$n</strong>\n";
    }
    else {
        $name[$k] = "$n";
    }

    $h =~ tr/a-z/A-Z/;
    $hexno[$k] = "<span class=\"c1\">" . "#$h" . "</span>";
    $color[$k] = "#$h";
}

my $blankline = <<"EndBlank";
  <tr>
  <td colspan="4"><hr /></td>
  <td colspan="4"><hr /></td>
  <td colspan="4"><hr /></td>
  </tr>
EndBlank

$total = $k;
$inc   = int($total / 3) + 1;
$i     = 0;

while ($i < $inc) {
    $i++;
    $j = $i + $inc;
    $k = $j + $inc;
    print "  <tr>\n";

    print <<"EndEntry" if $hexno[$i];
    <td width="6%" align="right">$hexno[$i]</td>
    <td width="6%" align="right">$name[$i]</td>
    <td width="6%" bgcolor="$color[$i]">\&nbsp;</td>
    <td width="13%">\&nbsp;</td>

EndEntry

    print <<"EndEntry" if $hexno[$j];
    <td width="6%" align="right">$hexno[$j]</td>
    <td width="6%" align="right">$name[$j]</td>
    <td width="6%" bgcolor="$color[$j]">\&nbsp;</td>
    <td width="13%">\&nbsp;</td>

EndEntry

    print <<"EndEntry" if $hexno[$k];
    <td width="6%" align="right">$hexno[$k]</td>
    <td width="6%" align="right">$name[$k]</td>
    <td width="6%" bgcolor="$color[$k]">\&nbsp;</td>
    <td width="13%">\&nbsp;</td>
EndEntry

    print "  </tr>\n";
    print "$blankline" if $i % 10 == 0;
}

# Footer.
my $adate = arpadate();

print <<"EndTrailer";
  </table>
  <p>\&nbsp;<hr /></p>
  <table summary="footer" border="0" width="100%">
    <tr>
      <td align="left" width="40%">
        <em>Generated by $myname from $file</em>
      </td>
      <td align="right" width="40%">
        <em>Last update: $adate</em>
      </td>
    </tr>
  </table>
</body>
</html>
EndTrailer

close($fh);
exit(0);

#---------------------------------------------------------------------
# Accept an optional date in total seconds since the epoch,
# and write it in ARPA-mail standard form:
#       Fri, 28 Mar 1997 15:40:57 -0500

sub arpadate {
    use POSIX qw(strftime);
    my $t = shift || time();
    return strftime("%a, %d %b %Y %T %z", localtime($t));
}
