
//*************************************************************************************************************************
//Set Hue and Saturation, build lightness table and store values about HUE and SATURATION
function getHueSat()
{var mouseX = parseInt(parseFloat(event.clientX)/196*360)
 var mouseY = parseInt(((parseInt(event.clientY)-208)/208)*(-100))
 var RBGcolors = new Array(3)

 //Set Hue,Sat colors value
 HueValue = mouseX
 SatValue = mouseY

 //Set RGB colors value
 RBGcolors = HLStoRGB(HueValue,LumValue,SatValue) 
 
 document.all.rV.value = parseInt(RBGcolors.r)
 document.all.gV.value = parseInt(RBGcolors.g)
 document.all.bV.value = parseInt(RBGcolors.b) 
 
 rValue = document.all.rV.value;
 gValue = document.all.gV.value;
 bValue = document.all.bV.value;	 

 //Set color preview
 setHueSat()
 buildColorTable() 
 
 return
}

//*************************************************************************************************************************
//Display color number in HEX as ALT
function getHexNum(){
 var mouseX = parseInt(parseFloat(event.clientX)/196*360)
 var mouseY = parseInt(((parseInt(event.clientY)-208)/208)*(-100))
 var RBGcolors = new Array(3)

 //Set Hue,Sat colors value
 HueValue = mouseX
 SatValue = mouseY

 //Set RGB colors value
 RBGcolors = HLStoRGB(HueValue,LumValue,SatValue) 
 
 rValue = parseInt(RBGcolors.r);
 gValue = parseInt(RBGcolors.g);
 bValue = parseInt(RBGcolors.b);

 //Set color Alt
 document.all('hstbl').alt = RGBtoHex(rValue,gValue,bValue)
 
 return
}
//*************************************************************************************************************************
//Display settings
function setHueSat()
{//Set color preview
 RBGcolors = HLStoRGB(HueValue,LumValue,SatValue) 
 document.all.SelectedColorPreview.bgColor=RGBtoHex(rValue,gValue,bValue)
 document.all.HEXaValue.value = RGBtoHex(rValue,gValue,bValue)
 window.returnValue = document.all.HEXaValue.value
 return
}

//*************************************************************************************************************************
//Set Luminosity and store values about LUMINOSITY
function getLightness(lightValue)
{var colors = new Array(3)
 document.images.SpaceTop.height = 200-Math.round(lightValue*2.04)-3
 document.images.SpaceDown.height = 200-document.images.SpaceTop.height-5

 //Set Lightness colors value
 LumValue = lightValue
 
 //Set RGB colors value
 colors = HLStoRGB(HueValue,LumValue,SatValue)
 document.all.rV.value = parseInt(colors.r)
 document.all.gV.value = parseInt(colors.g)
 document.all.bV.value = parseInt(colors.b) 
 
 rValue = document.all.rV.value;
 gValue = document.all.gV.value;
 bValue = document.all.bV.value;	 
 
 //Set color preview
 setHueSat() 
 
 return
}

//*************************************************************************************************************************
function TestColorsSetting(curValue,maxValue,RGBpart)
{var kCode = event.keyCode
 if (kCode == 13)
 {
 if ((isNaN(parseInt(curValue)))||(parseInt(curValue) > maxValue)) curValue = maxValue;
 if (parseInt(curValue) < 0) curValue = 0
			 
 if (RGBpart == "255r") document.all.rV.value = curValue;
  else 
   if (RGBpart == "255g") document.all.gV.value = curValue;
	else document.all.bV.value = curValue;

	rValue = document.all.rV.value;
	gValue = document.all.gV.value;
	bValue = document.all.bV.value;				 
					
    setRGB(rValue,gValue,bValue)
 }
 return
} 


//*************************************************************************************************************************
function setRGB(R,G,B)
{var hls = new Array(3)
 hls = RGBtoHLS(R,G,B)
 //Store new Hue, Saturation, Luminosity settings
 HueValue = parseInt(hls.h)
 SatValue = parseInt(parseFloat(hls.s)*100)
 LumValue = parseInt(parseFloat(hls.l)*100)
 buildColorTable() 
 
 document.images.SpaceTop.height = 200-Math.round(LumValue*2.04)-3
 document.images.SpaceDown.height = 200-document.images.SpaceTop.height-5

 setHueSat()
 return
}

//*************************************************************************************************************************
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 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 buildColorTable() 
{var j
 var rgb = new Array(3)
   //Lightness - 0 az 100%
   for(j=100;j>-1;j--)
    {rgb = HLStoRGB(HueValue,j,SatValue)
	 hex = RGBtoHex(rgb.r,rgb.g,rgb.b)
     document.all("CellLight"+j).bgColor = hex
 	}
 return
}

//*************************************************************************************************************************
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);
  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=RGB(p1,p2,H+120); //+120
    rgb.g=RGB(p1,p2,H);
    rgb.b=RGB(p1,p2,H-120); //-120
  }
  rgb.r = Math.round(255*rgb.r);
  rgb.g = Math.round(255*rgb.g);
  rgb.b = Math.round(255*rgb.b);
  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);
}

//*************************************************************************************************************************
function buildLightnessTable() 
{var cTable= new String()
 var j
  cTable = '<table width="9" height="202" cellpadding="0" align="center" cellspacing="0" border="0">'

   for(j=100;j>-1;j--)
    {cTable += '<tr><td id="CellLight'+j+'" style="cursor:crosshair" onclick="getLightness('+j+')" width="9" height="2"><img src="spacer.gif" width="9" height="1" border="0"></td></tr>'}
	
 cTable += '</table>'
//	alert(document.getElementById('LightnesTable');
 document.all('LightnesTable').innerHTML = cTable
 return
}

//*************************************************************************************************************************
function body_onload()
{/*window.returnValue = "#f57304";*/
 buildLightnessTable();
 buildColorTable();
}

//*************************************************************************************************************************
//Check keypress - ESC
function document_onkeyup()
{var kCode = event.keyCode
 if (kCode==27) {window.returnValue = -1;window.close()} //ESC
}

//*************************************************************************************************************************
//Show palete - Web Safe / 16 mil. colors
function ShowPalete(idPalete)
{if (idPalete == "Palete1")
 {document.all.rV.disabled = true
  document.all.gV.disabled = true
  document.all.bV.disabled = true

  //set palete visibility
  document.all[idPalete].style.display = "block"
  document.all["Palete2"].style.display = "none"  
 }
 else
 {document.all.rV.disabled = false
  document.all.gV.disabled = false
  document.all.bV.disabled = false
  
  //set palete visibility
  document.all[idPalete].style.display = "block"
  document.all["Palete1"].style.display = "none"  
  setRGB(rValue,gValue,bValue)  
 }
}
//*************************************************************************************************************************
//vytvoreni tabulky s web colory s volitelnou funkci
function DisplaySafePalete() {
 var ColTable = new String("<table border='0' cellpadding='0' cellspacing='2'>");
 var Colors = new Array("00","33","66","99","CC","FF")
 var ColorsIndex = new Array (0,5,0)
 var i,j
 
 BuildProces:
 for (i=0;i<16;i++)
  {ColTable += "<tr>"
   for (j=0;j<16;j++)
    {ColTable += "<td title='#"+Colors[ColorsIndex[0]]+Colors[ColorsIndex[1]]+Colors[ColorsIndex[2]]+"' onclick='ClickOnSafePalete(\"#"+Colors[ColorsIndex[0]]+Colors[ColorsIndex[1]]+Colors[ColorsIndex[2]]+"\")' style='cursor:hand' width='16' height='10' bgcolor='#"+Colors[ColorsIndex[0]]+Colors[ColorsIndex[1]]+Colors[ColorsIndex[2]]+"'></td>"
	 ColorsIndex[2]+=1
	 if (ColorsIndex[2] == 6)
	  {ColorsIndex[2] = 0
	   ColorsIndex[0] +=1
	   if (ColorsIndex[0] == 6)
	    {ColorsIndex[2] = 0
		 ColorsIndex[0] = 0
		 ColorsIndex[1]-=1
		 if (ColorsIndex[1] < 0) {break BuildProces;}
		}
	  }
	} 
   ColTable += "</tr>"
  }
ColTable += "</table>"
return ColTable
}

//*************************************************************************************************************************
function TestColorsSettingHex(hex){
  document.all.SelectedColorPreview.bgColor = hex
}
//*************************************************************************************************************************
function ClickOnSafePalete(hex)
{ document.all.HEXaValue.value = hex
  document.all.SelectedColorPreview.bgColor = hex
  window.returnValue = document.all.HEXaValue.value

  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;
  
 document.all.rV.value = parseInt(rgb.r)
 document.all.gV.value = parseInt(rgb.g)
 document.all.bV.value = parseInt(rgb.b) 
 
 rValue = document.all.rV.value;
 gValue = document.all.gV.value;
 bValue = document.all.bV.value;
}
//*************************************************************************************************************************
//*************************************************************************************************************************
var LumValue = 50; //50
var HueValue = 360; //360
var SatValue = 100; //100

var rValue = 0;
var gValue = 0;
var bValue = 0;


