UNPKG

@colormate/core

Version:

A modular and efficient color management library for JavaScript and TypeScript applications.

26 lines (25 loc) 794 B
// HexToRgb.ts import { ColorValidator } from '@validators/ColorValidator'; import { ColorConverter } from '@helpers/ColorConverter'; export class HexToRgb extends ColorConverter { static convertHexToRgb(hex) { return new HexToRgb().convert(hex); } convert(hex) { if (!ColorValidator.isValidHex(hex)) { throw new Error('Invalid HEX format'); } const cachedResult = this.getCachedResult(hex); if (cachedResult) return cachedResult; hex = hex.replace(/^#/, ''); const bigint = parseInt(hex, 16); const result = { r: (bigint >> 16) & 255, g: (bigint >> 8) & 255, b: bigint & 255, }; this.cacheResult(hex, result); return result; } }