color.ts 598 B

12345678910111213141516171819
  1. export namespace Color {
  2. export function isValidHex(hex?: string): hex is string {
  3. if (!hex) return false
  4. return /^#[0-9a-fA-F]{6}$/.test(hex)
  5. }
  6. export function hexToRgb(hex: string): { r: number; g: number; b: number } {
  7. const r = parseInt(hex.slice(1, 3), 16)
  8. const g = parseInt(hex.slice(3, 5), 16)
  9. const b = parseInt(hex.slice(5, 7), 16)
  10. return { r, g, b }
  11. }
  12. export function hexToAnsiBold(hex?: string): string | undefined {
  13. if (!isValidHex(hex)) return undefined
  14. const { r, g, b } = hexToRgb(hex)
  15. return `\x1b[38;2;${r};${g};${b}m\x1b[1m`
  16. }
  17. }