MCPcopy Create free account
hub / github.com/Noumena-Network/code / parseOscColor

Function parseOscColor

src/ink/termio/osc.ts:318–343  ·  view source on GitHub ↗
(spec: string)

Source from the content-addressed store, hash-verified

316 * to 8-bit). Returns null on parse failure.
317 */
318export 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 `\\`

Callers 1

parseTabStatusFunction · 0.85

Calls 1

scaleFunction · 0.85

Tested by

no test coverage detected