
function RGBColorLite(color_string)
{
    this.ok = false;

    // strip any leading #
    if (color_string.charAt(0) == '#') { // remove # if any
        color_string = color_string.substr(1,6);
    }

    color_string = color_string.replace(/ /g,'');
    color_string = color_string.toLowerCase();


    // array of color definition objects
    var color_defs = [
        {
            re: /^rgb\((\d{1,3}),\s*(\d{1,3}),\s*(\d{1,3})\)$/,
            example: ['rgb(123, 234, 45)', 'rgb(255,234,245)'],
            process: function (bits){
                return [
                    parseInt(bits[1]),
                    parseInt(bits[2]),
                    parseInt(bits[3])
                ];
            }
        },
        {
            re: /^(\w{2})(\w{2})(\w{2})$/,
            example: ['#00ff00', '336699'],
            process: function (bits){
                return [
                    parseInt(bits[1], 16),
                    parseInt(bits[2], 16),
                    parseInt(bits[3], 16)
                ];
            }
        },
        {
            re: /^(\w{1})(\w{1})(\w{1})$/,
            example: ['#fb0', 'f0f'],
            process: function (bits){
                return [
                    parseInt(bits[1] + bits[1], 16),
                    parseInt(bits[2] + bits[2], 16),
                    parseInt(bits[3] + bits[3], 16)
                ];
            }
        }
    ];

    // search through the definitions to find a match
    for (var i = 0; i < color_defs.length; i++) {
        var re = color_defs[i].re;
        var processor = color_defs[i].process;
        var bits = re.exec(color_string);
        if (bits) {
            channels = processor(bits);
            this.r = channels[0];
            this.g = channels[1];
            this.b = channels[2];
            this.ok = true;
        }

    }

    // validate/cleanup values
    this.r = (this.r < 0 || isNaN(this.r)) ? 0 : ((this.r > 255) ? 255 : this.r);
    this.g = (this.g < 0 || isNaN(this.g)) ? 0 : ((this.g > 255) ? 255 : this.g);
    this.b = (this.b < 0 || isNaN(this.b)) ? 0 : ((this.b > 255) ? 255 : this.b);

    this.toHex = function () {
        var r = this.r.toString(16);
        var g = this.g.toString(16);
        var b = this.b.toString(16);
        if (r.length == 1) r = '0' + r;
        if (g.length == 1) g = '0' + g;
        if (b.length == 1) b = '0' + b;
        return r + g + b;
    }

}
function cascadedstyle(ele, cssproperty, csspropertyNS){
el=document.getElementById(ele)
if (el.currentStyle)
return el.currentStyle[cssproperty]
else if (window.getComputedStyle){
var elstyle=window.getComputedStyle(el, "")
return elstyle.getPropertyValue(csspropertyNS)
}
}
var n=0

var zkleur = new RGBColorLite(cascadedstyle("box1", "backgroundColor", "background-color"));
var wkleur = new RGBColorLite(cascadedstyle("balk", "backgroundColor", "background-color"));
var zfkleur=  new RGBColorLite(cascadedstyle("box1", "color", "color"));
var z=zkleur.toHex()
var w=wkleur.toHex()
var zf=zfkleur.toHex()

var hexChars = "0123456789ABCDEF";
	//document.writeln ('GG '+z+' | ' + ww+ ' | <BR>'); 
function initArray()
{
  this.length = initArray.arguments.length
  for (var i = 0; i < this.length; i++)
    this[i+1] = initArray.arguments[i]
}
function Dec2Hex (Dec)
 {
  var a = Dec % 16;
  var b = (Dec - a)/16;
  hex = "" + hexChars.charAt(b) + hexChars.charAt(a);
  return hex;
}
function CHANGEBG (begin,eind) {  
  redA     = begin.charAt(1) + begin.charAt(2);
  red_valA = parseInt(redA,'16');
  grnA     = begin.charAt(3) + begin.charAt(4);
  grn_valA = parseInt(grnA,'16');
  grnB     = eind.charAt(3) + eind.charAt(4);
  grn_valB = ((parseInt(grnB,'16') ));
  bluA     = begin.charAt(5) + begin.charAt(6);
  blu_valA = parseInt(bluA,'16');
  bluB     = eind.charAt(5) + eind.charAt(6);
  blu_valB = ((parseInt(bluB,'16')));
  redB     = eind.charAt(1) + eind.charAt(2);
  red_valB = ((parseInt(redB,'16')));
  var gr=0
  if (red_valB > red_valA) {gr=gr+1}
  if (blu_valB > blu_valA) {gr=gr+1}
  if (grn_valB > grn_valA) {gr=gr+1}
	bijr=Math.round(((2*red_valA)+red_valB)/40);
	bijg=Math.round(((2*grn_valA)+grn_valB)/40);
	bijb=Math.round(((2*blu_valA)+blu_valB)/40);
	if (gr >= 2) 
	{ var redC=red_valA;
	var bluC=blu_valA;
	var grnC=grn_valA;
	red_valA=red_valB;
	grn_valA=grn_valB;
	blu_valA=blu_valB;
	blu_valB=bluC;
	grn_valB=grnC;
	red_valB=redC;
	bijr=bijr*3
	bijb=bijb*3
	bijg=bijg*3	
	  }		
	red_int=red_valA;
	blu_int=blu_valA;
	grn_int=grn_valA;
		//document.writeln (bijr+' ' + bijg+ ' ' +bijb+'<BR>'); 

	if (red_valB <= grn_valB )
	 {
	 if (red_valB <= blu_valB ) 
	 		{red_int  = ((red_valA - bijr) );
	 		if (red_valB = blu_valB ){grn_int  = ((grn_valA - bijg )); }	 		}
		}
	else
			{	
			if (grn_valB <=  blu_valB) 
				{				grn_int  = ((grn_valA - bijg )); 
				if (grn_valB =  blu_valB) {blu_int  = ((blu_valA - bijb ) ); }
			}
			else
				{	blu_int  = ((blu_valA - bijb ) ); 			}
	}
    red = red_int;
    red_round = Math.round(red);
    red_hex = Dec2Hex(red);
    grn = grn_int;
    grn_round = Math.round(grn);
    grn_hex = Dec2Hex(grn);
    blu = blu_int;
    blu_round = Math.round(blu);
    blu_hex = Dec2Hex(blu);
  	s = '#'+red_hex + grn_hex + blu_hex;
	//document.writeln ('GG '+begin+' | ' + eind+ ' | ' +s+'<BR>'); 

  return s;

}
