Convert HEX to RGB to Hex with Javascript

Hex to RGB

Function:

function hexToRgb(hex)
    {
        var shorthandRegex = /^#?([a-f\d])([a-f\d])([a-f\d])$/i;
        hex = hex.replace(shorthandRegex, function(m, r, g, b) {
            return r + r + g + g + b + b;
        });

        var result = /^#?([a-f\d]{2})([a-f\d]{2})([a-f\d]{2})$/i.exec(hex);
        return result ? {
            r: parseInt(result[1], 16),
            g: parseInt(result[2], 16),
            b: parseInt(result[3], 16)
        } : null;
    }

Use:

// Convert hex white (#FFFFFF) to rgb (255,255,255).
var hex_color = '#FFFFFF';
var rgb_color = 'rgb(' + hexToRgb( hex_color ).r + ',' + hexToRgb( hex_color ).g + ',' + hexToRgb( hex_color ).b + ')';

RGB to HEX

function rgbToHex(r, g, b) {
    return "#" + ((1 << 24) + (r << 16) + (g << 8) + b).toString(16).slice(1);
}