nblackburn
5/29/2019 - 8:40 AM

Hex To RGB

module.exports = hex => {
    if (hex.charAt(0) === '#') {
        hex = hex.substring(1);
    }

    if (hex.length !== 3 && hex.length !== 6) {
        return false;
    }

    if (hex.length === 3) {
        hex = hex[0] + hex[0] + hex[1] + hex[1] + hex[2] + hex[2];
    }

    let red = parseInt(hex.substring(0, 2), 16);
    let blue = parseInt(hex.substring(4, 6), 16);
    let green = parseInt(hex.substring(2, 4), 16);

    return { red, green, blue };
};