/* COLOR LIBRARY */

color={};

color.cords=function(W) {

	var W2=W/2, rad=(hsv.H/360)*(Math.PI*2), hyp=(hsv.S+(100-hsv.V))/100*(W2/2);

	$S('mCur').left=Math.round(Math.abs(Math.round(Math.sin(rad)*hyp)+W2+3))+'px';
	$S('mCur').top=Math.round(Math.abs(Math.round(Math.cos(rad)*hyp)-W2-21))+'px';

};

color.HEX=function(o) { o=Math.round(Math.min(Math.max(0,o),255));

    return("0123456789ABCDEF".charAt((o-o%16)/16)+"0123456789ABCDEF".charAt(o%16));

};

color.RGB_HEX=function(o) { var fu=color.HEX; return('#'+fu(o.R)+fu(o.G)+fu(o.B)); };

color.HSV_RGB=function(o) {
    
    var R, G, A, B, C, S=o.S/100, V=o.V/100, H=o.H/360;

    if(S>0) { if(H>=1) H=0;

        H=6*H; F=H-Math.floor(H);
        A=Math.round(255*V*(1-S));
        B=Math.round(255*V*(1-(S*F)));
        C=Math.round(255*V*(1-(S*(1-F))));
        V=Math.round(255*V); 

        switch(Math.floor(H)) {

            case 0: R=V; G=C; B=A; break;
            case 1: R=B; G=V; B=A; break;
            case 2: R=A; G=V; B=C; break;
            case 3: R=A; G=B; B=V; break;
            case 4: R=C; G=A; B=V; break;
            case 5: R=V; G=A; B=B; break;

        }

        return({'R':R?R:0, 'G':G?G:0, 'B':B?B:0, 'A':1});

    }
    else return({'R':(V=Math.round(V*255)), 'G':V, 'B':V, 'A':1});

};

color.HSV_HEX=function(o) { return(color.RGB_HEX(color.HSV_RGB(o))); };

// converts rgb(r, g, b) looking values that come from css styles
// http://haacked.com/archive/2009/12/29/convert-rgb-to-hex.aspx
color.colorToHex = function (color) {
    if (color.substr(0, 1) === '#') {
        return color;
    }
    var digits = /(.*?)rgb\((\d+), (\d+), (\d+)\)/.exec(color);
    
    var red = parseInt(digits[2]).toString(16);
    var green = parseInt(digits[3]).toString(16);
    var blue = parseInt(digits[4]).toString(16);
    
    //var rgb = blue | (green << 8) | (red << 16);
    //r = digits[1] + '#' + rgb.toString(16);
    //value = rgb.toString(16);
    
    if(red.length == 1) red = '0' + red;
    if(green.length == 1) green = '0' + green;
    if(blue.length == 1) blue = '0' + blue;
    
    return '#' + red + green + blue;
};

color.RGB_HSV = function(rgb)
{
	// http://mjijackson.com/2008/02/rgb-to-hsl-and-rgb-to-hsv-color-model-conversion-algorithms-in-javascript
	/**
	 * Converts an RGB color value to HSV. Conversion formula
	 * adapted from http://en.wikipedia.org/wiki/HSV_color_space.
	 * Assumes r, g, and b are contained in the set [0, 255] and
	 * returns h, s, and v in the set [0, 1].
	 *
	 * @param   Number  r       The red color value
	 * @param   Number  g       The green color value
	 * @param   Number  b       The blue color value
	 * @return  Array           The HSV representation
	 */
	function rgbToHsv(r, g, b){
	    r = r/255, g = g/255, b = b/255;
	    var max = Math.max(r, g, b), min = Math.min(r, g, b);
	    var h, s, v = max;
	
	    var d = max - min;
	    s = max == 0 ? 0 : d / max;
	
	    if(max == min){
	        h = 0; // achromatic
	    }else{
	        switch(max){
	            case r: h = (g - b) / d + (g < b ? 6 : 0); break;
	            case g: h = (b - r) / d + 2; break;
	            case b: h = (r - g) / d + 4; break;
	        }
	        h /= 6;
	    }
	
	    return [h, s, v];
	}
	
	hsv = rgbToHsv(rgb.R, rgb.G, rgb.B);
	
	v = [];
	v.H = Math.round(hsv[0] * 360);
	v.S = Math.round(hsv[1] * 100);
	v.V = Math.round(hsv[2] * 100);
	
	return v;
}

// http://www.javascripter.net/faq/hextorgb.htm
color.HEX_RGB = function (h)
{
	function cutHex(h) {return (h.charAt(0)=="#") ? h.substring(1,7):h}
	
	v = [];
	v.R = parseInt((cutHex(h)).substring(0,2),16);
	v.G = parseInt((cutHex(h)).substring(2,4),16);
	v.B = parseInt((cutHex(h)).substring(4,6),16);
	
	return v;
}

