#!/usr/bin/perl
#
# Name:
#	im-hax-color-2-hash.pl.
#
# Purpose:
#	Read in the file created by
#	im-hax-color-2-config-1.pl,
#	or by im-hax-color-2-config-1.pl,
#	and store the data in a Perl hash.
#	Then write the data to a text file,
#	sorted various ways.
#
#	Thus the output file can be used as a
#	reference doc to convert between color
#	names, hex value and RGB, or the hash can
#	be used in any program to convert similarly.
#
#	See im-hax-color-2-config-1.pl for the code
#	to create the input file.
#	Or
#	See im-hax-color-2-config-2.pl for the code
#	to create the input file.
#
# 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
#
# 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:
#	im-hax-color-2-config-1.eval
#	(Output by im-hax-color-2-config-1.pl).
#	Or
#	im-hax-color-2-config-2.eval
#	(Output by im-hax-color-2-config-2.pl).
#
# Output files:
#	im-hax-color-2-config.txt.
#
# 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 Image::Magick;

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

sub by_rgb
{
	my(@a) = split(/,/, $a);
	my(@b) = split(/,/, $b);

	sprintf('%03d%03d%03d', @a)
	cmp
	sprintf('%03d%03d%03d', @b);

}	# End of by_rgb.

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

sub load_config
{
	my($file_name) = @_;

	my(%color, $eval);

	open(INX, $file_name)
	|| die("Can't open($file_name): $!");

	{
		local $/;

		$eval = <INX>;
	}

	eval $eval;

	die($@) if ($@);

	return \%color;

}	# End of load_config.

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

my($dir_name)		= '/temp';
my($im_version)		= $Image::Magick::VERSION;

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

print "ImageMagick V $im_version. \n";
print "\n";

my($input_file_name)	=
	'im-hax-color-2-config-1.eval';
my($output_file_name)	=
	'im-hax-color-2-config-1.txt';
my($color)				=
	load_config($input_file_name);

open(OUT, "> $output_file_name")
|| die("Can't open(> $output_file_name): $!");

print OUT "ImageMagick V $im_version. ",
		"Color/Hex/RGB list. \n";
print OUT "\n";

# Sort by color name.

print OUT "Table 1: Sorted by color name: \n";
print OUT "\n";

for (sort grep{/^[a-z]/} keys %$color)
{
	print OUT "Color: $_. Hex: ",
		"$$color{$_}{'hex'}. ",
		"RGB: $$color{$_}{'rgb'}. \n";
}

print OUT "\n";

# Sort by hex value.

print OUT "Table 2: Sorted by hex value: \n";
print OUT "\n";

for (sort grep{/^#/} keys %$color)
{
	print OUT "Hex: $_. Color: ",
		"$$color{$_}{'color'}. ",
		"RGB: $$color{$_}{'rgb'}. \n";
}

print OUT "\n";

# Sort by RGB value.

print OUT "Table 3: Sorted by RGB value: \n";
print OUT "\n";

for (sort by_rgb grep{/^\d/} keys %$color)
{
	print OUT "RGB: $_. Color: ",
		"$$color{$_}{'color'}. ",
		"Hex: $$color{$_}{'hex'}. \n";
}

close(OUT);

print "Wrote: $output_file_name. \n";
print "\n";
print "Testing... \n";

my($color_name)	= 'darkslategrey';
my($hex)		= 'Unknown';
my($rgb)		= 'Unknown';

if ($$color{$color_name})
{
	$hex	= $$color{$color_name}{'hex'};
	$rgb	= $$color{$color_name}{'rgb'};
}

print "Color: $color_name. Hex: $hex. ",
	"RGB: $rgb. \n";

$hex		= '#32cd32';
$color_name	= 'Unknown';
$rgb		= 'Unknown';

if ($$color{$hex})
{
	$color_name	= $$color{$hex}{'color'};
	$rgb		= $$color{$hex}{'rgb'};
}

print "Hex: $hex. Color: $color_name. ",
	"RGB: $rgb. \n";

$rgb		= '72,209,204';
$color_name	= 'Unknown';
$hex		= 'Unknown';

if ($$color{$rgb})
{
	$color_name = $$color{$rgb}{'color'};
	$hex		= $$color{$rgb}{'hex'};
}

print "RGB: $rgb. Color: $color_name. ",
	"Hex: $hex. \n";
