#!/usr/bin/perl
#
# Name:
#	im-hax-color-2-config-1.pl.
#
# Purpose:
#	Convert ImageMagick doc file color.html into
#	a Perl hash which is then written to a file
#	with Data::Dumper, so that it can be read in
#	and eval-ed into code, so as to provide
#	a 3-way look-up table.
#
#	See im-hax-color-2-hash.pl for the code to
#	read the output back in.
#
#	See im-hax-color-2-config-2.pl for code to
#	read colors.mgk. This program outputs in the
#	same format as im-hax-color-2-config-1.pl.
#
# Note:
#	o Store data all 3 ways per color entry in
#	color.html, ie as:
#		'aliceblue'		=>
#		{
#			hex	=> '#f0f8ff',
#			rgb	=> '240,248,255'
#		},
#		'#f0f8ff'		=>
#		{
#			color_name	=> 'aliceblue',
#			rgb			=> '240,248,255'
#		},
#		'240,248,255'	=>
#		{
#			color_name	=> 'aliceblue',
#			hex			=> '#f0f8ff'
#		}
#	o Hence:
#		First char of key	Interpretation
#		----------------------------------
#		a .. z				Color name
#		#					Hex value
#		0 .. 9				RGB values
#	o Lower case is used everywhere
#	o Spaces are removed everywhere,
#	eg from RGB: '240, 255, 255'
#
# V 1.00 9-Jun-2002
# -----------------
# o Initial version
#
# Test environment:
#	OS: WinNT 4 SP 6a
#	Web server: Apache V 1.3.26
#	IM: V 5.4.7
#
# Input file:
#	color.html
#	(http://www.imagemagick.org/www/color.html)
#
# Output file:
#	im-hax-color-2-config-1.eval.
#
# Note:
#	o tab = 4 spaces || die.
#	o Text reformatted to max 50 chars wide
#		so source can be converted to PDF
#
# Author:
#	Ron Savage <ron@savage.net.au>
#	http://savage.net.au/ImageMagick.html
#
# Licence:
#	Copyright (c) 1999-2002 Ron Savage
#
#	All Programs of mine are
#	'OSI Certified Open Source Software';
#	you can redistribute them and/or modify them
#	under the terms of the Artistic License,
#	a copy of which is available at:
#	http://www.opensource.org/licenses/index.html

use strict;
use warnings;

use Data::Dumper;
use DBD::AnyData;

# -----------------------------------------------

my($dir_name) = '/temp';

chdir($dir_name)
|| die("Can't chdir($dir_name): $!");

my($output_file_name)	=
	'im-hax-color-2-config-1.eval';
my($table_name)			= 'color';
my($table_type)			= 'HTMLtable';
my($table_file)			=
	'/ImageMagick/www/color.html';
my($row_names)			= 'Name,Color,RGB,Hex';
my($dbh)				=
	DBI -> connect('dbi:AnyData(RaiseError=>1):');

$dbh -> func($table_name, $table_type,
	$table_file, {col_names => $row_names},
	'ad_catalog');

my($sql) = "select * from $table_name";
my($sth) = $dbh -> prepare($sql);

$sth -> execute();

my($data, $value, $rgb, %color);

while ($data = $sth -> fetchrow_hashref() )
{
	# Table is effectively double-spaced.
	# So we examine the data, and skip any which
	# is null. Use this opportunity to lower case
	# and zap spaces.

	$value = join('', map
	{
		$$data{$_} = lc $$data{$_};
		$$data{$_} =~ tr/ //d;
		$$data{$_}
	} keys %$data);

	next if (! $value);

	$color{$$data{'Color'} }	=
	{
		hex	=> $$data{'Hex'},
		rgb	=> $$data{'RGB'}
	};
	$color{$$data{'Hex'} }		=
	{
		color	=> $$data{'Color'},
		rgb		=> $$data{'RGB'}
	};
	$color{$$data{'RGB'} }		=
	{
		color	=> $$data{'Color'},
		hex		=> $$data{'Hex'}
	};

	#print map{"$_ => $$data{$_}. "} keys %$data;
	#print "\n";
}

$dbh -> disconnect();

$Data::Dumper::Indent = 1;
open(OUT, "> $output_file_name")
|| die("Can't open(> $output_file_name): $!");
print OUT Data::Dumper -> Dump([\%color],
		[qw(*color)]);
close(OUT);
print "Wrote: $output_file_name. \n";
