var tempRGB = new Array(3);
var RGB = new Array(256);
var HSL = new Array(3);
var red=0,blue=0,green=0,compRed=0,compGreen=0,compBlue=0;
var increment=0;
var k = 0;
var hex = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F");
for (i = 0; i < 16; i++) 
{	
	for (j = 0; j < 16; j++) 
	{		
		RGB[k] = hex[i] + hex[j];
		k++;
	}
}

function GetColorSet(r,g,b)
{
	if (r>255) r=255;
	if (g>255) g=255;
	if (b>255) b=255;
	if (r<0) r=0;
	if (g<0) g=0;
	if (b<0) b=0;
	codes.r.value=r;
	codes.g.value=g;
	codes.b.value=b;
	codes.hex.value="#"+RGB[r]+RGB[g]+RGB[b];
	document.all.color.style.backgroundColor="#"+RGB[r]+RGB[g]+RGB[b];
	if (eval(r)>153||eval(g)>153||eval(b)>153)
		increment=-51;
	else
		increment=51;
	RGBtoHSL(r,g,b);
	GetMono(eval(r)+eval(increment),eval(g)+eval(increment),eval(b)+eval(increment),1);
	GetWheel();
}

function GetWheel()
{
	tempHSL=HSL[0];
	for (i = 0; i < 4; i++) 
	{
		tempRGB=HSLtoRGB(HSL[0],HSL[1],HSL[2]);
		red=RGB[tempRGB.r];
		green=RGB[tempRGB.g];
		blue=RGB[tempRGB.b];
		document.all.row1[i].style.backgroundColor="#" + red + green + blue;
		document.all.row1HEX[i].innerHTML="#" + red + green + blue;
		document.all.row1RGB[i].innerHTML="<a href='javascript:GetColorSet("+tempRGB.r+","+tempRGB.g+","+tempRGB.b+")'>"+tempRGB.r+","+tempRGB.g+","+tempRGB.b+"</a>";
	  HSL[0]+=30;
		if (HSL[0]>=360)
			HSL[0]-=360;
	}

		tempRGB=HSLtoRGB(HSL[0],HSL[1],HSL[2]);
		red=RGB[tempRGB.r];
		green=RGB[tempRGB.g];
		blue=RGB[tempRGB.b];
		document.all.row2[3].style.backgroundColor="#" + red + green + blue;
		document.all.row2HEX[3].innerHTML="#" + red + green + blue;
		document.all.row2RGB[3].innerHTML="<a href='javascript:GetColorSet("+tempRGB.r+","+tempRGB.g+","+tempRGB.b+")'>"+tempRGB.r+","+tempRGB.g+","+tempRGB.b+"</a>";
	  HSL[0]+=30;
		if (HSL[0]>=360)
			HSL[0]-=360;
		
		tempRGB=HSLtoRGB(HSL[0],HSL[1],HSL[2]);
		red=RGB[tempRGB.r];
		green=RGB[tempRGB.g];
		blue=RGB[tempRGB.b];
		document.all.row3[3].style.backgroundColor="#" + red + green + blue;
		document.all.row3HEX[3].innerHTML="#" + red + green + blue;
		document.all.row3RGB[3].innerHTML="<a href='javascript:GetColorSet("+tempRGB.r+","+tempRGB.g+","+tempRGB.b+")'>"+tempRGB.r+","+tempRGB.g+","+tempRGB.b+"</a>";
	  HSL[0]+=30;
		if (HSL[0]>=360)
			HSL[0]-=360;
			
	for (i = 3; i >= 0; i--) 
	{
		tempRGB=HSLtoRGB(HSL[0],HSL[1],HSL[2]);
		red=RGB[tempRGB.r];
		green=RGB[tempRGB.g];
		blue=RGB[tempRGB.b];
		document.all.row4[i].style.backgroundColor="#" + red + green + blue;
		document.all.row4HEX[i].innerHTML="#" + red + green + blue;
		document.all.row4RGB[i].innerHTML="<a href='javascript:GetColorSet("+tempRGB.r+","+tempRGB.g+","+tempRGB.b+")'>"+tempRGB.r+","+tempRGB.g+","+tempRGB.b+"</a>";
	  HSL[0]+=30;
		if (HSL[0]>=360)
			HSL[0]-=360;
	}
		tempRGB=HSLtoRGB(HSL[0],HSL[1],HSL[2]);
		red=RGB[tempRGB.r];
		green=RGB[tempRGB.g];
		blue=RGB[tempRGB.b];
		document.all.row3[0].style.backgroundColor="#" + red + green + blue;
		document.all.row3HEX[0].innerHTML="#" + red + green + blue;
		document.all.row3RGB[0].innerHTML="<a href='javascript:GetColorSet("+tempRGB.r+","+tempRGB.g+","+tempRGB.b+")'>"+tempRGB.r+","+tempRGB.g+"."+tempRGB.b+"</a>";
	  HSL[0]+=30;
		if (HSL[0]>=360)
			HSL[0]-=360;
		
		tempRGB=HSLtoRGB(HSL[0],HSL[1],HSL[2]);
		red=RGB[tempRGB.r];
		green=RGB[tempRGB.g];
		blue=RGB[tempRGB.b];
		document.all.row2[0].style.backgroundColor="#" + red + green + blue;
		document.all.row2HEX[0].innerHTML="#" + red + green + blue;
		document.all.row2RGB[0].innerHTML="<a href='javascript:GetColorSet("+tempRGB.r+","+tempRGB.g+","+tempRGB.b+")'>"+tempRGB.r+","+tempRGB.g+","+tempRGB.b+"</a>";
	  HSL[0]+=30;
		if (HSL[0]>=360)
			HSL[0]-=360;


	HSL[0]=tempHSL;
}


function HSLtoRGB(H,S,L) {
  var p1,p2;
  var rgb = new Array(3);
  L/=100;
  S/=100;
  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=FindRGB(p1,p2,H+120);
    rgb.g=FindRGB(p1,p2,H);
    rgb.b=FindRGB(p1,p2,H-120);
  }
  rgb.r *= 255;
  rgb.g *= 255;
  rgb.b *= 255;
  rgb.r=Math.round(rgb.r);
  rgb.g=Math.round(rgb.g);
  rgb.b=Math.round(rgb.b);	
  return rgb;
}

function FindRGB(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);
}


function GetMono(r,g,b,index)
{
	if(eval(r)<0)
		r=0;
	if(eval(g)<0)
		g=0;
	if(eval(b)<0)
		b=0;
	if(eval(r)>255)
		r=255;
	if(eval(g)>255)
		g=255;
	if(eval(b)>255)
		b=255;
	red=RGB[r];
	green=RGB[g];
	blue=RGB[b];

	compRed=RGB[255-eval(r)];
	compGreen=RGB[255-eval(g)];
	compBlue=RGB[255-eval(b)];

	document.all.row2[index].style.backgroundColor="#"+red+green+blue;
	document.all.row2HEX[index].innerHTML="#"+red+green+blue;
	document.all.row3[index].style.backgroundColor="#"+compRed+compGreen+compBlue;
	document.all.row3HEX[index].innerHTML="#"+compRed+compGreen+compBlue;
	row2RGB[index].innerHTML="<a href='javascript:GetColorSet("+r+","+g+","+b+")'>"+r+","+g+","+b+"</a>";
	row3RGB[index].innerHTML="<a href='javascript:GetColorSet("+(255-eval(r))+","+(255-eval(g))+","+(255-eval(b))+")'>"+(255-eval(r))+","+(255-eval(g))+","+(255-eval(b))+"</a>";

	
	if (eval(index)<3)
		GetMono(eval(r)+eval(increment),eval(g)+eval(increment),eval(b)+eval(increment),eval(index)+1);
	
}

function RGBtoHSL(r,g,b)
{
	
	var Min=0;
	var Max=0;
	r=(eval(r)/51)*.2;
	g=(eval(g)/51)*.2;
	b=(eval(b)/51)*.2;

	if (eval(r)>=eval(g))
		Max=eval(r);
	else
		Max=eval(g);
	if (eval(b)>eval(Max))
		Max=eval(b);
	
	if (eval(r)<=eval(g))
		Min=eval(r);
	else
		Min=eval(g);
	if (eval(b)<eval(Min))
		Min=eval(b);

	HSL[2]=(eval(Max)+eval(Min))/2;
	if (eval(Max)==eval(Min))
	{
		HSL[1]=0;
		HSL[0]=0;
	}
	else
	{
		if (HSL[2] < .5)
			HSL[1]=(eval(Max)-eval(Min))/(eval(Max)+eval(Min));
		if (HSL[2] >= .5)
			HSL[1]=(eval(Max)-eval(Min))/(2-eval(Max)-eval(Min));
		if (r==Max)
			HSL[0] = (eval(g)-eval(b))/(eval(Max)-eval(Min));
		if (g==Max)
			HSL[0] = 2+((eval(b)-eval(r))/(eval(Max)-eval(Min)));
		if (b==Max)
			HSL[0] = 4+((eval(r)-eval(g))/(eval(Max)-eval(Min)));
	}
	HSL[0]=Math.round(HSL[0]*60);
	if(HSL[0]<0) HSL[0] += 360;
  	if(HSL[0]>=360) HSL[0] -= 360;
	HSL[1]=Math.round(HSL[1]*100);
	HSL[2]=Math.round(HSL[2]*100);
	
}
