FF.Color = {};

FF.Color = function(red, green, blue) {
	this.red = red;
	this.green = green;
	this.blue = blue;
};
FF.Color.prototype.toString = function() {
	return 'rgb(' + parseInt(this.red) + ', ' + parseInt(this.green) + ', '
			+ parseInt(this.blue) + ')';
};

FF.Color.fromHSL = function(hue, sat, lum) {
	var c = parseInt(hue % (360 / 6) * 6);
	var sec = parseInt(hue / 360 * 6);
	
	switch (sec) {
		case 0: return new FF.Color(255, c, 0); break;
		case 1: return new FF.Color(255-c, 255, 0); break;
		case 2: return new FF.Color(0, 255, c); break;
		case 3: return new FF.Color(0, 255-c, 255); break;
		case 4: return new FF.Color(c, 0, 255); break;
		case 5: return new FF.Color(255, 0, 255-c); break;
		default:
			if (sec == 6 && c == 0) {
				return new FF.Color(255, 0, 0);
			}
	}
};
	// Color picker.
(function() {
	FF.Color.createHuePicker = function(width, height, res, func) {
		var el = document.createElement('canvas');
		el.width = width;
		el.height = height;
		
		var ctx = el.getContext('2d');
		for (var i=0; i<width; i+=res) {
			ctx.fillStyle = FF.Color.fromHSL(i/width * 360, 100, 100).toString();
			ctx.fillRect(i, 0, res, height);
		}
		
		var active = false;
		function callback(ev) {
			var c = parseInt((ev.clientX - FF.Dom.getX(el)) * 360 / width);
		//	if (c > 255) c = 255;
			
			func(c);
		}
		
		el.onmousedown = function(ev) {
			active = true;
			callback(ev);
		};
		el.onmouseup = function(ev) {
			active = false;
			callback(ev);
		};
		el.onmousemove = function(ev) {
			if (active) {
				callback(ev);
			}
		};
		
		return el;
	};
})();
