MCPcopy Create free account
hub / github.com/modem-dev/hunk / parseOsc11BackgroundColor

Function parseOsc11BackgroundColor

src/core/themeDetection.ts:44–65  ·  view source on GitHub ↗
(sequence: string)

Source from the content-addressed store, hash-verified

42
43/** Parse common OSC 11 background-color responses into RGB. */
44export 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. */
68export function themeModeForBackgroundColor({ red, green, blue }: RgbColor): TerminalThemeMode {

Callers 3

onDataFunction · 0.85

Calls 1

parseHexChannelFunction · 0.85

Tested by

no test coverage detected