(spec: string)
| 316 | * to 8-bit). Returns null on parse failure. |
| 317 | */ |
| 318 | export function parseOscColor(spec: string): Color | null { |
| 319 | const hex = spec.match(/^#([0-9a-f]{2})([0-9a-f]{2})([0-9a-f]{2})$/i) |
| 320 | if (hex) { |
| 321 | return { |
| 322 | type: 'rgb', |
| 323 | r: parseInt(hex[1]!, 16), |
| 324 | g: parseInt(hex[2]!, 16), |
| 325 | b: parseInt(hex[3]!, 16), |
| 326 | } |
| 327 | } |
| 328 | const rgb = spec.match( |
| 329 | /^rgb:([0-9a-f]{1,4})\/([0-9a-f]{1,4})\/([0-9a-f]{1,4})$/i, |
| 330 | ) |
| 331 | if (rgb) { |
| 332 | // XParseColor: N hex digits → value / (16^N - 1), scale to 0-255 |
| 333 | const scale = (s: string) => |
| 334 | Math.round((parseInt(s, 16) / (16 ** s.length - 1)) * 255) |
| 335 | return { |
| 336 | type: 'rgb', |
| 337 | r: scale(rgb[1]!), |
| 338 | g: scale(rgb[2]!), |
| 339 | b: scale(rgb[3]!), |
| 340 | } |
| 341 | } |
| 342 | return null |
| 343 | } |
| 344 | |
| 345 | /** |
| 346 | * Parse OSC 21337 payload: `key=value;key=value;...` with `\;` and `\\` |
no test coverage detected