Convert RGB color to hex color - Node.js CSS

Node.js examples for CSS:Color

Description

Convert RGB color to hex color

Demo Code


function rgb2hex(rgb) {
    if (/^#[0-9A-F]{6}$/i.test(rgb)) return rgb;

    rgb = rgb.match(/gb\((\d+),\s*(\d+),\s*(\d+)\)$/);
    function hex(x) {
        return ("0" + parseInt(x).toString(16)).slice(-2);
    }/*w w w.  j a v a 2  s  .co m*/

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

Related Tutorials