/**
* RGB class for handling color calculation
* @param input A hex color string or an array of 3 decimal numbers
*/
var RGB = function(input) {
	this.color = new Array(3);
	if (typeof(input) == 'string') {
		this.color = input.match(/[0-9a-f]{2}/gi);
		for (var i=0; i<3; i++) this.color[i] = parseInt('0x' + this.color[i], 16);
	} else {
		for (var i=0; i<3; i++) {
			this.color[i] = (Math.round(input[i]) > 255)? 255 : Math.round(input[i]);
			this.color[i] = (this.color[i] < 0)? 0 : this.color[i];
		}
	}
	
	/**
	* add color to current RGB object, only perform addition with hex code input
	* @param rgb color hex string or array with decimal number e.g) '#abc123' , [12, 34, 56]
	*/
	this.sum = function(rgb) {
		if (typeof(rgb) == 'string') {
			var rgb = rgb.match(/[0-9a-f]{2}/gi);
			for (var i=0; i<3; i++) {
				var result = this.color[i] + parseInt('0x' + rgb[i], 16);
				this.color[i] = (result > 255)? 255 : result;
				this.color[i] = (this.color[i] < 0)? 0 : this.color[i];
			}
		} else {
			for (var i=0; i<3; i++) {
				var result = this.color[i] + rgb[i];
				this.color[i] = (result > 255)? 255 : result;
				this.color[i] = (this.color[i] < 0)? 0 : this.color[i];
			}
		}
		return this;
	};
	
	this.getDifference = function(rgb) {
		var result = [];
		if (typeof(rgb) == 'string') {
			var rgb = rgb.match(/[0-9a-f]{2}/gi);
			for (var i=0; i<3; i++) result[i] = parseInt('0x' + rgb[i], 16) - this.color[i];
		} else {
			var tmp;
			for (var i=0; i<3; i++) {
				tmp = (rgb[i] > 255)? 255 : rgb[i];
				tmp = (tmp < 0)? 0 : tmp;
				result[i] = tmp - this.color[i];
			}
		}
		return result;
	};
		
	this.toString = function() {
		var hex = "#";
		for (var i=0; i<3; i++) {
			this.color[i] = (this.color[i] > 255)? 255 : this.color[i];
			this.color[i] = (this.color[i] < 0)? 0 : this.color[i];
			hex += (Math.round(this.color[i]).toString(16).length < 2)? "0" + Math.round(this.color[i]).toString(16) : Math.round(this.color[i]).toString(16);
		}
		return hex;
	};
	
}




