// Add trim functions
if (!(String.prototype.lTrim)) {
    String.prototype.lTrim = function() {
        return this.replace(/^\s*/, '');
    }
}
if (!(String.prototype.rTrim)) {
    String.prototype.rTrim = function() {
        return this.replace(/\s*$/, '');
    }
}
if (!(String.prototype.trim)) {
    String.prototype.trim = function() {
        return this.lTrim().rTrim();
    }
}

 //Function to get hex format a rgb colour
function rgb2hex(rgb) { //generates the hex-digits for a colour.
    function hex(x) {
        hexDigits = new Array("0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "A", "B", "C", "D", "E", "F");
        return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
    }

    return "#" + hex(rgb[0]) + hex(rgb[1]) + hex(rgb[2]);
}

// for full compatibiltiy, this must match hifi/lib/Scalar::charToStr method (sorry)
if (!(String.prototype.fromChar)) {
    String.prototype.fromChar = function() {
        var val = this.valueOf();
        switch (val) {
            case "`": return "grave";
            case "~": return "tilde";
            case "1": return "one";
            case "!": return "bang";
            case "2": return "two";
            case "@": return "strudel"; // or "at", "snail", "monkey tail", "arrobase", "asperand", etc..
            case "3": return "three";
            case "#": return "hash";
            case "4": return "four";
            case "$": return "dollar";
            case "5": return "five";
            case "%": return "percent";
            case "6": return "six";
            case "^": return "caret";
            case "7": return "seven";
            case "&": return "ampersand";
            case "8": return "eight";
            case "*": return "asterisk";
            case "9": return "nine";
            case "(": return "left-paren";
            case "0": return "zero";
            case ")": return "right-paren";
            case "-": return "hyphen";
            case "_": return "underscore";
            case "=": return "equals";
            case "+": return "plus";
            case "&larr;": return "delete";
            case "&#8633;": return "tab";
            case "[": return "left-square-bracket";
            case "]": return "right-square-bracket";
            case "{": return "left-squiggly-bracket";
            case "}": return "right-squiggly-bracket";
            case "\\": return "back-slash";
            case "|": return "pipe";
            case ";": return "semi-colon";
            case ":": return "colon";
            case "&#8682;": return "caps-lock";
            case "'": return "quote";
            case '"': return "double-quote";
            case "&#x23ce;": return "enter";
            case '&#8679;': return "shift";
            case ",":return "comma";
            case "<": return "left-angle-backet";
            case ".": return "period";
            case ">": return "right-angle-bracket";
            case "/": return "forward-slash";
            case "?": return "question-mark";
            case " ": return "space";
            default: {
                var charCode = this.charCodeAt(0);
                // return regular ascii characters as is
                if ((charCode > 47 && charCode < 91) || (charCode > 96 && charCode < 123)) {
                    return val;
                }
                return false;
            }
        }
    }
}

