#!/usr/bin/perl
#
# swatches.pl
# 06/25/95 John Miller
# jcmiller@jmit.com
# http://www.jmit.com
#
# This script is handy for generating the RGB Hex codes needed to 
# specify background and text colors in Netscape. 
#
# This script displays a panel of color "swatches", similar to
# Adobe Photoshop. (These swatches are 12x12 pixel color squares.)
# The user clicks on the desired color swatch, 
# and this script returns the RGB values (in hex) that represent
# the chosen color.   
#
# This script is recursive, in that it generates 
# HTML to call itself. It can be called from a URL like:
#   <A HREF="/cgi-bin/swatches.pl"><IMG SRC="images/swatches.gif" ISMAP>
# The ISMAP parameter results in the pixel coordinates being passed in 
# environment variable QUERY_STRING.  
#
# Alternatively, a link can be coded like:
#    <A REF="http://www.malamute.calstate.edu/cgi-bin/swatches.pl">
# in which case the HTML is built the first (and subsequent)
# invocation by the cgi script itself.
# 
# My original design was to build the swatches GIF file dynamically 
# each time to facilitate changing colors, but decided to ditch this
# approach since it was a bit more unfriendly to users on slow 
# network connections. 

#-------------------------------------
# Build color table with RGB values
#-------------------------------------

# Set subscript start to 1.
$[ = 1;

# Popluate color table

$colors[1]  = "248,000,000";
$colors[2]  = "248,252,000";
$colors[3]  = "000,252,000";
$colors[4]  = "000,252,248";
$colors[5]  = "000,000,248";
$colors[6]  = "248,000,248";
$colors[7]  = "255,255,255";
$colors[8]  = "232,232,232";
$colors[9]  = "216,220,216";
$colors[10] = "208,212,208";
$colors[11] = "200,200,200";
$colors[12] = "184,184,184";
$colors[13] = "176,176,176";
$colors[14] = "168,168,168";
$colors[15] = "152,156,152";
$colors[16] = "144,144,144";

$colors[(1*16)+1]  = "200,000,048";
$colors[(1*16)+2]  = "248,236,000";
$colors[(1*16)+3]  = "000,152,080";
$colors[(1*16)+4]  = "000,172,224";
$colors[(1*16)+5]  = "040,016,104";
$colors[(1*16)+6]  = "200,000,120";
$colors[(1*16)+7]  = "128,132,128";
$colors[(1*16)+8]  = "120,120,120";
$colors[(1*16)+9]  = "104,108,104";
$colors[(1*16)+10] = "096,096,096";
$colors[(1*16)+11] = "080,080,080";
$colors[(1*16)+12] = "064,068,064";
$colors[(1*16)+13] = "048,052,048";
$colors[(1*16)+14] = "032,036,032";
$colors[(1*16)+15] = "016,020,016";
$colors[(1*16)+16] = "000,000,000";

$colors[(2*16)+1]  = "240,168,136";
$colors[(2*16)+2]  = "248,188,144";
$colors[(2*16)+3]  = "248,208,152";
$colors[(2*16)+4]  = "248,240,168";
$colors[(2*16)+5]  = "216,228,168";
$colors[(2*16)+6]  = "184,216,168";
$colors[(2*16)+7]  = "160,208,168";
$colors[(2*16)+8]  = "160,208,208";
$colors[(2*16)+9]  = "152,212,240";
$colors[(2*16)+10] = "152,180,216";
$colors[(2*16)+11] = "152,160,200";
$colors[(2*16)+12] = "152,144,184";
$colors[(2*16)+13] = "176,148,184";
$colors[(2*16)+14] = "200,156,192";
$colors[(2*16)+15] = "240,168,192";
$colors[(2*16)+16] = "240,168,168";

$colors[(3*16)+1]  = "224,120,088";
$colors[(3*16)+2]  = "240,156,096";
$colors[(3*16)+3]  = "248,188,104";
$colors[(3*16)+4]  = "248,236,120";
$colors[(3*16)+5]  = "192,216,120";
$colors[(3*16)+6]  = "152,200,120";
$colors[(3*16)+7]  = "104,184,120";
$colors[(3*16)+8]  = "104,188,184";
$colors[(3*16)+9]  = "088,192,232";
$colors[(3*16)+10] = "104,148,200";
$colors[(3*16)+11] = "104,124,176";
$colors[(3*16)+12] = "112,096,152";
$colors[(3*16)+13] = "144,100,160";
$colors[(3*16)+14] = "176,108,160";
$colors[(3*16)+15] = "224,120,160";
$colors[(3*16)+16] = "224,120,128";

$colors[(4*16)+1]  = "200,000,048";
$colors[(4*16)+2]  = "224,112,000";
$colors[(4*16)+3]  = "232,164,000";
$colors[(4*16)+4]  = "248,236,000";
$colors[(4*16)+5]  = "160,200,000";
$colors[(4*16)+6]  = "096,176,032";
$colors[(4*16)+7]  = "000,152,080";
$colors[(4*16)+8]  = "000,156,160";
$colors[(4*16)+9]  = "000,172,224";
$colors[(4*16)+10] = "000,116,176";
$colors[(4*16)+11] = "000,076,144";
$colors[(4*16)+12] = "040,016,104";
$colors[(4*16)+13] = "096,000,112";
$colors[(4*16)+14] = "136,000,112";
$colors[(4*16)+15] = "200,000,120";
$colors[(4*16)+16] = "200,000,080";

$colors[(5*16)+1]  = "144,000,032";
$colors[(5*16)+2]  = "160,080,000";
$colors[(5*16)+3]  = "168,116,000";
$colors[(5*16)+4]  = "176,168,000";
$colors[(5*16)+5]  = "112,140,000";
$colors[(5*16)+6]  = "064,124,024";
$colors[(5*16)+7]  = "000,104,056";
$colors[(5*16)+8]  = "000,112,112";
$colors[(5*16)+9]  = "000,120,160";
$colors[(5*16)+10] = "000,080,120";
$colors[(5*16)+11] = "000,056,104";
$colors[(5*16)+12] = "024,008,072";
$colors[(5*16)+13] = "072,000,072";
$colors[(5*16)+14] = "096,000,080";
$colors[(5*16)+15] = "144,000,080";
$colors[(5*16)+16] = "144,000,064";

$colors[(6*16)+1]  = "104,000,024";
$colors[(6*16)+2]  = "120,060,000";
$colors[(6*16)+3]  = "128,088,000";
$colors[(6*16)+4]  = "136,128,000";
$colors[(6*16)+5]  = "080,108,000";
$colors[(6*16)+6]  = "048,096,016";
$colors[(6*16)+7]  = "000,080,040";
$colors[(6*16)+8]  = "000,084,088";
$colors[(6*16)+9]  = "000,092,120";
$colors[(6*16)+10] = "000,060,096";
$colors[(6*16)+11] = "000,040,080";
$colors[(6*16)+12] = "016,008,056";
$colors[(6*16)+13] = "048,000,056";
$colors[(6*16)+14] = "072,000,056";
$colors[(6*16)+15] = "104,000,064";
$colors[(6*16)+16] = "104,000,048";

$colors[(7*16)+1]  = "208,192,168";
$colors[(7*16)+2]  = "168,152,128";
$colors[(7*16)+3]  = "128,116,096";
$colors[(7*16)+4]  = "096,080,072";
$colors[(7*16)+5]  = "064,056,048";
$colors[(7*16)+6]  = "208,172,120";
$colors[(7*16)+7]  = "176,140,096";
$colors[(7*16)+8]  = "152,116,064";
$colors[(7*16)+9]  = "128,092,040";
$colors[(7*16)+10] = "104,068,016";
$colors[(7*16)+11] = "255,255,255";
$colors[(7*16)+12] = "255,255,255";
$colors[(7*16)+13] = "255,255,255";
$colors[(7*16)+14] = "255,255,255";
$colors[(7*16)+15] = "255,255,255";
$colors[(7*16)+16] = "255,255,255";

#-------------------------------
# Build HTML
#-------------------------------
print "Content-type: text/html\n\n";
print "<HTML>\n";
print "<BASE HREF=\"http://sicg.calstate.edu/htmlcase/\">\n";
print "<HEAD>\n";
print "<TITLE>Color Swatches</TITLE>\n";
print "<BODY>\n";
print "<H2>RGB Color Swatches</H2>\n";

print "<HR>\n";
print "<A HREF=\"/scripts/htmlcase/cgi-bin/swatches.pl\">\n";
print "<IMG SRC=\"images/swatches.gif\" BORDER=0 ALIGN=LEFT ISMAP></A>\n";

print "<H5>Click on the desired color to get the RGB Hex values needed for specifying color\n";
print "in Netscape. The RGB values needed for Netscape color specs will show up below.  Do\n";
print "a copy and paste to get this into your HTML.</H5><BR CLEAR=LEFT>\n";

print "<HR>\n";

# print "$ENV{'QUERY_STRING'} ( $subscr )<BR>\n"; # Debugging

#-------------------------------------------------------------
# If QUERY_STRING is blank, then the script was not invoked
# by a click on a swatch - so don't display a hex value. 
# If it is non-blank, then calculate the RGB value.
#-------------------------------------------------------------

if ($ENV{'QUERY_STRING'}) {    
  # Get x,y pixel coordinates
  ($x,$y) = split(/,/,$ENV{'QUERY_STRING'});	

  # Get displacement into table
  $subscr = ( (int($y/12)*16) + (int($x/12)+1) );	

  # Parse table entry
  ($red,$grn,$blu) = split(/,/,$colors[$subscr]);

  # Format in hex
  $rgb = sprintf("#%2.2lx%2.2lx%2.2lx",$red, $grn, $blu); 

  # Print it (uppercase)
  print "\U$rgb\E<BR>\n";	
  }			

print "<P><BR><P><BR>\n";
print "<HR>\n";
print "<A HREF=\"index.html\"><IMG SRC=\"images/htmlretn.gif\"></A><P>\n";
print "<h6>Copyright &copy 1995, <A HREF=\"mailto:jcmiller\@calstate.edu\">John Miller</A>.</H6>\n";

print "</BODY>\n";
print "</HTML>\n";

exit;


