/**
 * Color Tools Javascript
 * 
 * Contiene varias clases para manipular colores en Javascript.
 * Basado en la versión 0.5 de Color Tools PHP por
 * Jack Sleight - www.reallyshiny.com
 *
 * @author Pedro Fuentes <pedro.fuentes@oxus.cl>
 * @version 0.1
 * @license Creative Commons Attribution-ShareAlike 2.5 License
 */

colorTools = {
	
	/**
	 * Convert Hex code string to RGB array
	 * @param string hex hexadecimal color code
	 * @return array
	 */
	hexToRgb : function(hex){
		
		regex		= hex.replace('#', '');
		rgb			= new Array();
		
		rgb['r']	= colorTools.hexdec(regex.substr(0,2));
		rgb['g']	= colorTools.hexdec(regex.substr(2,2));
		rgb['b']	= colorTools.hexdec(regex.substr(4,2));
		
		return rgb;
		
	},
	
	/**
	 * Convert RGB array to Hex code string
	 * @param array rgb
	 * @return string
	 */
	rgbToHex : function(rgb){
		return sprintf('%02x', rgb['r']) + sprintf('%02x', rgb['g']) + sprintf('%02x', rgb['b']);
	},
	
	/**
	 * Create gradient from one color to another
	 * @param col1 string
	 * @param col2 string
	 * @param steps integer
	 * @return array
	 */
	gradient : function(col1, col2, steps){
		gradient	= new Array();
		step		= new Array();
		
		col1 = colorTools.hexToRgb(col1);
		col2 = colorTools.hexToRgb(col2);
		
		step['r'] = (col1['r'] - col2['r']) / (steps - 1);
		step['g'] = (col1['g'] - col2['g']) / (steps - 1);
		step['b'] = (col1['b'] - col2['b']) / (steps - 1);
		
		for(i = 0; i <= steps; i++) {
			
			color		= new Array();
				
			color['r']	= Math.round(col1['r'] - (step['r'] * i));
			color['g']	= Math.round(col1['g'] - (step['g'] * i));
			color['b']	= Math.round(col1['b'] - (step['b'] * i));
			
			gradient[i]	= colorTools.rgbToHex(color);
			
		}
		
		return gradient;
	},
	
	/**
	 * Create a text gradient from one color to another
	 * @param col1 string
	 * @param col2 string
	 * @param str string
	 * @return array
	 */
	textGradient : function(col1, col2, str){
		gradient	= new Array();
		step		= new Array();
		
		col1 = colorTools.hexToRgb(col1);
		col2 = colorTools.hexToRgb(col2);
		
		step['r']	= (col1['r'] - col2['r']) / (str.length - 1);
		step['g']	= (col1['g'] - col2['g']) / (str.length - 1);
		step['b']	= (col1['b'] - col2['b']) / (str.length - 1);
		
		var res			= '';
		
		for(i = 0; i <= str.length; i++) {
			
			color		= new Array();
				
			color['r']	= Math.round(col1['r'] - (step['r'] * i));
			color['g']	= Math.round(col1['g'] - (step['g'] * i));
			color['b']	= Math.round(col1['b'] - (step['b'] * i));
			
			res	= res + '<span style="color: #' + colorTools.rgbToHex(color) + '">' + str.charAt(i) + '</span>';
			
		}
		
		return res;
	},
	
	/**
	 * Invert a color
	 * @param col string
	 * @return string
	 */
	invert : function(col){
		col1	= colorTools.hexToRgb(col);
		col2	= new Array();
		
		col2['r']	= 255 - col1['r'];
		col2['g']	= 255 - col1['g'];
		col2['b']	= 255 - col1['b'];
		
		return colorTools.rgbToHex(col2);
		
	},
	
	/**
	 * Checks if two colors adhere to the W3C
	 * recommendations for brightness and color contrast
	 * @param col1 string
	 * @param col2 string
	 * @return bool
	 */
	compare : function(col1,col2){
		var brightnessDifference	= colorTools.brightnessDifference(col1,col2);
		var colorDifference			= colorTools.colorDifference(col1,col2);
		
		return ( ( (brightnessDifference > 125) && (colorDifference > 500) ) ? true : false );
	},
	
	/**
	 * Get the brightness value of a color
	 * @param col string
	 * @return float
	 */
	brightness : function(col){
		
		col	= colorTools.hexToRgb(col);
		
		return ( ( (col['r'] * 299) + ( col['g'] * 587 ) + ( col['b'] * 114 ) ) / 1000 );
	},
	
	/**
	 * Get the brightness difference of two colors
	 * @param col1 string
	 * @param col2 string
	 * @return int
	 */
	brightnessDifference : function(col1,col2){
		return Math.abs( colorTools.brightness(col1) - colorTools.brightness(col2) );
	},
	
	/**
	 * Get the color difference of two colors
	 * @param col1 string
	 * @param col2 string
	 * @return int
	 */
	colorDifference : function(col1,col2){
		col1	= colorTools.hexToRgb(col1);
		col2	= colorTools.hexToRgb(col2);
		
		return ( Math.max(col1['r'],col2['r']) - Math.min(col1['r'],col2['r']) ) + ( Math.max(col1['g'],col2['g']) - Math.min(col1['g'],col2['g']) ) + ( Math.max(col1['b'],col2['b']) - Math.min(col1['b'],col2['b']) );
	},
	
	/**
	 * Convert Decimal to Hexadecimal
	 * @param d decimal
	 * @return hexadecimal
	 */
	dechex : function(d) {
		return d.toString(16);
	},
	
	/**
	 * Convert Hexadecimal to Decimal
	 * @param h hexadecimal
	 * @return int
	 */
	hexdec : function(h) {
		return parseInt(h,16);	
	}
	
}
