#!/usr/bin/perl
package CreateGD;

##########################################################################
##### Author :	Shuai Weng
##### Date   :  Oct. 2001
##### Description : This package contains all necessary methods for 
#####               creating GD object and more .....
#####
#####  Usage: 
#####  use CreateGD;
#####
#####  To instantiate a new CreateGD object, you may use  
#####  following syntax:
#####  my $gd = CreateGD->new(width=>$imgWidth,
#####	  	              height=>$imgHeight);                       
#####     
#####
#####  my $im = $gd->im;
#####  my $white = $gd->white;
#####  my $red = $gd->red;
#####  and more ......      
#######################################################################
use strict;
use CGI qw/:all/;
use GD;

#######################################################################
sub new {      ############ constructor ###############################
#######################################################################
       
    my ($self, %args) = @_;

    $self = {};
    bless $self;
    
    if (!$args{'width'} || !$args{'height'}) {
	print "The image width and height must be passed to this object.";
	return;
    }
    $self->{'_im'} = new GD::Image($args{'width'}, $args{'height'});

    $self->{'_width'} = $args{'width'};
    $self->{'_height'} = $args{'height'};
#    $self->{'_bgcolor'} = $args{'bgcolor'};

    $self->initColors;

    return $self;
}

#######################################################################
sub initColors {
#######################################################################
    my ($self) = @_;

#    if ($self->{'_bgcolor'}) {

#	$self->{'_lightGrey'} = $self->im->colorAllocate(215, 215, 215);

#    }
    $self->{'_white'} = $self->im->colorAllocate(255, 255, 255);
    $self->{'_white1'} = $self->im->colorAllocate(254, 254, 254);
    
    $self->{'_black'} = $self->im->colorAllocate(0, 0, 0);

    $self->{'_blue'} = $self->im->colorAllocate(0, 0, 255);
    $self->{'_blue1'} = $self->im->colorAllocate(0, 0, 125);
    $self->{'_blue2'} = $self->im->colorAllocate(0, 255, 255);
    $self->{'_blue3'} = $self->im->colorAllocate(0, 204, 204);
    $self->{'_blue4'} = $self->im->colorAllocate(80, 153, 255);
    $self->{'_blue5'} = $self->im->colorAllocate(128, 156, 201);
    $self->{'_lightBlue'} = $self->im->colorAllocate(127, 255, 255);
    $self->{'_darkBlue'} = $self->im->colorAllocate(10, 80, 161);
    
    $self->{'_darkGreen'} = $self->im->colorAllocate(47, 79, 47);
    $self->{'_green'} = $self->im->colorAllocate(0, 255, 0);
    $self->{'_green1'} = $self->im->colorAllocate(51, 160, 44);
    $self->{'_green2'} = $self->im->colorAllocate(102, 255, 102);
    $self->{'_green3'} = $self->im->colorAllocate(153, 212, 127);
    $self->{'_green4'} = $self->im->colorAllocate(0, 255, 51);
    $self->{'_green5'} = $self->im->colorAllocate(102, 153, 102);


    $self->{'_grey'} = $self->im->colorAllocate(180, 180, 180);
    $self->{'_darkGrey'} = $self->im->colorAllocate(102, 102, 102);
    $self->{'_lightGrey'} = $self->im->colorAllocate(215, 215, 215);

    $self->{'_magenta'} = $self->im->colorAllocate(255, 0, 255);
    $self->{'_magenta1'} = $self->im->colorAllocate(255, 153, 255);
    $self->{'_magenta2'} = $self->im->colorAllocate(127, 0, 255);
    $self->{'_magenta3'} = $self->im->colorAllocate(204, 50, 153);
    $self->{'_magenta4'} = $self->im->colorAllocate(188, 128, 189);
    $self->{'_magenta5'} = $self->im->colorAllocate(129, 23, 136);
    

    $self->{'_maroon'} = $self->im->colorAllocate(142, 35, 107);

    $self->{'_orange'} = $self->im->colorAllocate(255, 175, 0);
    $self->{'_orange1'} = $self->im->colorAllocate(255, 125, 0);
    
    $self->{'_red'}  = $self->im->colorAllocate(255, 0, 0);
    $self->{'_red1'} = $self->im->colorAllocate(255, 152, 153);
    $self->{'_red2'} = $self->im->colorAllocate(204, 0, 102);
    $self->{'_red3'} = $self->im->colorAllocate(153, 0, 0);
    $self->{'_red4'} = $self->im->colorAllocate(251, 128, 95);

    $self->{'_tan1'} = $self->im->colorAllocate(252, 192, 167);
    $self->{'_tan'} = $self->im->colorAllocate(235, 199, 158);

    $self->{'_yellow'} = $self->im->colorAllocate(255, 255, 0);
    $self->{'_yellow1'} = $self->im->colorAllocate(255, 204, 51);
    $self->{'_yellow2'} = $self->im->colorAllocate(255, 204, 102);
    $self->{'_yellow3'} = $self->im->colorAllocate(255, 204, 153);

    $self->{'_chartreuse'} = $self->im->colorAllocate(204, 236, 244);

    $self->{'_cenColor'} = $self->im->colorAllocate(0, 10, 10);
    
    $self->{'_sagecolor1'} = $self->im->colorAllocate(255, 127, 0);
    $self->{'_sagecolor2'} = $self->im->colorAllocate(127, 0, 255);
    $self->{'_sagecolor3'} = $self->im->colorAllocate(207, 181, 59);
    $self->{'_sagecolor4'} = $self->im->colorAllocate(204, 50, 153);

   
}


#######################################################################
sub setBarColor {
#######################################################################
    my ($self, %args) = @_;
    if ($args{'feature_type'} =~ /clone/i) {
	return $self->darkGreen;
    }	
    elsif ($args{'feature_type'} =~ /^tRNA/i) {
	return $self->green;
    }
    elsif ($args{'feature_type'} =~ /^RNA/i) {
	return $self->darkGrey;
    }
    elsif ($args{'feature_type'} =~ /^Transposon/i) {
	return $self->yellow;
    }
    elsif ($args{'feature_type'} =~ /^(rRNA|Ribosomal RNA)/i) {
	return $self->tan;
    }
    elsif ($args{'feature_type'} =~ /^(snRNA|Small Nuclear RNA)/i) {
	return $self->maroon;
    }
    elsif ($args{'feature_type'} =~ /^LTR/i) {
	return $self->magenta;
    }
    elsif ($args{'feature_type'} =~ /^CEN/i) {
	return $self->cenColor;
    }
    elsif ($args{'feature_type'} =~ /^Watson/i) {
	return $self->red;
    }
    elsif ($args{'feature_type'} =~ /^Crick/i) {
	return $self->blue;
    }
    elsif ($args{'feature_type'} =~ /^ARS/i) {
	return $self->black;
    }
    elsif ($args{'feature_type'} =~ /^snoRNA/i) {
	return $self->red2;
    }
    elsif ($args{'feature_type'} =~ /^Y\' element/i) {
	return $self->yellow1;
    }
    elsif ($args{'feature_type'} =~ /^X element Core Sequence/i) {
	return $self->red3;
    }
    elsif ($args{'feature_type'} =~ /^X element Combinatorial Repeats/i) {
	return $self->green5;
    }
    elsif ($args{'feature_type'} =~ /^Telomeric Repeat/i) {
	return $self->blue3;
    }
    elsif ($args{'feature_type'} =~ /^Dubious/i) {
	return $self->lightGrey;
    }
    else {
	if  ($args{'strand'} =~ /^W/i) {
	    return $self->red;
	}
	else {
	    return $self->blue;
	}
    }
}

#######################################################################
sub setTaxonomyColor {
#######################################################################
    my ($self, %args) = @_;
    if ($args{'organism'} =~ /^archaea/i) {
	return $self->orange1;
    }	
    elsif ($args{'organism'} =~ /bacteria/i) {
	return $self->yellow1;
    }	
    elsif ($args{'organism'} =~ /virus/i) {
	return $self->magenta4;
    }	
    elsif ($args{'organism'} =~ /ascomycetes/i) {
	return $self->red;
    }	
    elsif ($args{'organism'} =~ /basidiomycetes/i) {
	return $self->tan1;
    }	
    elsif ($args{'organism'} =~ /other fungi/i) {
	return $self->red4;
    }	
    elsif ($args{'organism'} =~ /mammal/i) {
	# return $self->darkBlue;
	return $self->blue4;
    }	
    elsif ($args{'organism'} =~ /other vertebrate/i) {
	# return $self->chartreuse;
	return $self->blue5;
	
    }	
    elsif ($args{'organism'} =~ /animals/i && 
	   $args{'organism'} =~ /other/i) {
	# return $self->blue5;
	return $self->darkBlue;
    }	
    elsif ($args{'organism'} =~ /plant/i) {
	return $self->green1;
    }	
    elsif ($args{'organism'} =~ /other eukaryotes/i) {
	return $self->green3;
    }	
    else { ### unknown organism
	return $self->magenta5;
    }	
    

}


######################################################################

######################################################################


sub im { $_[0]->{'_im'} }

sub white1 { $_[0]->{'_white1'} }
sub black { $_[0]->{'_black'} }

sub blue { $_[0]->{'_blue'} }
sub blue1 { $_[0]->{'_blue1'} }
sub blue2 { $_[0]->{'_blue2'} }
sub blue3 { $_[0]->{'_blue3'} }
sub blue4 { $_[0]->{'_blue4'} }
sub blue5 { $_[0]->{'_blue5'} }
sub lightBlue { $_[0]->{'_lightBlue'} }
sub darkBlue { $_[0]->{'_darkBlue'} }

sub darkGreen { $_[0]->{'_darkGreen'} }
sub green { $_[0]->{'_green'} }
sub green1 { $_[0]->{'_green1'} }
sub green2 { $_[0]->{'_green2'} }
sub green3 { $_[0]->{'_green3'} }
sub green4 { $_[0]->{'_green4'} }
sub green5 { $_[0]->{'_green5'} }

sub grey { $_[0]->{'_grey'} }
sub darkGrey { $_[0]->{'_darkGrey'} }
sub lightGrey { $_[0]->{'_lightGrey'} }

sub magenta { $_[0]->{'_magenta'} }
sub magenta1 { $_[0]->{'_magenta1'} }
sub magenta2 { $_[0]->{'_magenta2'} }
sub magenta3 { $_[0]->{'_magenta3'} }
sub magenta4 { $_[0]->{'_magenta4'} }
sub magenta5 { $_[0]->{'_magenta5'} }

sub maroon { $_[0]->{'_maroon'} }

sub orange { $_[0]->{'_orange'} }
sub orange1 { $_[0]->{'_orange1'} }


sub red { $_[0]->{'_red'} }
sub red1 { $_[0]->{'_red1'} }
sub red2 { $_[0]->{'_red2'} }
sub red3 { $_[0]->{'_red3'} }
sub red4 { $_[0]->{'_red4'} }

sub tan { $_[0]->{'_tan'} }
sub tan1 { $_[0]->{'_tan1'} }

sub white { $_[0]->{'_white'} }

sub yellow { $_[0]->{'_yellow'} }
sub yellow1 { $_[0]->{'_yellow1'} }
sub yellow2 { $_[0]->{'_yellow2'} }
sub yellow3 { $_[0]->{'_yellow3'} }

sub chartreuse { $_[0]->{'_chartreuse'} }

sub cenColor { $_[0]->{'_cenColor'} }

sub sagecolor1 { $_[0]->{'_sagecolor1'} }
sub sagecolor2 { $_[0]->{'_sagecolor2'} }
sub sagecolor3 { $_[0]->{'_sagecolor3'} }
sub sagecolor4 { $_[0]->{'_sagecolor4'} }

########################################################################
1;
########################################################################

=pod

=head1 Name

CreateGD.pm    

=head1 Description









=head1 Author

Shuai Weng, shuai@genome.stanford.edu


=cut







