(hex: string)
| 5 | |
| 6 | /** Parse a hex color string (with or without `#`) into RGB components. */ |
| 7 | export function hexToRgb(hex: string): { r: number; g: number; b: number } { |
| 8 | const cleaned = hex.replace(/^#/, '') |
| 9 | if (cleaned.length !== 6 && cleaned.length !== 3) { |
| 10 | return { r: 0, g: 0, b: 0 } |
| 11 | } |
| 12 | const full = |
| 13 | cleaned.length === 3 |
| 14 | ? cleaned[0] + cleaned[0] + cleaned[1] + cleaned[1] + cleaned[2] + cleaned[2] |
| 15 | : cleaned |
| 16 | const num = Number.parseInt(full, 16) |
| 17 | return { |
| 18 | r: (num >> 16) & 0xff, |
| 19 | g: (num >> 8) & 0xff, |
| 20 | b: num & 0xff, |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | /** Convert RGB components (0-255 each) to a 6-digit `#rrggbb` hex string. */ |
| 25 | export function rgbToHex(r: number, g: number, b: number): string { |
no test coverage detected