
/* **** start define Color **** */
colorapp.Color = function() {
	this.red;
	this.green;
	this.blue;

	this.init.apply(this,arguments);
};
	colorapp.Color.prototype.init = function() {
		this.setColor.apply(this,arguments);
	};
	colorapp.Color.prototype.setColor = function() {
		if ( 3 == arguments.length ) {
			this.red   = parseInt( arguments[0], 10 );
			this.green = parseInt( arguments[1], 10 );
			this.blue  = parseInt( arguments[2], 10 );
			if ( isNaN(this.red) || isNaN(this.green) || isNaN(this.blue) ) {
				this.red   = colorapp.hexToDecimal( arguments[0] );
				this.green = colorapp.hexToDecimal( arguments[1] );
				this.blue  = colorapp.hexToDecimal( arguments[2] );
			}
			else {
				this.red   = this.getValidColorInteger( this.red   );
				this.green = this.getValidColorInteger( this.green );
				this.blue  = this.getValidColorInteger( this.blue  );
			}
		}
		else {
			var str = "" + arguments[0];
			if ( 6 == str.length || 7 == str.length ) {
				if ( 7 == str.length ) {
					str = str.substr(1);
				}
				this.red   = colorapp.hexToDecimal( str.substr(0,2) );
				this.green = colorapp.hexToDecimal( str.substr(2,2) );
				this.blue  = colorapp.hexToDecimal( str.substr(4,2) );
			}
			else if ( 3 == str.length || 4 == str.length ) {
				function dupHexValue(v) {
					return v + '' + v;
				}
				if ( 4 == str.length ) {
					str = str.substr(1);
				}
				this.red   = colorapp.hexToDecimal( dupHexValue( str.substr(0,1) ) );
				this.green = colorapp.hexToDecimal( dupHexValue( str.substr(1,1) ) );
				this.blue  = colorapp.hexToDecimal( dupHexValue( str.substr(2,1) ) );
			}
			else {
				alert( 'Invalid Color setting: ' + arguments[0] );
			}
		}
	};
	colorapp.Color.prototype.getValidColorInteger = function(cint) {
		return ( cint > 255 ? 255 : ( cint < 0 ? 0 : cint ) );
	};

	colorapp.Color.prototype.getRed = function() { return this.red; };
	colorapp.Color.prototype.getGreen = function() { return this.green; };
	colorapp.Color.prototype.getBlue = function() { return this.blue; };

	colorapp.Color.prototype.getMean = function() { // average
		return ( this.red + this.green + this.blue ) / 3;
	};
	colorapp.Color.prototype.getMedian = function() { // the middle value
		var ary = [this.red,this.green,this.blue];
		return ary[1];
	};

	colorapp.Color.prototype.sortValue = function(func) {
		return this[func]();
	};

	colorapp.Color.prototype.toHexColor = function(r,g,b) {
		if ( r == undefined || g == undefined || b == undefined ) {
			r = this.red;
			g = this.green;
			b = this.blue;
		}
		try {
			return '#'
				+ colorapp.padString( colorapp.decimalToHex( r ), 2, '0' ) + ''
				+ colorapp.padString( colorapp.decimalToHex( g ), 2, '0' ) + ''
				+ colorapp.padString( colorapp.decimalToHex( b ), 2, '0' ) + ''
			;
		}
		catch(e) {
			return null;
		}
	};

	/* from user comments - Manithu on 23-Mar-2007 05:23 @ http://us2.php.net/manual/en/function.hexdec.php#74092 */
	colorapp.Color.prototype.getContrastColor = function() {
		if ( colorapp.hexToDecimal( this.toHexColor().substr(1) ) > 8388607.5 ) { // 0xffffff/2 ) {
			return '#000';
		}
		else {
			return '#fff';
		}
	}

	/* from user comments - Manithu on 23-Mar-2007 05:23 @ http://us2.php.net/manual/en/function.hexdec.php#74092 */
	colorapp.Color.prototype.getNegativeColor = function() {
		var r = 255 - colorapp.decimalToHex( this.red   );
		var g = 255 - colorapp.decimalToHex( this.green );
		var b = 255 - colorapp.decimalToHex( this.blue  );
		return this.toHexColor(r,g,b);
	}

/* **** end define Color **** */

