
function make_objects () {
	infoDivObj = new getObj('infoDiv');
	descDivObj = new getObj('descDiv');
}//made objs
function getObj(name)	{


  	if (document.getElementById) { return document.getElementById(name).style; }
  	if (document.all) { return document.all[name].style; }
	if (document.layers) { return document.layers[name]; }
	
}// got objs

function makeColorObj(name) {

	this.name = name;
	this.hex = "CC";
	this.dec = 204;
	this.lock = 0;
	return this;
	
}

redObj = new makeColorObj("red");
greenObj = new makeColorObj("green");
blueObj = new makeColorObj("blue");

colorObj = new Object();
colorObj.red = redObj; colorObj.green = greenObj; colorObj.blue = blueObj;
var thingToColor = "body";

//***********************OK that's object creation ***********************************************



function main (currentColor, decColor) {
	if (colorObj[currentColor].lock) {return 0};
	colorObj[currentColor].name = currentColor;
	colorObj[currentColor].dec = decColor;
	colorObj[currentColor].hex =  getHexColor(decColor);

	//alert ("So far this color is " + colorObj[currentColor].name + " " + colorObj[currentColor].dec + " "+colorObj[currentColor].hex);

	setBoxes (currentColor);
	setItemColor();
	moveArrows(currentColor, decColor);
	makeyuv ();
	
	moveCross (ciex, ciey);
	
}//main



function setItemColor () {

	allcol = colorObj.red.hex + colorObj.green.hex  + colorObj.blue.hex;
	if (thingToColor == "body") {
		document.bgColor= eval(hexformat);
		return;
	}
	//else set text color
	infoDivObj.color= eval(hexformat);
	descDivObj.color=eval(hexformat);
	//document.fgColor= eval(hexformat);
}//set color


function setBoxes (currentColor) {

	hexformat= "\"\#" + colorObj.red.hex + colorObj.green.hex  + colorObj.blue.hex + "\"";
	decformat= colorObj.red.dec + "\, " + colorObj.green.dec + "\, " + colorObj.blue.dec;
	if (colorObj[currentColor].lock) {return 0};
	document.forms[1].elements[currentColor + "hexbox"].value = colorObj[currentColor].hex;
	document.forms[1].elements[currentColor + "decbox"].value = colorObj[currentColor].dec;

	if (thingToColor == "body") {
		document.alldisplay.rgbval.value=(decformat);
		document.alldisplay.hexval.value=(hexformat);
		return 0;
	}
	document.forms[2].elements['textHexValue'].value=(hexformat);
	document.forms[2].elements['textRgbValue'].value=(decformat);

}//set boxes

function getHexColor (decColor) {

var hexnumbers = "0123456789ABCDEF";

	hexColor = Math.floor(decColor / 16);
	hexdig10 = hexnumbers.substring(hexColor,hexColor+1);
	hexColor *=16;
	hexColor = decColor - hexColor;
	hexdig01 = hexnumbers.substring(hexColor,hexColor+1);
	hexColor = hexdig10 + hexdig01;
	return hexColor;

} //get hex

function moveArrows (col, topPos) {

arrowHeight = ((255 - ( eval(topPos) ) ) -130); // takes  height into calcs for pos
var MoverObj;
	if (document.getElementById) {
		moverObj = document.getElementById(col + "_arrow").style
		moverObj.top = arrowHeight + "px"
	}
	else {
		//****that should really be the test as to which code gets loaded, unless we get into some freakish mac ie thing
		//alert ("doink");
		"document." + col + "_arrow.top = topPos"
	}
}//fn

function moveCross (xPos, yPos) {
//so I guess we'll try and scale the coords in here...
// we know that we have 386 useable pixels, that scale from 0.0 to 0.8 nad that out of those we get to .6 or so at best
// and then the image is at 500 x and 15 y , then the left edge to the grid is 23 pix, and top to top of grid is 42, to bottom (.8) 428
//Zo...

xPos *= 480.25;
xPos += 514;	//23, the offset, less the browser margin window thing
xPos = Math.round(xPos);

yPos = 1-yPos;
yPos *= 480.25;
yPos -= 54;
yPos += 15;	//to match the div
yPos = Math.round (yPos);
var crossMover;
	if (document.getElementById) {
		crossMover = document.getElementById('crossDiv').style
		//alert (crossDiv);
		crossMover.top = yPos + "px";
		crossMover.left = xPos + "px";
	}
	else {
	document.crossDiv.top = yPos;
	}
} // cross mover

function makeyuv () {
	var rdecset = colorObj.red.dec;
	var gdecset = colorObj.green.dec;
	var bdecset = colorObj.blue.dec;
	var Y;
 	var X;
 	var Z;
	
var MatrixR = 0.299;
var	MatrixG = 0.587;
var	MatrixB = 0.114;
//	Y = Math.round (( ( (MatrixR * rdecset) + (MatrixG * gdecset) + (MatrixB * bdecset) ) * (235-16) / 255) + 16);
//	Cb = Math.round ((0.5 / (1 - MatrixB) * (bdecset - ((MatrixR * rdecset) + (MatrixG * gdecset) + (MatrixB * bdecset))) * (240-16) / 255) + 128);
//	Cr = Math.round ((0.5 / (1 - MatrixR) * (rdecset - ((MatrixR * rdecset) + (MatrixG * gdecset) + (MatrixB * bdecset))) * (240-16) / 255) + 128);
//function as pasted from Vince Cerundolo @ discreet

//*************now let's paste something from easyrgb.com


var_R = ( rdecset / 255 )        //R = From 0 to 255
var_G = ( gdecset / 255 )        //G = From 0 to 255
var_B = ( bdecset / 255 )        //B = From 0 to 255


if ( var_R > 0.04045 ) {
	var_R = (( var_R + 0.055 ) / 1.055 );
	var_R = Math.pow (var_R, 2.4);
	
	}
	else	{var_R = var_R / 12.92};
if ( var_G > 0.04045 ) var_G = Math.pow( ( ( var_G + 0.055 ) / 1.055 ), 2.4);
	else	var_G = var_G / 12.92;
//document.alldisplay.errBox.value=var_G;

if ( var_B > 0.04045 ) var_B = Math.pow( ( ( var_B + 0.055 ) / 1.055 ), 2.4);
else	var_B = var_B / 12.92;

var_R = var_R * 100
var_G = var_G * 100
var_B = var_B * 100

//Observer. = 2°, Illuminant = D65
X = (var_R * 0.4124) + (var_G * 0.3576) + (var_B * 0.1805);
Y = (var_R * 0.2126) + (var_G * 0.7152) + (var_B * 0.0722);
Z = (var_R * 0.0193) + (var_G * 0.1192) + (var_B * 0.9505);

//and now to Yxy...
//X = From 0 to  95.047       Observer. = 2°, Illuminant = D65
//Y = From 0 to 100.000
//Z = From 0 to 108.883

//Y = Y;
Y = Math.round (Y*100) / 100;
ciex = Math.round(( X / ( X + Y + Z ) ) * 1000) / 1000;
ciey = Math.round((Y / ( X + Y + Z ) ) * 1000) / 1000;



	document.alldisplay.Yval.value=X;
	document.alldisplay.Cbval.value=Y;
	document.alldisplay.Crval.value=Z;

	document.alldisplay.Yxy_Y.value=Y;
	document.alldisplay.Yxy_x.value=ciex;
	document.alldisplay.Yxy_y.value=ciey;
}// formerly the function known as Yuv


function DoLock (lockcol) {

	if (colorObj[lockcol].lock == 0) {
		document.forms[1].elements[lockcol + "statbox"].value = "Lock at " + colorObj[lockcol].dec
		colorObj[lockcol].lock =1;
		return 0;
	}
	document.forms[1].elements[lockcol + "statbox"].value = "Unlocked"
	colorObj[lockcol].lock =0;
}

