#!/usr/bin/perl
use Term::ANSIColor;
#	 License: GPLv2
#	--MikeeUSA--

@numberarray = (0,  16,  32,  48,  64,  80,  96, 112, 128, 144, 160, 176, 192, 208, 224, 240, 255);
$decimals = 10;
$includewords = 1;
#Startstuff And Display info
$saturation = @ARGV[0]; #0 full color 255 white all the way mixed in
$brightness = @ARGV[1];
if (@ARGV[2]) { $hue =  @ARGV[2];
	hueapply(); betweenhues(); #deals with hues not in the 16 table
} else {
$red = 1;
$green = 0;
$blue = 0.5;	
}

acolor();
if (@ARGV[1]) {
	acolorb();
}
##Displayed info
##Done
##on_rNNNgNNNbNNN
##print color red;
##print RED ON_GREEN "This text is red on green.\n";
#print"xxx\n";
$originalhuefromcommandline = $hue;
$boldwhite = 'bold white';

sub setcurrentcolorforprints
{
$bgcblack = 'r'.sprintf("%03d",$b_Red).'g'.sprintf("%03d",$b_Green).'b'.sprintf("%03d",$b_Blue);
$bgcblack2 = 'r'.sprintf("%03d",$color_r*255).'g'.sprintf("%03d",$color_g*255).'b'.sprintf("%03d",$color_b*255);
}

setcurrentcolorforprints();
print colored('12) Status Printout       ',"$boldwhite on_$bgcblack");
print"\n";
print colored('12) Status Printout       ',"$boldwhite on_$bgcblack2");
print"\n";

printpurehue();print"\n";
printcolorhue();print"\n";

s16sweep();
allsweep();

sub s16sweep {
	foreach (@numberarray) {
		$hue = $_;
		hueapply();
			betweenhues(); #deals with hues not in the 16 table, not applicable here however since this only does the table, unless you change the array
		saturationapply();
		brightnessapply();
		setcurrentcolorforprints();
		$decimals = 3;
		$includewords = 0;
		printpurehue();
	}
	print("\n");
	foreach (@numberarray) {
		$hue = $_;
		hueapply();
			betweenhues(); #deals with hues not in the 16 table, not applicable here however since this only does the table, unless you change the array
		saturationapply();
		brightnessapply();
		setcurrentcolorforprints();
		$decimals = 3;
		$includewords = 0;
		printcolorhue();
	}
	print("\n");
}

sub allsweep {
	my @a = (0..255);
	foreach (@a) {
	$hue = $_;
	hueapply();
	betweenhues(); #deals with hues not in the 16 table
	saturationapply();
	brightnessapply();
	setcurrentcolorforprints();
	$decimals = 3;
	$includewords = -1;
	printpurehue();
	}
	print("\n");
	print("\n");
	foreach (@a) {
	$hue = $_;
	hueapply();
	betweenhues(); #deals with hues not in the 16 table
	saturationapply();
	brightnessapply();
	setcurrentcolorforprints();
	$decimals = 3;
	$includewords = -1;
	printcolorhue();
	}
	print("\n");
	print("\n");
}

sub printpurehue 
{	
	$mydcmal = '%0'."$decimals".'d';
	if ($includewords == 1) {
		$myhuenumber = ' Hue number: '.sprintf("$mydcmal",$hue).'   ';
	} elsif ($includewords == -1) {
		$myhuenumber = sprintf("$mydcmal",$hue);
	} else {
		$myhuenumber = '   '.sprintf("$mydcmal",$hue).'   ';
	}
	print colored($myhuenumber,"$boldwhite on_$bgcblack");	
}
sub printcolorhue 
{	
	$mydcmal = '%0'."$decimals".'d';
	if ($includewords == 1) {
		$myhuenumber = ' Hue number: '.sprintf("$mydcmal",$hue).'   ';
	} elsif ($includewords == -1) {
		$myhuenumber = sprintf("$mydcmal",$hue);
	} else {
		$myhuenumber = '   '.sprintf("$mydcmal",$hue).'   ';
	}
	print colored($myhuenumber,"$boldwhite on_$bgcblack2");	
}


#These are the subroutines that do the work
#apply them in succession to get the rgb 0..1 color:
#set: $hue $saturation $brightness
#	hueapply();
#	saturationapply();
#	brightnessapply();
#get: $color_r $color_b $color_g
sub formatforrgb
{
	$red = $b_Red/255;
	$green = $b_Green/255;
	$blue = $b_Blue/255;
}
sub betweenhues
{
	$oldhue = $hue; #save
	betweenhues_interior();
	formatforrgb();
	$hue = $oldhue; #restore
}
sub setbColor0
{
	$b_Red0 = $b_Red;
	$b_Green0 = $b_Green;
	$b_Blue0 = $b_Blue;
}
sub setbColor1
{
	$b_Red1 = $b_Red;
	$b_Green1 = $b_Green;
	$b_Blue1 = $b_Blue;
}
#sub betweenhues_interior
#{
#	if ($oldhue > 240 && $oldhue < 255) {
#		print("hello!\n");
#		$hue = 240; hueapply(); setbColor0();
#		$hue = 255; hueapply(); setbColor1();
#		$myrange = 255 - 240; #15
#		$myfrombottom = $oldhue - 240; #ex: 241 - 240 = 1
#		$myfromtop   =  255 - $oldhue; #ex: 255 - 241 = 14
#		
#		$bottomweight = $myfromtop/$myrange;
#		$topweight = $myfrombottom/$myrange;
#		
#		$b_Red = ($b_Red0 * $bottomweight) + ($b_Red1 * $topweight);
#		$b_Green = ($b_Green0 * $bottomweight) + ($b_Green1 * $topweight);
#		$b_Blue = ($b_Blue0 * $bottomweight) + ($b_Blue1 * $topweight);
#	}
#}
sub betweenhues_interior
{
	$lownum = 0;
	$highnum = 0;
	#(0,  16,  32,  48,  64,  80,  96, 112, 128, 144, 160, 176, 192, 208, 224, 240, 255);
	     if ($oldhue > 240 && $oldhue < 255) {
		$lownum = 240;
		$highnum = 255;
		
	} elsif ($oldhue > 224 && $oldhue < 240) {
		$lownum = 224;
		$highnum = 240;
		
	} elsif ($oldhue > 208 && $oldhue < 224) {
		$lownum = 208;
		$highnum = 224;
			
	} elsif ($oldhue > 192 && $oldhue < 208) {
		$lownum = 192;
		$highnum = 208;
			
	} elsif ($oldhue > 176 && $oldhue < 192) {
		$lownum = 176;
		$highnum = 192;
			
	} elsif ($oldhue > 144 && $oldhue < 176) {
		$lownum = 144;
		$highnum = 176;
			
	} elsif ($oldhue > 128 && $oldhue < 144) {
		$lownum = 128;
		$highnum = 144;
			
	} elsif ($oldhue > 112 && $oldhue < 128) {
		$lownum = 112;
		$highnum = 128;
			
	} elsif ($oldhue > 96 && $oldhue < 112) {
		$lownum = 96;
		$highnum = 112;
			
	} elsif ($oldhue > 80 && $oldhue < 96) {
		$lownum = 80;
		$highnum = 96;
			
	} elsif ($oldhue > 64 && $oldhue < 80) {
		$lownum = 64;
		$highnum = 80;
			
	} elsif ($oldhue > 48 && $oldhue < 64) {
		$lownum = 48;
		$highnum = 64;
			
	} elsif ($oldhue > 32 && $oldhue < 48) {
		$lownum = 32;
		$highnum = 48;
			
	} elsif ($oldhue > 16 && $oldhue < 32) {
		$lownum = 16;
		$highnum = 32;
			
	} elsif ($oldhue > 0 && $oldhue < 16) {
		$lownum = 0;
		$highnum = 16;
			
	}
	
	
	if ($lownum || $highnum) {
		betweenhues_interior_interior();
	} else {
		#Do nothing as we are spot on one of the table values
	}
	
}
sub betweenhues_interior_interior
{
	if ($oldhue > $lownum && $oldhue < $highnum) {
		#print("hello!\n");
		$hue = $lownum; hueapply(); setbColor0();
		$hue = $highnum; hueapply(); setbColor1();
		$myrange = $highnum - $lownum; #15
		$myfrombottom = $oldhue - $lownum; #ex: 241 - $lownum = 1
		$myfromtop   =  $highnum - $oldhue; #ex: $highnum - 241 = 14
		
		$bottomweight = $myfromtop/$myrange;
		$topweight = $myfrombottom/$myrange;
		
		$b_Red = ($b_Red0 * $bottomweight) + ($b_Red1 * $topweight);
		$b_Green = ($b_Green0 * $bottomweight) + ($b_Green1 * $topweight);
		$b_Blue = ($b_Blue0 * $bottomweight) + ($b_Blue1 * $topweight);
		
		#format removing decimals
		$b_Red = int($b_Red);
		$b_Green = int($b_Green);
		$b_Blue = int($b_Blue);
	}
}

#test
$hue = 241; $hue=$originalhuefromcommandline; hueapply();
betweenhues();
print("\t",'"','b_Rgb','"',' '); print('"',"$b_Red $b_Green $b_Blue",'"'); print("\n");


sub hueapply
{
	$b_Red		= 0;
	$b_Green	= 0;
	$b_Blue		= 0;
	
	if ($hue >= 255) { #FF0000 Below
		$b_Red		= 255;
		$b_Green	= 0;
		$b_Blue		= 0;
	}  elsif ($hue >= 240) {#E50030 Below
		$b_Red		= 229;
		$b_Green	= 0;
		$b_Blue		= 48;
	}  elsif ($hue >= 224) {#B20061 Below
		$b_Red		= 178;
		$b_Green	= 0;
		$b_Blue		= 97;
	}  elsif ($hue >= 208) {#8300A3 Below
		$b_Red		= 131;
		$b_Green	= 0;
		$b_Blue		= 163;
	}  elsif ($hue >= 192) {#4200D5 Below
		$b_Red		= 66;
		$b_Green	= 0;
		$b_Blue		= 213;
	}  elsif ($hue >= 176) {#1000FE Below
		$b_Red		= 16;
		$b_Green	= 0;
		$b_Blue		= 254;
	}  elsif ($hue >= 160) {#001EEA Below
		$b_Red		= 0;
		$b_Green	= 30;
		$b_Blue		= 234;
	} elsif ($hue >= 144) {#0052BA Below
		$b_Red		= 0;
		$b_Green	= 82;
		$b_Blue		= 186;
	} elsif ($hue >= 128) {#008388  Below
		$b_Red		= 0;
		$b_Green	= 131;
		$b_Blue		= 136;
	} elsif ($hue >= 112) { #00B255  Below
		$b_Red		= 0;
		$b_Green	= 178;
		$b_Blue		= 85;
	} elsif ($hue >= 96) { #00E820  Below
		$b_Red		= 0;
		$b_Green	= 232;
		$b_Blue		= 32;
	} elsif ($hue >= 80) { #10F600  Below
		$b_Red		= 16;
		$b_Green	= 246;
		$b_Blue		= 0;
	} elsif ($hue >= 64) { #42CA00  Below
		$b_Red		= 66;
		$b_Green	= 202;
		$b_Blue		= 0;
	} elsif ($hue >= 48) { #84A500  Below
		$b_Red		= 132;
		$b_Green	= 165;
		$b_Blue		= 0;
	} elsif ($hue >= 32) { #B16700  Below
		$b_Red		= 177;
		$b_Green	= 103;
		$b_Blue		= 0;
	} elsif ($hue >= 16) {#CF2E00  Below
		$b_Red		= 207;
		$b_Green	= 46;
		$b_Blue		= 0;
	} elsif ($hue >= 0) { #FF0000  Below
		$b_Red		= 255;
		$b_Green	= 0;
		$b_Blue		= 0;
	}
	
	$red = $b_Red/255;
	$green = $b_Green/255;
	$blue = $b_Blue/255;
}


sub saturationapply
{
	$whiteamnt = $saturation/255;
	$coloramnt = 1 - $whiteamnt;
		$pre_color_r = $red * $coloramnt;
		$pre_color_g = $green * $coloramnt;
		$pre_color_b = $blue * $coloramnt;
		
	$color_r = $pre_color_r + $whiteamnt;
	$color_g = $pre_color_g + $whiteamnt;
	$color_b = $pre_color_b + $whiteamnt;
}

sub brightnessapply
{
	$color_r = $color_r * ($brightness/255);
	$color_g = $color_g * ($brightness/255);
	$color_b = $color_b * ($brightness/255);
}

#above are needed
############################################################




#These are prints, don't need to be in


sub acolor 
{
	print("\n","\n",'sat 0 is full color, 255 is white', "\n");
	print("\t",$saturation, "\n");
	saturationapply();
	#if ($saturation) {
	#	
	#	$color_r = $color_r * ($saturation / 255);
	#	$color_g = $color_g * ($saturation / 255);
	#	$color_b = $color_b * ($saturation / 255);
	#	$offset_saturation = 128 * (255 - $saturation) / 255;
	#	$color_r += $offset_saturation / 255;
	#	$color_g += $offset_saturation / 255;
	#	$color_b += $offset_saturation / 255;
	#}
print("\t",'"','color','"',' '); print('"',"$color_r $color_g $color_b",'"'); print("\n");
}
sub acolorb 
{
	print("\n","\n",'brightness 0 is black, 255 is full color', "\n");
	print("\t",$brightness, "\n");
	brightnessapply();
print("\t",'"','color','"',' '); print('"',"$color_r $color_g $color_b",'"'); print("\n");
}


if (!@ARGV[0]) {
	print("Command line: saturation(0..255) brightness(0..255) hue(0..255)\n")
}
