#!/usr/local/bin/perl
#========================================================================#
#c# Copyright (c) 1998 hesketh.com/inc. - All Rights Reserved.
#c# 
#c# THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF HESKETH.COM/INC.
#c# The copyright notice above does not evidence any actual or
#c# intended publication of such source code.
#c#
#------------------------------------------------------------------------#
# 
# Name: make-xpm.pl
#
# Author: Steven Champeon <schampeo@hesketh.com>
#
# Maintainer: same
#
# Version:
# $Id$
# Version History:
#  1.0 - 07/27/98 - initial version.
#
# Dependencies:
#  none.
# Purpose:
#  read a file containing an array of values representing the state of
#  registration of various internet domains, then write an X Pixmap
#  based on that array.
#
# Usage:
#  
#------------------------------------------------------------------------#

$file = "./3d.txt";

init_colors();
read_domains_file();
make_grid_array();
write_xpm();

sub get_2d_header {
    $header = '/* XPM */';
    $header .= 'static char* 2dnamespace = \{';
    $header .= '"26 26 8 2",';
    $header .= '"_r c #ff0000",';
	$header .= '"_g c #00ff00",';
	$header .= '"_b c #0000ff",';
	$header .= '"_y c #ffff00",';
	$header .= '"_m c #ff00ff",';
	$header .= '"_c c #00ffff",';
    $header .= '"_k c #000000",';
    $header .= '"__ c #ffffff",';

    return $header;
}

sub get_3d_header {
    $header = "/* XPM */\n";
    $header .= "static char* 3letter_namespace = {\n";

    # should be like '"_r c #ff0000",'
    foreach $rgb (sort (keys %colors)) {
		$clut .= "\"$colors{$rgb} c $rgb\",\n";
		$num_colors++;
    }
    $header .= "\"26 26 " . $num_colors . " 2\",\n";
    $header .= "$clut";
    $header .= "/*\n a b c d e f g h i j k l m ";
    $header .= "n o p q r s t u v w x y z\n*/\n";
    return $header;
}

sub read_domains_file {
    open(FILE, $file)
	or die "couldn't open $file! $!";

    while(<FILE>) {
		$index = index($_, '.');
		$domain = substr($_, 0, $index);
		if(/com/) {
			if(/y$/) {
				$com{$domain} = 1;
			} else {
				$com{$domain} = 0;
			}
		} elsif(/org/) {
			if(/y$/) {
				$org{$domain} = 1;
			} else {
				$org{$domain} = 0;
			}
		} elsif(/net/) {
			if(/y$/) {
				$net{$domain} = 1;
			} else {
				$net{$domain} = 0;
			}
		}
	}
    close(FILE);
}

sub make_grid_array {
    for($i=97; $i<123; $i++) {
		$one = chr($i);
		for($j=97; $j<123; $j++) {
			$two = chr($j);
			for($k=97; $k<123; $k++) {
				$three = chr($k);
				$dom = $one . $two . $three;
				$xy = $one . $two;
				if($com{$dom} == 1) {
					$com_grid{$xy} += 9.8;
				} 
				if($net{$dom} == 1) {
					$net_grid{$xy} += 9.8;
				}
				if($org{$dom} == 1) {
					$org_grid{$xy} += 9.8;
				}
				
				$grid{$xy} = ""; # initialize grid
			}
		}
    }
	
    foreach $xy (sort (keys %grid)) {
		# should be aa, ab, ac, etc.
		($x, $y) = split(//, $xy);
		# get rgb val for square
		$r = get_hex_value($com_grid{$xy});
		$g = get_hex_value($net_grid{$xy});
		$b = get_hex_value($org_grid{$xy});
		$grid_lines{$y} .= get_color($r, $g, $b);
    }
}

sub get_hex_value {
    local($n) = @_;
    if($n <= 0) { return "00"; }
    elsif($n > 0 && $n <= 51) { return "33"; }
    elsif($n > 51 && $n <= 102) { return "66"; }
    elsif($n > 102 && $n <= 153) { return "99"; }
    elsif($n > 153 && $n <= 204) { return "CC"; }
    elsif($n > 204 && $n <= 255) { return "FF"; }
    else { return "FF"; }
}

sub get_color {
    local($r, $g, $b) = @_;
    $rgb = "#" . $r . $g . $b;
    if($colors{$rgb}) {
		return $colors{$rgb};
    } else {
		return '00'; # error condition
    }
}

sub write_xpm {
    open(XPM, ">./3d.xpm")
		or die "Um, sorry, dave. $!";
	
    $head = get_3d_header();
	
    for($x=1;$x<=26;$x++) {
		$letter = chr($x + 96);
		$body .= "\"$grid_lines{$letter}\", /* $letter */\n";
    }
	
    print XPM "$head$body};\n";
    close(XPM);
}

sub init_colors {
    @hex = ( "00", "33", "66", "99", "CC", "FF" );
    $cnt = 0;
	
    for($r=0;$r<6;$r++) {
		for($g=0;$g<6;$g++) {
			for($b=0;$b<6;$b++) {
				$rgb = "#" . $hex[$r] . $hex[$g] . $hex[$b];
				$temp = sprintf "%lx", $cnt;
				if(length($temp) == 1) {
					$colors{$rgb} = "0" . $temp;
				} else {
					$colors{$rgb} = $temp;
				}
				$cnt++;
			}
		}
    }
}
