(color: string | undefined)
| 66 | * Returns `undefined` if `color` is undefined/empty. |
| 67 | */ |
| 68 | export function inkColorToCSS(color: string | undefined): string | undefined { |
| 69 | if (!color) return undefined |
| 70 | |
| 71 | // Pass through hex colors |
| 72 | if (color.startsWith('#')) return color |
| 73 | |
| 74 | // Normalise `rgb(r,g,b)` → `rgb(r, g, b)` (browsers accept both, but clean) |
| 75 | if (color.startsWith('rgb(')) { |
| 76 | return color.replace(/\s+/g, '') |
| 77 | } |
| 78 | |
| 79 | // ansi256(N) |
| 80 | const ansi256Match = color.match(/^ansi256\((\d+)\)$/) |
| 81 | if (ansi256Match) { |
| 82 | return ansi256ToHex(parseInt(ansi256Match[1]!, 10)) |
| 83 | } |
| 84 | |
| 85 | // ansi:name |
| 86 | if (color.startsWith('ansi:')) { |
| 87 | const name = color.slice(5) |
| 88 | return ANSI_COLORS[name] ?? color |
| 89 | } |
| 90 | |
| 91 | // Unknown format — return as-is (browser may understand it) |
| 92 | return color |
| 93 | } |
no test coverage detected