(sequence: string)
| 42 | |
| 43 | /** Parse common OSC 11 background-color responses into RGB. */ |
| 44 | export function parseOsc11BackgroundColor(sequence: string): RgbColor | null { |
| 45 | const rgbMatch = |
| 46 | /\x1b\]11;rgb:([0-9a-f]{2,4})\/([0-9a-f]{2,4})\/([0-9a-f]{2,4})(?:\x07|\x1b\\)/i.exec(sequence); |
| 47 | if (rgbMatch) { |
| 48 | const red = parseHexChannel(rgbMatch[1]!); |
| 49 | const green = parseHexChannel(rgbMatch[2]!); |
| 50 | const blue = parseHexChannel(rgbMatch[3]!); |
| 51 | return red === null || green === null || blue === null ? null : { red, green, blue }; |
| 52 | } |
| 53 | |
| 54 | const hexMatch = /\x1b\]11;#([0-9a-f]{6})(?:\x07|\x1b\\)/i.exec(sequence); |
| 55 | if (!hexMatch) { |
| 56 | return null; |
| 57 | } |
| 58 | |
| 59 | const [, hex] = hexMatch; |
| 60 | return { |
| 61 | red: Number.parseInt(hex!.slice(0, 2), 16), |
| 62 | green: Number.parseInt(hex!.slice(2, 4), 16), |
| 63 | blue: Number.parseInt(hex!.slice(4, 6), 16), |
| 64 | }; |
| 65 | } |
| 66 | |
| 67 | /** Classify a background color using relative luminance. */ |
| 68 | export function themeModeForBackgroundColor({ red, green, blue }: RgbColor): TerminalThemeMode { |
no test coverage detected