/**
NEED : null
SOFTWARE: IE, FIREFOX, MOZILLA (le navigateur doit seulement gerer '.style.backgroundColor')

L'objet Kcolor stock une couleur faite de rouge de vert et de bleu et un objethtml lié à cette couleur
l'utilisateur gere ces couleurs en nombre entier sur l'intervale [0 a 255}] (pas d'hexadecimale, car géré par la classe)

XxX= Red (rouge) ou Green (vert)ou Blue (bleu)
*/
function KColor(htmlLinkedObj) {

var unblock;

var noColor;
/**
_htmlLinkedObj : objethtml lié à cette objet KColor
*/

var _htmlLinkedObj;


/**
this._Colorval[0] : valeur du rouge
this._Colorval[1] : valeur du vert
this._Colorval[2] : valeur du bleu */
var _Colorval;




this.set=function(index,val){
	if (val){
	if(this.unblock){
		val=Math.round(val);
this._Colorval[index]=val;	
	}}
}

this.get=function(index){
	if(this._Colorval[index]!=null){
		val=this._Colorval[index];
		if(val<0)
		this._Colorval[index] = 0;
	if (val>255)
		this._Colorval[index] = 255;
	}
return this._Colorval[index];
}



this.setColor=function(color){
this.setR(color.getR());
this.setG(color.getG());
this.setB(color.getB());
this.noColor=color.noColor;
}


/*
this.setXxX affecté la valeur du XxX sur l'interval [0, 255];
need: function -this.set();
*/
this.setR=function(val){this.set(0,val);this.setPhysColor();}
this.setG=function(val){this.set(1,val);this.setPhysColor();}
this.setB=function(val){this.set(2,val);this.setPhysColor();}

/*
this.incXxX incremente la valeur XxX;
this.decXxX decremente la valeur XxX;
need: function -this.set(index,val);
*/
this.incR=function(val){this.setR(this.get(0)+val);this.setPhysColor();}
this.decR=function(val){this.setR(this.get(0)-val);this.setPhysColor();}
this.incG=function(val){this.setG(this.get(1)+val);this.setPhysColor();}
this.decG=function(val){this.setG(this.get(1)-val);this.setPhysColor();}
this.incB=function(val){this.setB(this.get(2)+val);this.setPhysColor();}
this.decB=function(val){this.setB(this.get(2)-val);this.setPhysColor();}

/*
this.getXxX renvois la valeur XxX;
need: function -this.get(index);
*/
this.getR=function(){return this.get(0);}
this.getG=function(){return this.get(1);}
this.getB=function(){return this.get(2);}
	
/*
this.setPhyColor applique la couleur à l'objet;
need: function -this.getXxX;
*/
this.setPhysColor=function(){

	//if (this.getR()!=-1 && this.getG()!=-1 && this.getB()!=-1 ){
if(this.noColor){
	this._htmlLinkedObj.style.backgroundColor="";
	}else{
this._htmlLinkedObj.style.backgroundColor="#"+this.toHexa(this.getR())+this.toHexa(this.getG())+this.toHexa(this.getB());}
/*}else{
	this._htmlLinkedObj.style.backgroundColor="";
	}*/
}

this.setWoC=function(){
	this.noColor=true;
	this.setPhysColor();
	}
this.setWC=function(){
	this.noColor=false;
	this.setPhysColor();
	}

/*
this.createLink crée le lien entre l'objet et l'objet Html;
*/
this.createLink=function(htmlLinkedObj){
	this._htmlLinkedObj=htmlLinkedObj;
}

/*
toHexa est transforme un nombre entier sur l'interval [0, 255] en hexadecimal;
*/
 this.toHexa=function(n){
	function parthexa(c){
		switch(c){
		case 10: c="A"; break;
		case 11: c="B"; break;
		case 12: c="C"; break;
		case 13: c="D"; break;
		case 14: c="E"; break;
		case 15: c="F"; break;
		default:
		}
		return c;
		}
		
	if (n>255)
		n=255;
	if (n<0)
		n=0;
	
	o1=n%16;
	o2=Math.floor(n/16);
	o1=parthexa(o1);
    o2=parthexa(o2);
	
	
	m=o2+""+o1;
	if(m.length<2)
	m="0"+m;
	return(m);
}

this.unlock=function(){this.unblock=true;}
this.lock=function(){this.unblock=false;}

/*
this.constructor initialise l'objet
*/

this.construtor=function(htmlLinkedObj){
	this.createLink(htmlLinkedObj);
	this.noColor=true;
	this._Colorval= new Array(-1,-1,-1);
	this.unlock();
	
	this.setR(0);
	this.setG(0);
	this.setB(0);
	}

this.construtor(htmlLinkedObj);
	
}
