#!/usr/bin/perl
#
# $Revision: 1.1 $ $Date: 2019-04-29 16:01:46-04 $
# $Source: /doc/html/htdocs/color/RCS/mkcolorbyname,v $
# $Host: furbag.home.arpa $
# $UUID: 0004ffaf-bd8b-4076-a718-aef7973f8c12 $
#
# 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, $total, $y);
my (@color, @hexno, @name, @cname);

# 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[*/
span.c1 {font-family: Verdana, Arial, Helvetica; font-size: 120%}
/*]]>*/
  </style>
  </head>
  <body>
  <h1>Color Samples</h1>
  <hr />
  <table summary="color" border="0" width="100%" align="left" cellpadding="5">
EndHeader

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

    $cname[$k] = $name[$k];
    $cname[$k] =~ s/\*//;

    $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="$cname[$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="$cname[$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="$cname[$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>
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));
}
