// ===============================================================
// CBkort version 2.x, copyright Carl Bro GIS&IT, 2006
// ===============================================================
// $Archive: /Products/CBKort2/development/2.6/standard/wwwroot/js/standard/color.js $ 
// $Date: 7-10-10 11:44 $
// $Revision: 2 $ 
// $Author: Kpo $
// =============================================================== 
    
function Color (color) {
    this.hex = null;
    this.rgb = null;
    this.rgbArray = [];
    this.hexchars = "0123456789ABCDEF";
    this.set (color);
}
Color.prototype.set = function(color) {
    if(color.indexOf('rgb') == 0) {
        //CASE: rgb(255,0,0)
        var r = color.split(",");
        this.rgbArray [0] = parseInt(r[0].substr(1 + r[0].indexOf("(")));
        this.rgbArray [1] = parseInt(r[1]);
        this.rgbArray [2] = parseInt(r[2].substr(0, r[2].indexOf(")")));
        this.hex = '#'+this.numberToHex (this.rgbArray [0])+this.numberToHex (this.rgbArray [1])+this.numberToHex (this.rgbArray [2])
    } else if (color.indexOf(' ') == -1 && color.indexOf(',') == -1){
        // CASE: #FF0000
        if(color.indexOf('#')!=0) {
            color = '#'+color;
        }
        this.hex = color;
        color = color.replace (/#/,'');
        this.rgbArray[0] = (this.hexcharToDec(color.substr(0, 1)) * 16) + this.hexcharToDec(color.substr(1, 1));
        this.rgbArray[1] = (this.hexcharToDec(color.substr(2, 1)) * 16) + this.hexcharToDec(color.substr(3, 1));
        this.rgbArray[2] = (this.hexcharToDec(color.substr(4, 1)) * 16) + this.hexcharToDec(color.substr(5, 1));
    } else {
        //CASE: 255 0 0
        this.rgbArray = color.split (' ');
        this.rgbArray[0] = parseInt(this.rgbArray[0], 10);
        this.rgbArray[1] = parseInt(this.rgbArray[1], 10);
        this.rgbArray[2] = parseInt(this.rgbArray[2], 10);
        this.hex = '#'+this.numberToHex (this.rgbArray [0])+this.numberToHex (this.rgbArray [1])+this.numberToHex (this.rgbArray [2])
    }
    this.rgb = this.rgbArray [0] +' '+ this.rgbArray [1] +' '+ this.rgbArray [2];
}
Color.prototype.numberToHex = function(number) {
    number = parseInt(number || 0, 10);
    if (isNaN(number)) number = 0;
    number = Math.round(Math.min(Math.max(0, number), 255));
    return this.hexchars.charAt((number - number % 16) / 16) + this.hexchars.charAt(number % 16);
}
Color.prototype.hexcharToDec = function(hexchar) {
    return this.hexchars.indexOf(hexchar.toUpperCase())
}


