UNPKG

lib-colors

Version:

Simple node.js library for work with colors

18 lines (12 loc) 403 B
import type { IRGB } from '../rgb.interface'; export function rgbFromHex(hex: string): IRGB { hex = hex.replace(/^#/, ''); const r = parseInt(hex.substring(0, 2), 16); const g = parseInt(hex.substring(2, 4), 16); const b = parseInt(hex.substring(4, 6), 16); let a; if (hex.length === 8) { a = parseInt(hex.substring(6, 8), 16) / 255; } return { r, g, b, a }; }