UNPKG

pretty-easy-hex-to-rgb

Version:

Converts a hex color value to it's coresponding rgb value and returns it in an array like format of red, green, blue color values

33 lines (27 loc) 973 B
/** * @description * Makes an rgb color value of a HEX color value * and returns it in an Array like format * * @export * @param {string} hex * @returns {number[]} */ export function rgb(hex: string): number[] | Error { // Placeholders let red : number; let green : number; let blue : number; // Make it a valid hex color value (3 -> 6) if (hex.length === 3) hex = `${hex[0]}${hex[0]}${hex[1]}${hex[1]}${hex[2]}${hex[2]}`; // Make a rgb color value array red = parseInt(hex.substring(0, 2), 16); green = parseInt(hex.substring(2, 4), 16); blue = parseInt(hex.substring(4, 6), 16); // Not valid hex color passed if(isNaN(red) || isNaN(green) || isNaN(blue)) return new Error('Invalid HEX color code value supplied'); // Return a RGB color representation of a HEX color // in the array of [red, green, blue] color values // of number types return [ red, green, blue ]; }