Nodejs String to Color Convert hexRgb(retarr)

Here you can find the source of hexRgb(retarr)

Method Source Code

/**//  w  w w  . j  a  v a 2s.c  o m
 * @overview Color calculator for freshman-zju-qsc
 * @author Senorsen <sen@senorsen.com>
 * @copyright Qiu Shi Chao
 */

String.prototype.hexRgb = function(retarr) {
    var that = this,
        hex, 
        newhex = '';
    try {
       hex = this.split('#')[1].toLocaleLowerCase();
    } catch (e) {
        return this;
    }
    var regexps = [/^[0-9a-f]{3}$/, /^[0-9a-f]{6}$/];
    if (regexps[1].test(hex)) {
        newhex = hex;
    } else if (regexps[0].test(hex)) {
        newhex = hex.charAt(0) + hex.charAt(0) + hex.charAt(1) + hex.charAt(1) + hex.charAt(2) + hex.charAt(2);
    } else {
        // ??return??
        return this;
    }
    var hr, hg, hb, r, g, b;
    hr = newhex.charAt(0) + newhex.charAt(1);
    hg = newhex.charAt(2) + newhex.charAt(3);
    hb = newhex.charAt(4) + newhex.charAt(5);
    r = parseInt('0x' + hr);
    g = parseInt('0x' + hg);
    b = parseInt('0x' + hb);
    if (retarr) {
        return [r, g, b];
    }
    return 'rgb(' + r + ', ' + g + ', ' + b + ')';
};

Related

  1. hexRgbFade(a)
    String.prototype.hexRgbFade = function(a) {
        var rgb = this.hexRgb(true);
        if (!$.isArray(rgb)) {
            rgb = this.parseRgb();
        a = parseFloat(a);
        var r = parseInt(255 - (255 - rgb[0]) * a),
            g = parseInt(255 - (255 - rgb[1]) * a),
            b = parseInt(255 - (255 - rgb[2]) * a);
    ...
    
  2. hexRgba(a)
    String.prototype.hexRgba = function(a) {
        var rgb = this.hexRgb(true);
        if (!$.isArray(rgb)) {
            rgb = this.parseRgb();
        var r = rgb[0], g = rgb[1], b = rgb[2];
        return 'rgba(' + r + ', ' + g + ', ' + b + ', ' + a + ')';
    };
    
  3. hexToRGB( output )
    String.prototype.hexToRGB = function( output ) {
        if ( output == undefined ) {
            output = 'css';
        var rgb = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec( this );
        if ( output.toLowerCase() == 'css' ) {
            return rgb ? 'rgb( ' + parseInt( rgb[1], 16 ) + ', ' + parseInt( rgb[2], 16 ) + ', ' + parseInt( rgb[3], 16 ) + ' )' : null;
        else {
    ...