parseHex
The parseHex
function parses a HEX color string to its RGB components. This function supports both 3-digit and 6-digit HEX formats.
Syntax
parseHex(color: string): { r: number; g: number; b: number; }
Parameters
color
(string): The HEX color string to parse.
Returns
- object: An object containing RGB values.
Throws
- Error: Throws an error if the HEX color format is invalid.
Example
import { parseHex } from 'colore-js';
const rgb = parseHex('#ff0000');
console.log(rgb);
// Output: { r: 255, g: 0, b: 0 }
const rgbShort = parseHex('#f00');
console.log(rgbShort);
// Output: { r: 255, g: 0, b: 0 }
Usage
The parseHex
function is used to convert a HEX color string to its RGB components. This can be useful for color manipulation and analysis in various design and art applications.