/*
* Taken from:
* http://krazydad.com/makecolors.php
* Except for a few changes to colorText(),
* it's all his code.
*/

function RGB2Color(r,g,b)
{
  return '#' + byte2Hex(r) + byte2Hex(g) + byte2Hex(b);
}

function byte2Hex(n)
{
  var nybHexString = "0123456789ABCDEF";
  return String(nybHexString.substr((n >> 4) & 0x0F,1)) + nybHexString.substr(n & 0x0F,1);
}


function roundDecimal(v,n)
{
  var isNeg = v < 0;
  v = Math.abs(v);

  return (isNeg? '-' : '') + String(Math.floor(v)) + '.' + String((1 + Math.abs(v) - Math.floor(Math.abs(v)))*Math.pow(10,n)).substr(1,n);
}

function colorText(str,phase)
{
  if (phase == undefined) {phase=0;}
  center = 128;
  width = 127;
  frequency = Math.PI*64/str.length;
	var t="";
  for (var i = 0; i < str.length; ++i)
  {
     red   = Math.sin(frequency*i+2+phase) * width + center;
     green = Math.sin(frequency*i+0+phase) * width + center;
     blue  = Math.sin(frequency*i+4+phase) * width + center;
     t+='<span style="color:' + RGB2Color(red,green,blue) + ';">' + str.substr(i,1) + '</span>';
  }
	return t;
}

function makeColorGradient(frequency1, frequency2, frequency3, phase1,phase2,phase3,center,width,len)
{
  if (len == undefined)
    len = 50;
  if (center == undefined)
    center = 128;
  if (width == undefined)
    width = 127;
  for (var i = 0; i < len; ++i)
  {
     var red = Math.sin(frequency1*i + phase1) * width + center;
     var grn = Math.sin(frequency2*i + phase2) * width + center;
     var blu = Math.sin(frequency3*i + phase3) * width + center;
     document.write('<span style="color:' + RGB2Color(red,grn,blu) + '">&#9608;</span>');
  }
}
