/////////////////////////////////////////////////////////////////////////
// Color object with alpha
/////////////////////////////////////////////////////////////////////////
function AColor( r, g, b, a )
{
  this.r = r;
  this.g = g;
  this.b = b;
  this.a = a;
  this.key = null;
}

AColor.prototype.clone = function() {
  var color = new Color( this.r, this.g, this.b, this.a );
  return color;
}

AColor.prototype.toString = function() {
  if ( this.stringValue == null ) this.stringValue = 'AColor{'+this.r+','+this.g+','+this.b+','+this.a+'}';
  return this.stringValue;
}

AColor.hexCache = new Object();
AColor.color2Hex = function(n) { return (n.toString(16).length < 2) ? '0' + n.toString(16) : n.toString(16); }
AColor.prototype.toHex = function() {
  var hexValue = AColor.hexCache[this.toString()];
  if ( hexValue == null ) {
    hexValue = '#' + AColor.color2Hex(Math.floor(255*this.r)) + AColor.color2Hex(Math.floor(255*this.g)) + AColor.color2Hex(Math.floor(255*this.b));
    AColor.hexCache[this.stringValue] = hexValue;
  }
  return hexValue;
}

AColor.mergeColor = function(oldColor, newColor) {
    return new AColor(
        (1-newColor.a)*oldColor.r + newColor.a*newColor.r,
        (1-newColor.a)*oldColor.g + newColor.a*newColor.g,
        (1-newColor.a)*oldColor.b + newColor.a*newColor.b,
        Math.max(oldColor.a,newColor.a) );
}

AColor.fromHex = function( hexColor, alpha )
{
  a = ( arguments.length > 1 ? alpha : 1 );
  if ( hexColor.startsWith('#') )
    hexColor = hexColor.substring(1);
  return new AColor( parseInt(hexColor.substring(0,2),16)/255, parseInt(hexColor.substring(2,4),16)/255,
                     parseInt(hexColor.substring(4),16)/255, a);
}

AColor.setBackgroundColor = function( element, colorMap, background )
{
  var color = AColor.getColorForClasses( element, colorMap, background );
  if ( color )
    element.style.backgroundColor = color.toHex();
}

AColor.setBorderColor = function( element, colorMap, background, top, bottom, right, left )
{
  var color = AColor.getColorForClasses( element, colorMap, background );
  if ( color )
  {
    if ( top )
      element.style.borderTopColor = color.toHex();
    if ( bottom )
      element.style.borderBottomColor = color.toHex();
    if ( right )
      element.style.borderRightColor = color.toHex();
    if ( left )
      element.style.borderLeftColor = color.toHex();
  }
}

AColor.getColorForClasses = function( element, colorMap, background )
{
  var finalColor = background;
  var styles = element.className.split(' ');
  for ( var i=0; i < styles.length; i++ )
  {
    var color = colorMap[styles[i]];
    if ( color )
    {
      if ( finalColor == null )
        finalColor = color;
      else
        finalColor = AColor.mergeColor( finalColor, color );
    }
  }
  return finalColor;
}

AColor.getDarkened = function( acolor ) { return new AColor( acolor.a / 2, acolor.g / 2, acolor.b / 2, acolor.a ); }
AColor.getLightened = function( acolor ) { return new AColor( (1+acolor.a) / 2, (1+acolor.g) / 2, (1+acolor.b) / 2, acolor.a ); }


