// JavaScript Document
var loff=0;
var hoff=0;
var lr;
var lg;
var lb;

function darker()
{
   loff=loff-.05;
   updatepre();
}

function lighter()
{
   loff=loff+.05;
   updatepre();
}

function colorup()
{
   hoff=hoff+15;
   updatepre();
}

function colordn()
{
   hoff=hoff-15;
   updatepre();
}

function updatepre()
{
hls=RGBtoHLS(lr*1,lg*1,lb*1)

hls.l=hls.l+loff;
hls.h=hls.h+hoff;
rgba=HLStoRGB(hls.h,hls.l,hls.s);
generatebase(hls.h,hls.l,hls.s);
showbase('0',true)
}

function coinit()
{
}


function gcolor(r,g,b)
{
lr=r;
lb=b;
lg=g;

hls=RGBtoHLS(r*1,g*1,b*1)
generatebase(hls.h,hls.l,hls.s);
showbase('0',true)

}

function setcolors(base,c1,c2,c3,c4,c6,c5)
{
   document.getElementById(base+"a").style.backgroundColor=c1;
   document.getElementById(base+"b").style.backgroundColor=c2;
   document.getElementById(base+"c").style.backgroundColor=c3;
   document.getElementById(base+"d").style.backgroundColor=c4;
   document.getElementById(base+"e").style.backgroundColor=c5;
   document.getElementById(base+"f").style.backgroundColor=c6;
}

function fixupColor(c){
   if(c=="")
      return "FFFFFF";
   if(c.indexOf("rgb")==0)
      c=eval(c);

   if(c.charAt(0)=='#')
      return c.substr(1).toLowerCase();
   return c.toLowerCase();
}


function initpreview()
{
   c1='#000000';
   c4 = '#ffffff';
   c2=document.mainform.d2.value;
   c3=document.mainform.d3.value;
   
   c5=document.mainform.l2.value;
   c6=document.mainform.l3.value;
   setpreview(c1,c2,c3,c4,c5,c6);

   rgb2=HextoRGB(c3)
   hls2=RGBtoHLS(rgb2.r*1,rgb2.g*1,rgb2.b*1)
   lr=rgb2.r;
   lg=rgb2.g;
   lb=rgb2.b;

   generatebase(hls2.h,hls2.l,hls2.s);
   updatecolorbar();
}

function setpreview(c1,c2,c3,c4,c5,c6)
{
	if(document.mainform.d2)
	{
		document.mainform.d2.value=fixupColor(c2);
	}
	if(document.mainform.d2)
	{
		document.mainform.d2.value=fixupColor(c2);
	}

	if(document.mainform.d2)
	{
		document.mainform.d2.value=fixupColor(c2);
	}

	if(document.mainform.d2)
	{
		document.mainform.d2.value=fixupColor(c2);
	}

	if(document.mainform.d2)
	{
		document.mainform.d2.value=fixupColor(c2);
	}
	document.mainform.d2.value=fixupColor(c2);
   document.mainform.d3.value=fixupColor(c3);
   document.mainform.l2.value=fixupColor(c5);
   document.mainform.l3.value=fixupColor(c6);
   var cstr=document.mainform.d2.value+"-"+document.mainform.d3.value+"-"+document.mainform.l2.value+"-"+document.mainform.l3.value;
   document.getElementById("colorstr").innerHTML=cstr;
}

function HextoRGB(hex) {
  hex = hex.toUpperCase();
  if(hex.charAt(0) == "#") hex = hex.substring(1,hex.length);
  var rgb = new Array(3);
  rgb.r = hex.substring(0,2);
  rgb.g = hex.substring(2,4);
  rgb.b = hex.substring(4,6);
  rgb.r = parseInt(rgb.r,16);
  rgb.g = parseInt(rgb.g,16);
  rgb.b = parseInt(rgb.b,16);
  if(isNaN(rgb.r)) rgb.r = 0;
  if(isNaN(rgb.g)) rgb.g = 0;
  if(isNaN(rgb.b)) rgb.b = 0;
  return rgb;
}

function HLStoHEX(hueq,l,s,hadd,ladd,sadd)
{
 hueq=hueq*1;
 hadd=hadd*1;
 hueq=hueq+hadd;

 thisl = l+ladd;
 if(thisl>.9)
    thisl=l+ladd;
 if(thisl<0)
    thisl=l-ladd;

 thiss = s+sadd;
 if(thiss>.9)
    thiss=s+sadd;
 if(thiss<0)
    thiss=s-sadd;

 if(ladd>0)
   {
   thiss=sadd;
   thisl=ladd;
 }

 if(hueq>360)
    hueq=hueq-360;
 if(hueq<0)
    hueq=hueq+360;
 rgba=HLStoRGB(hueq,thisl,thiss);
 c=RGBtoHex(rgba.r,rgba.g,rgba.b);

 return c

}

function generateMono(hue,l,s)
{
     hue=hue*1;
     l=l*1;
     s=s*1;

     newl=l-.2;
     if(newl<.2)
        newl=l+.2;

     if(newl>.5)
        newl=.5;

    //Do the comps
    c1="000000";
    c2=HLStoHEX(hue,l,s,0,0,0);
    c3=HLStoHEX(hue,newl,s,0,0,0);
    c4='ffffff'
    c5=HLStoHEX(hue,.7,.5,0,0,0);
    c6=HLStoHEX(hue,.8,.7,0,0,0);
    setcolors("MONO1",c1,c2,c3,c4,c5,c6);
    setpreview(c1,c2,c3,c4,c5,c6);
    setcolors("MONO2",c1,c2,c3,c6,c4,c5);

    return false;
}

function showpreview(name)
{
   c1=document.getElementById(name+"a").style.backgroundColor;
   c2=document.getElementById(name+"b").style.backgroundColor;
   c3=document.getElementById(name+"c").style.backgroundColor;
   c4=document.getElementById(name+"d").style.backgroundColor;
   c5=document.getElementById(name+"e").style.backgroundColor;
   c6=document.getElementById(name+"f").style.backgroundColor;
   setpreview(c1,c2,c3,c4,c5,c6);
}

function showbase(name,noclear)
{
   showpreview('BASE'+name);
   id=name*1;
   updatecolorbar();
   if(noclear==true)
      return;
}

function generatebase(hue,l,s)
{
	
   for(x=0;x<10;x++)
     {
     document.getElementById("BASE"+x+"a").style.backgroundColor="#000000";
     document.getElementById("BASE"+x+"c").style.backgroundColor=HLStoHEX(hue,l,s,0,0,0);
     document.getElementById("BASE"+x+"d").style.backgroundColor="#ffffff";
     document.getElementById("BASE"+x+"f").style.backgroundColor=HLStoHEX(hue,l,s,0,.9,.9);
     }

   ml=l-.2;
   if(ml<.2)
      ml=l+.2;

   if(ml>.5)
      ml=.5;

   document.getElementById("BASE0b").style.backgroundColor=HLStoHEX(hue,ml,s,0,.5,.5);
   document.getElementById("BASE1b").style.backgroundColor=HLStoHEX(hue,l,s,90,.5,.5);
   document.getElementById("BASE2b").style.backgroundColor=HLStoHEX(hue,l,s,60,.5,.5);
   document.getElementById("BASE3b").style.backgroundColor=HLStoHEX(hue,l,s,30,.5,.5);
   document.getElementById("BASE4b").style.backgroundColor=HLStoHEX(hue,l,s,120,.5,.5);
   document.getElementById("BASE5b").style.backgroundColor=HLStoHEX(hue,l,s,-30,.5,.5);
   document.getElementById("BASE6b").style.backgroundColor=HLStoHEX(hue,l,s,-60,.5,.5);
   document.getElementById("BASE7b").style.backgroundColor=HLStoHEX(hue,l,s,-90,.5,.5);
   document.getElementById("BASE8b").style.backgroundColor=HLStoHEX(hue,l,s,-120,.5,.5);
   document.getElementById("BASE9b").style.backgroundColor=HLStoHEX(hue,l,s,-180,.5,.5);


   document.getElementById("BASE0e").style.backgroundColor=HLStoHEX(hue,l,s,0,.8,.8);
   document.getElementById("BASE1e").style.backgroundColor=HLStoHEX(hue,l,s,90,.8,.8);
   document.getElementById("BASE2e").style.backgroundColor=HLStoHEX(hue,l,s,60,.8,.8);
   document.getElementById("BASE3e").style.backgroundColor=HLStoHEX(hue,l,s,30,.8,.8);
   document.getElementById("BASE4e").style.backgroundColor=HLStoHEX(hue,l,s,120,.8,.8);
   document.getElementById("BASE5e").style.backgroundColor=HLStoHEX(hue,l,s,-30,.8,.8);
   document.getElementById("BASE6e").style.backgroundColor=HLStoHEX(hue,l,s,-60,.8,.8);
   document.getElementById("BASE7e").style.backgroundColor=HLStoHEX(hue,l,s,-90,.8,.8);
   document.getElementById("BASE8e").style.backgroundColor=HLStoHEX(hue,l,s,-120,.8,.8);
   document.getElementById("BASE9e").style.backgroundColor=HLStoHEX(hue,l,s,-180,.8,.8);
   
   /*document.getElementById("BASE0f").style.backgroundColor=HLStoHEX(hue,l,s,0,.9,.9);
   document.getElementById("BASE1f").style.backgroundColor=HLStoHEX(hue,l,s,95,.9,.9);
   document.getElementById("BASE2f").style.backgroundColor=HLStoHEX(hue,l,s,65,.9,.9);
   document.getElementById("BASE3f").style.backgroundColor=HLStoHEX(hue,l,s,35,.9,.9);
   document.getElementById("BASE4f").style.backgroundColor=HLStoHEX(hue,l,s,125,.9,.9);
   document.getElementById("BASE5f").style.backgroundColor=HLStoHEX(hue,l,s,-35,.9,.9);
   document.getElementById("BASE6f").style.backgroundColor=HLStoHEX(hue,l,s,-65,.9,.9);
   document.getElementById("BASE7f").style.backgroundColor=HLStoHEX(hue,l,s,-95,.9,.9);
   document.getElementById("BASE8f").style.backgroundColor=HLStoHEX(hue,l,s,-125,.9,.9);
   document.getElementById("BASE9f").style.backgroundColor=HLStoHEX(hue,l,s,-185,.9,.9);*/
}

function generateTwo(hue,l,s,name,factor)
{
     factor=factor*1;
     hue=hue*1;
     l=l*1;
     s=s*1;

     fl=.9;
     fs=.9;

    //Do the comps
    c1="000000";
    c2=HLStoHEX(hue,l,s,0,0,0);
    c3=HLStoHEX(hue,l,s,factor,0,0);
    c4='ffffff'
    c5=HLStoHEX(hue,l,s,factor,fl,fs);
    c6=HLStoHEX(hue,l,s,0,fl,fs);
    setcolors(name+"1",c1,c2,c3,c4,c5,c6);
     setpreview(c1,c2,c3,c4,c5,c6);

    c1="000000";
    c2=HLStoHEX(hue,l,s,0,0,0);
    c3=HLStoHEX(hue,l,s,factor,0,0);
    c4='ffffff'
    c5=HLStoHEX(hue,l,s,factor,fl,fs);
    c6=HLStoHEX(hue,l,s,0,fl,fs);
    setcolors(name+"2",c1,c2,c3,c4,c5,c6);

    c1="000000";
    c2=HLStoHEX(hue,l,s,0,0,0);
    c3=HLStoHEX(hue,l,s,factor,0,0);
    c4='ffffff'
    c5=HLStoHEX(hue,l,s,factor,fl,fs);
    c6=HLStoHEX(hue,l,s,0,fl,fs);
    setcolors(name+"3",c1,c2,c3,c4,c5,c6);

    c1="000000";
    c2=HLStoHEX(hue,l,s,0,0,0);
    c3=HLStoHEX(hue,l,s,-factor,0,0);
    c4='ffffff'
    c5=HLStoHEX(hue,1,s,-factor,fl,fs);
    c6=HLStoHEX(hue,1,s,0,fl,fs);
    setcolors(name+"4",c1,c2,c3,c4,c5,c6);
    setpreview(c1,c2,c3,c4,c5,c6);

    c1="000000";
    c2=HLStoHEX(hue,l,s,0,0,0);
    c3=HLStoHEX(hue,l,s,-factor,0,0);
    c4='ffffff'
    c5=HLStoHEX(hue,l,s,-factor,fl,fs);
    c6=HLStoHEX(hue,l,s,0,fl,fs);
    setcolors(name+"5",c1,c2,c3,c4,c5,c6);

    c1="000000";
    c2=HLStoHEX(hue,l,s,0,0,0);
    c3=HLStoHEX(hue,l,s,-factor,0,0);
    c4='ffffff'
    c5=HLStoHEX(hue,l,s,-factor,fl,fs);
    c6=HLStoHEX(hue,l,s,0,fl,fl);
    setcolors(name+"6",c1,c2,c3,c4,c5,c6);
}


function generatecolors(hue,l,s)
{

  l=l*1;
  s=s*1;
  generateTwo(hue,l,s,"TWO",120);
  generateTwo(hue,l,s,"c60_TWO",60);
  generateTwo(hue,l,s,"c90_TWO",90);
  generateTwo(hue,l,s,"c180_TWO",180);
  generateTwo(hue,l,s,"ADD",30);
  generateMono(hue,l,s);
}

function docolor()
   {
   if(!document.all)document.all=document.layers;
   hue=document.all.ch.value;
   sat=document.all.cs.value;
   light=document.all.cl.value;

   hue=hue*1
   sat=sat*1
   light=light*1
  generatecolors(hue,sat,light)
  }

function MIN() {
  var min = 255;
  for(var i = 0; i < arguments.length; i++)
    if(arguments[i] < min)
      min = arguments[i];
  return min;
}
function MAX() {
  var max = 0;
  for(var i = 0; i < arguments.length; i++)
    if(arguments[i] > max)
      max = arguments[i];
  return max;
}

function RGBtoHLS(R,G,B) {
  R /= 255;
  G /= 255;
  B /= 255;
  var max, min,diff,r_dist,g_dist,b_dist;
  var hls = new Array(3);
  max = MAX(R,G,B);
  min = MIN(R,G,B);
  diff = max-min;
  hls.l = (max+min)/2;
  if (diff==0) {
    hls.h = 0;
    hls.s = 0;
  } else {
    if (hls.l<0.5) hls.s = diff/(max+min);
    else hls.s = diff/(2-max-min);
    r_dist = (max-R)/diff;
    g_dist = (max-G)/diff;
    b_dist = (max-B)/diff;
    if (R == max) { hls.h = b_dist-g_dist; }
    else if (G == max) { hls.h = 2+r_dist-b_dist; }
    else if (B == max) { hls.h = 4+g_dist-r_dist; }
    hls.h *= 60;
    if (hls.h<0) hls.h += 360;
    if (hls.h>=360) hls.h -= 360;
  }
  return hls;
}

function rgb(R, G, B) {
  var n = Math.round(B);
  n += Math.round(G) << 8;
  n += Math.round(R) << 16;
  return DectoHex(n);
}

function RGBtoHex(R, G, B) {
  var n = Math.round(B);
  n += Math.round(G) << 8;
  n += Math.round(R) << 16;
  return DectoHex(n);
}
// turns decimal integer (e.g., bgColor) into hexadecimal string
function DectoHex(num) {
  var i = 0; var j = 20;
  var str = "#";
  while(j >= 0) {
    i = (num >> j)%16;
    if(i >= 10) {
      if(i == 10) str += "A";
      else if(i == 11) str += "B";
      else if(i == 12) str += "C";
      else if(i == 13) str += "D";
      else if(i == 14) str += "E";
      else str += "F";
    } else
      str += i;
    j -= 4;
  }
  return str;
}

function HLStoRGB(H,L,S) {

  var p1,p2;
  var rgb = new Array(3);
  if (L<=0.5) p2=L*(1+S);
  else p2=L+S-(L*S);
  p1=2*L-p2;

  if (S==0) {
    rgb.r=L;
    rgb.g=L;
    rgb.b=L;
  } else {
    rgb.r=RGB(p1,p2,H+120);
    rgb.g=RGB(p1,p2,H);
    rgb.b=RGB(p1,p2,H-120);
  }
  rgb.r *= 255;
  rgb.g *= 255;
  rgb.b *= 255;
  return rgb;
}

function RGB(q1,q2,hue) {
  if (hue>360) hue=hue-360;
  if (hue<0) hue=hue+360;
  if (hue<60) return (q1+(q2-q1)*hue/60);
  else if (hue<180) return(q2);
  else if (hue<240) return(q1+(q2-q1)*(240-hue)/60);
  else return(q1);
}
var initbk="000000";

function updatecolorbar(){
   document.getElementById("optd2").style.backgroundColor="#"+document.mainform.d2.value;
   document.getElementById("optd3").style.backgroundColor="#"+document.mainform.d3.value;
   document.getElementById("optl2").style.backgroundColor="#"+document.mainform.l2.value;
   document.getElementById("optbk").style.backgroundColor = document.getElementById("optl3").style.backgroundColor="#"+document.mainform.l3.value;
   document.getElementById("optbktext").style.color="#"+gethexforscheme(document.mainform.d3.value);
   document.getElementById("optbktext1").style.color="#"+gethexforscheme(document.mainform.d2.value);
   document.getElementById("optbktext2").style.color="#"+gethexforscheme(document.mainform.l2.value);
   document.getElementById("optbktext41").style.color = "#"+initbk;
   document.getElementById("optbktext42").style.color = "#"+initbk;
   document.getElementById("optbktext31").style.color = "#"+gethexforscheme(document.mainform.d2.value);
   document.getElementById("optbktext32").style.color = "#"+gethexforscheme(document.mainform.d2.value);
}

function get2var(v){
   if(v=="$dark2$")
     return "d2"
   if(v=="$dark3$")
     return "d3"
  if(v=="$light2$")
    return "l2"
  if(v=="$light3$")
    return "l3"
  return v;
}
function getdollarvar(v){
   if(v=="d2")
     return "$dark2$"
   if(v=="d3")
     return "$dark3$"
  if(v=="l2")
    return "$light2$"
  if(v=="l3")
    return "$light3$"
}
function gethexforscheme(sv){
   if(sv.indexOf("$")>=0)
      return document.mainform[get2var(sv)].value;
   else if(sv.length==2)
      return document.mainform[sv].value;
   else
      return sv;
}
function gg(a,b,c){gcolor(a,b,c);}
function setDefault(){
	document.mainform.defaultval.value = '1';
	document.mainform.submit();
}
