/** ----------------------------------------------------------------------------
 * file         : CanvasDraw.CommonClasses.js
 * file version : 1.0
 * date         : 2010-09-25
 * -----------------------------------------------------------------------------
 * author: grum at grum.fr
 * << May the Little SpaceFrog be with you >>
 *
 * This program is free software and is published under the terms of the GNU GPL
 * Please read CanvasDraw.ReadMe.txt file for more information
 *
 * -----------------------------------------------------------------------------
 *
 * dependencies : none
 *
 * -----------------------------------------------------------------------------
 *
 * provided classes :
 *  - CDOrigin
 *      . setX(value)
 *      . setY(value)
 *      . getX()
 *      . getY()
 *
 *  - CDPoint(x,y)
 *      . x
 *      . y
 *
 *  - CDRPoint(x,y,r)
 *      . x
 *      . y
 *      . r
 *
 *  - CDGradientStep(step, color)
 *      . step
 *      . color
 *
 * -----------------------------------------------------------------------------
 */



/**
 * The CDOrigin class give methods to manage origin values on abscissa an ordinate
 * It's used to determinate objects origin when positionning them

 *
 *
 * Object public properties
 *  - x : String "left" | "middle" | "right"
 *        origin on the abscissa
 *          default "left"
 *
 *  - y : String "top" | "middle" | "bottom"
 *        origin on the ordinate
 *          default "top"
 *
 *
 * Object public functions & methods
 * This object don't have any public functions & methods
 */
function CDOrigin () {

  this.properties = {
    x:"left",
    y:"top"
  }

  this.setX = function(value)
  {
    if((value=="left")||(value=="right")||(value=="middle")) this.properties.x = value;
    return(this.properties.x);
  }


  this.setY = function (value)
  {
    if((value=="top")||(value=="bottom")||(value=="middle")) this.properties.y = value;
    return(this.properties.y);
  }

}

/**
 * this CDPoint is used to defined a coordinate
 */
function CDPoint (x,y) {
  this.x=x;
  this.y=y;
}

/**
 * this CDRPoint is used to defined a radial coordinate (for radial gradient)
 */
function CDRPoint (x,y,r) {
  this.x=x;
  this.y=y;
  this.r=r;
}

/**
 * this CDGradientStep is used to defined a gradient step
 */
function CDGradientStep (step,color) {
  this.step=step;
  this.color=color;
}




/**
 * returns an object {r:RR, g:GG, b:BB} from a color string #RRGGBB
 *
 * if the given color is not a color, return r:0, g:0, b:0
 *
 * @param String value : color value
 */
function colorHexToRGBInt(value) {
  returned={r:0, g:0, b:0}

  re=/#([0-9A-F][0-9A-F])([0-9A-F][0-9A-F])([0-9A-F][0-9A-F])/i;
  rgb=re.exec(value);
  if(rgb!=null)
  {
    returned.r=parseInt('0x'+rgb[1]);
    returned.g=parseInt('0x'+rgb[2]);
    returned.b=parseInt('0x'+rgb[3]);
  }
  return(returned);
}


/**
 * returns a rgba() color
 *
 * @param String color : a #rrggbb color value
 * @param Float opacity : the opacity
 * @return String :
 */
function toRGBA(color, opacity) {
  rgb=colorHexToRGBInt(color);
  return('rgba('+rgb.r+', '+rgb.g+', '+rgb.b+', '+opacity+')');
}



/**
 * returns true if given color is a valid color string #rrggbb
 *
 * @param String value : color value
 */
function isValidColor(value) {
  re=/#([0-9A-F][0-9A-F])([0-9A-F][0-9A-F])([0-9A-F][0-9A-F])/i;
  if(re.exec(value)!=null) return(true);
  return(false);
}

/**
 * returns true if given opacity is valid
 *
 * @param Float value : color value
 */
function isValidOpacity(value) {
  if((value>=0)&&(value<=1)) return(true);
  return(false);
}


