Line data Source code
1 : import { hexToRgb } from '@comfyorg/shared-frontend-utils/colorUtil'
2 :
3 : export interface Rgb {
4 : r: number
5 : g: number
6 : b: number
7 : }
8 :
9 2355 : const BLACK: Rgb = { r: 0, g: 0, b: 0 }
10 :
11 : /** Parses a hex, `rgb()` or `rgba()` colour; anything else is black. */
12 0 : export function parseToRgb(color: string): Rgb {
13 0 : if (color.startsWith('#')) return hexToRgb(color)
14 0 : const channels = /^rgba?\(\s*(\d+)\s*,\s*(\d+)\s*,\s*(\d+)/.exec(color)
15 0 : if (!channels) return BLACK
16 0 : return {
17 : r: Number(channels[1]),
18 : g: Number(channels[2]),
19 : b: Number(channels[3])
20 : }
21 : }
|