Log in

View Full Version : Yet another of my Perl Contraptions....


TheMatrix
June 16th, 2011, 07:19 PM
Right, so this is kinda basic and stuff, but still........

Anyways, I decided to make a random colours script. It makes 2 of the 256 available colours for the web - one for text and one for the background.
Pretty nice, even if I say so myself.
Without further ado:
#!/usr/bin/perl -w

use CGI;

my $CGI = new CGI;

my $bg = &mkColour() or die( "cannot make colour: $!" );
my $txt = &mkColour() or die( "cannot make colour: $!" );


print $CGI->header( "text/html" );
print qq`
<html>
<head>
<title>Pretty colours!</title>
<script type="text/javascript">
alert( 'Background: $bg\\nText: $txt' );
</script>
</head>
<body bgcolor="$bg" text="$txt">
<h1>Pretty Colours!!!!!</h1>
<button onclick="location.href='colours.cgi'">New colours</button>
</body>
</html>`;


sub mkColour() {
my $str = '';
my $i = 1;
my @hex = ( "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F" );
while( $i <= 6 ) {
my $n = int( rand( $#hex ) );
$str .= $hex[$n];
$i++;
}
return $str;
}


Pretty nice, huh?

Maybe it can come in handy, but until now it's been making hideous combinations. But hey, that's the power of random!