/* Javascript Document
 *
 * Created by fire.crow@firecrow.com 2009
 */

(function(){
    
    if(window.com == undefined) window['com'] = {}; 
    if(window.com.firecrow == undefined) window.com['firecrow'] = {}; 
    var path = window.com.firecrow;
    var alert = function(message){
        if(path.alert) path.alert(message);
        else window.alert(message);
    }
     
    function Color()
    {
        this.map = {r:0,g:1,b:2}; 
        this.values = { 
            r:0,
            g:0,
            b:0,
            get: function(){
                return [this.r, this.g, this.b]; 
            }
        }
        this.setRGB(arguments); 
    }
    Color.prototype = {
        toString: function(hex)
        { 
            if(hex){ 
                var str = '#'
                for(prop in this.map){ 
                        var hex = this.values[prop].toString(16); 
                        while(hex.length < 2) hex = '0'+hex; 
                        str += hex;
                    }
                return str;
            }else
                return 'rgb(' + this.values.r + ',' + this.values.g + ',' + this.values.b + ')'; 
        }, 
        setRGB: function()
        { 
            if(arguments.length < 1) 
                throw new Error('color argument required for setRGB function. '
                    + 'use int for r g and b (r,g,b) or ([r,g,b]) or  ("rgb(r,g,b)")'
                    + ' or ("#rrggbb") or ("rrggbb") ');

            // strip source array down to the 3 colors in the event
            // that it arrives as an array inside an array inside an array
            source = arguments;
            while((source.length == 1) && (typeof source != 'string'))
                source = source[0];

            if(typeof source == 'string') 
                this._setFromString(source);
            else if((source.length == 3)) 
                this._setFromArray(source)
        }, 
        setChannel: function(channel,value)
        {
             if(!{r:true,g:true,b:true}[channel]) 
                throw Error('Color.setChannel first argument '
                + 'must be string "r" or "g" or "b". recieved: "' + channel + '"');
                
             if(value < 0 || value > 255) 
                 throw new Error('Color.setChannel value must '
                + 'be between 0 and 255. recieved: "' + value + '"for channel "' + channel + '"' );
                
             //this[channel] = value;
             this.values[channel] = value;
        }, 
        _setFromArray: function(source)
        {
            for(prop in this.map)
                this.setChannel(prop,source[this.map[prop]]);
        }, 
        _setFromString: function(str)
        { 
            if(/^rgb\(\s?\d{1,3},\s?\d{1,3},\s?\d{1,3}\)$/.test(str))
                this._setFromRGBString(str);

            else if(/^#?[\da-f]{6}$/i.test(str)) 
                this._setFromHexString(str);

            else
                throw new Error('string formatted color argument "' + str +'" not recognized '
                + 'must be "rgb(,r,g,b)" or "#RRGGBB" or "RRGGBB"');
        }, 
        _setFromRGBString: function(str)
        {
            this._setFromArray(
                str.substring(
                    4,
                    str.length-1
                    ).split(','));
        }, 
        _setFromHexString: function(str)
        {
            if(str.substring(0,1) == '#') str = str.substring(1,str.length);
            for(prop in this.map)
            {
                var start = this.map[prop] * 2; 
                this.setChannel(prop, parseInt(
                    str.substring(start,start+2),
                    16)); 
            }
        } 
    }

    /**
     * @params
     *      colorFrom   - [r,g,b]
     *      colorTo     - [r,g,b]
     *      decimal     - number 0.0 to 1.0
     * @returns
     *      [r,b,g] the in between color
     */
    Color.getTransform = function(colorFrom,colorTo,percent) 
    {
        var transformed = [];
        for(var i = 0; i < 3; i ++)
            if(colorTo[i] != colorFrom[i])
                transformed[i] = parseInt(colorFrom[i]) + Math.round((colorTo[i] - colorFrom[i]) * (percent / 100));
            else
                transformed[i] = colorFrom[i];;
        
        return transformed;
    }

    /**
     * returns true if both arrays attibutes match
     */
    Color.compare = function(a,b)
    {
        return ((a[0] = b[0]) && (a[1] == b[1]) && (a[2] == b[2]));
    }

    path['Color'] = Color;
})();

