UNPKG

@teaui/core

Version:

A high-level terminal UI library for Node

35 lines 1.17 kB
import { colors } from '@teaui/term'; export function colorToHex(color) { if (Array.isArray(color)) { return colors.RGBtoHex(color); } else if (typeof color === 'string' && color.startsWith('#')) { return color; } else if (typeof color === 'object' && 'sgr' in color) { return colors.indexToHex(+color.sgr); } else if (typeof color === 'object' && 'grayscale' in color) { return colors.RGBtoHex(color.grayscale, color.grayscale, color.grayscale); } else { const index = colors.nameToIndex(color); return index === -1 ? '#ffffff' : colors.indexToHex(index); } } export function colorToSGR(color, fgbg) { if (typeof color === 'object' && 'sgr' in color) { return `${color.sgr} ${fgbg}`; } else if (Array.isArray(color)) { return `${colorToHex(color)} ${fgbg}`; } else if (typeof color === 'object' && 'grayscale' in color) { const gray = 232 + Math.round((color.grayscale / 255) * 23); return `${colorToHex(color)}(${gray}) ${fgbg}`; } else { return `${color} ${fgbg}`; } } //# sourceMappingURL=Color.js.map