#!/usr/bin/perl 
#
# hexcol : simple perl 5 script to calculate hex notation for web colours.
#
# Requires Perl 5 (you may need to change the "#!/usr/bin/perl" line above
# to reflect where perl is installed on your machine) and the CGI.pm 
# module.  Both of these can be obtained from the CPAN archives - see
# the Perl Language Home Page (http://www.perl.com/perl) or contact your
# local sysadmin or Perl guru for more details
#
# You may freely copy, distribute, modify and use this script on your own 
# web site as long as the original author attribution remains intact.  
# See message below.
#
# Copyright (C) 1996,97 Andy Wardley.  All Rights Reserved.
#


use lib '/home/abw/lib';
use CGI qw(:all);

$query = new CGI;

print $query->header();
print $query->start_html(
		-title       => "Andy's Magic Hex Colour Converter",
		-author      => 'abw@peritas.com',
		-bgcolor     => '#ffffff');

print "\n<center>\n\n";

print $query->h1("Hex Colour Converter"), "\n\n";


#
# This is the original author attribution.  You may not remove this.
# If you change the script significantly, you may want to add your own
# name and change the line below that reads "by $author $email" to "based 
# on the original by $author $email".  That's cool, just don't pretend I
# never wrote it :-)
#

$author = "<a href=\"http://www.peritas.com/~abw\">Andy Wardley</a>";
$email  = "<a href=\"mailto:abw\@peritas.com\">&lt;abw\@peritas.com&gt;</a>";
print     "<font size=\"-1\">by $author $email</font>\n<p>\n";


#
# print a help message and the RGB form
#

print "Enter the RGB triple in decimal (0-255) and click on \"Convert Me!\"\n";

print $query->startform('post', $query->url),
	"\nRed: ", 
	$query->textfield(
		-name        => 'red',
		-default     => '192',
		-size        => 3,
		-maxlength   => 3),
	"\nGreen: ",
	$query->textfield(
		-name        => 'green',
		-default     => '192',
		-size        => 3,
		-maxlength   => 3),
	"\nBlue: ",
	$query->textfield(
		-name        => 'blue',
		-default     => '192',
		-size        => 3,
		-maxlength   => 3),
	"\n\n<p>\n\n",
	$query->submit(
		-name        => 'convert',
		-value       => ' Convert Me! '),
	"\n\n",
	$query->endform,
	"\n<p>\n\n";


#
# if this is a form submission, convert and display the colour
#

if ($query->param()) {

	print "<hr width=75%>\n\n";
	print $query->h1("Hex Colour Conversion"), "\n\n";

	$red = $query->param('red');
	$red = $red > 255 ? 255 : $red < 0 ? 0 : $red;

	$green = $query->param('green');
	$green = $green > 255 ? 255 : $green < 0 ? 0 : $green;

	$blue = $query->param('blue');
	$blue = $blue > 255 ? 255 : $blue < 0 ? 0 : $blue;

	print("<table border=0 cellpadding=10 cellspacing=5>\n");
	print("<tr bgcolor=\"#cccccc\"><th> </th>");
	print("<th>Decimal</th><th>Hexadecimal</th></tr>\n");

	print("<tr bgcolor=\"#ccaaaa\" align=right><th align=left>Red</th>");
	printf("<td>%3d</td><td>%02X</td></tr>\n", $red, $red);

	print("<tr bgcolor=\"#aaccaa\" align=right><th align=left>Green</th>");
	printf("<td>%3d</td><td>%02X</td></tr>\n", $green, $green);

	print("<tr bgcolor=\"#aaaacc\" align=right><th align=left>Blue</th>");
	printf("<td>%3d</td><td>%02X</td></tr>\n", $blue, $blue);

	print("<tr bgcolor=\"#cccccc\" align=right><th align=left>Colour</th>");
	printf("<td colspan=2>#%02X%02X%02X</td></tr>\n", $red, $green, $blue);

	print("<tr bgcolor=\"#ffffff\" align=center><td colspan=3>");
	printf("<font color=\"#%2x%2x%2x\" size=\"+2\">", $red, $green, $blue);
	print("Colour Sample</font></td></tr>\n\n");

	printf("<tr bgcolor=\"#%2x%2x%2x\" align=center>", $red, $green, $blue);
	print("<td colspan=3> SAMPLE </td></tr>\n\n");

	print("</table>\n\n<p>\n\n");

}


print $query->end_html;





