next up previous contents
Next: An Address Book Search Up: Some Example Perl CGI Previous: Some Example Perl CGI

Red, Green and Blue to Hexadecimal Converter

This is a useful program since HTML Background colours are specified in Hex values (See later lectures). This HTML FORM/CGI script converts RGB numbers in range 0-155 to HEX values.

The HTML FORM:

<HTML>
<HEAD>
<TITLE>RGBtoHex: an RGB to Hexidecimal Color Converter</TITLE>
</HEAD>
<BODY>
<H2>RGBtoHex</H2>


<HR>
<FORM METHOD=POST ACTION="http://www.cs.cf.ac.uk/User-bin/Dave.Marshall/rgb.pl">
<P>Please enter the RGB values for your color:
<P>Red (0-255): <INPUT TYPE="text" NAME="red"><BR>
Green (0-255): <INPUT TYPE="text" NAME="green"><BR>
Blue (0-255): <INPUT TYPE="text" NAME="blue"><BR>
<INPUT TYPE="submit" VALUE="Submit Values"><INPUT TYPE="reset" 
VALUE="Clear Values">
<HR>
<ADDRESS>
RGBtoHex was written by 
<A HREF="http://www.lne.com/lemay/">Laura Lemay</A> and 
<A HREF="http://www.lne.com/ericm/">Eric Murray</A>
</ADDRESS>

and looks like this:

RGBtoHex: an RGB to Hexidecimal Color Converter

RGBtoHex

RGBtoHex converts standard RGB values (three 0 to 255 ASCII numbers indicating red, green, and blue), into a hexadecimal triplet that can be used for the background and text colors in Netscape 1.1b1 or in any other program that requires colors in this format.

RGB2toHex was written by Eric Murray and Laura Lemay, and is in the public domain. Feel free to install the source for this program on your own system (it requires the perl language and cgi-lib.pl) but please credit us for it (you can use the signature at the bottom of this form).


Please enter the RGB values for your color:

Red (0-255):
Green (0-255):
Blue (0-255):


RGBtoHex was written by Laura Lemay and Eric Murray

The CGI Script is as follows:

require 'cgi-lib.pl';

# print header:
&ReadParse(*in);
$blort=&PrintHeader;

print "$blort\n";

print "<HTML><HEAD><TITLE>RGBtoHex: Results</TITLE></HEAD><BODY>\n";
print "<H2>RGBtoHex: Result</H2>\n";
print "<HR>\n";
# check for all values:
if (($in{'red'} eq '') || ($in{'green'} eq '') || ($in{'blue'} eq '')) {
	print "You need to give all three values!";
}
else {
	print "<p> RGB values of $in{'red'} $in{'green'} $in{'blue'} 
equals the hexadecimal value <B>";
	printf (" #%2.2X%2.2X%2.2X\n",$in{'red'},$in{'green'},$in{'blue'});
}
print "</B>\n<HR><ADDRESS>RGBtoHex was written by"; 
print "<A HREF=\"http://www.lne.com/lemay/\">Laura Lemay</A> and"; 
print "<A HREF=\"http://www.lne.com/ericm/\">Eric Murray</A>";
print "</ADDRESS><BODY></HTML>\n";



Download Source code:


What is going on here?

This Perl Script is fairly straightforward:


next up previous contents
Next: An Address Book Search Up: Some Example Perl CGI Previous: Some Example Perl CGI
dave@cs.cf.ac.uk