(data: string)
| 68 | type Rgb = { r: number; g: number; b: number } |
| 69 | |
| 70 | function parseOscRgb(data: string): Rgb | undefined { |
| 71 | // rgb:RRRR/GGGG/BBBB — each component is 1–4 hex digits. |
| 72 | // Some terminals append an alpha component (rgba:…/…/…/…); ignore it. |
| 73 | const rgbMatch = |
| 74 | /^rgba?:([0-9a-f]{1,4})\/([0-9a-f]{1,4})\/([0-9a-f]{1,4})/i.exec(data) |
| 75 | if (rgbMatch) { |
| 76 | return { |
| 77 | r: hexComponent(rgbMatch[1]!), |
| 78 | g: hexComponent(rgbMatch[2]!), |
| 79 | b: hexComponent(rgbMatch[3]!), |
| 80 | } |
| 81 | } |
| 82 | // #RRGGBB or #RRRRGGGGBBBB — split into three equal hex runs. |
| 83 | const hashMatch = /^#([0-9a-f]+)$/i.exec(data) |
| 84 | if (hashMatch && hashMatch[1]!.length % 3 === 0) { |
| 85 | const hex = hashMatch[1]! |
| 86 | const n = hex.length / 3 |
| 87 | return { |
| 88 | r: hexComponent(hex.slice(0, n)), |
| 89 | g: hexComponent(hex.slice(n, 2 * n)), |
| 90 | b: hexComponent(hex.slice(2 * n)), |
| 91 | } |
| 92 | } |
| 93 | return undefined |
| 94 | } |
| 95 | |
| 96 | /** Normalize a 1–4 digit hex component to [0, 1]. */ |
| 97 | function hexComponent(hex: string): number { |
no test coverage detected