File: palette.go

package info (click to toggle)
golang-github-charmbracelet-x 0.0~git20251028.0cf22f8%2Bds-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,940 kB
  • sloc: sh: 124; makefile: 5
file content (34 lines) | stat: -rw-r--r-- 967 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package ansi

import (
	"fmt"
	"image/color"
)

// SetPalette sets the palette color for the given index. The index is a 16
// color index between 0 and 15. The color is a 24-bit RGB color.
//
//	OSC P n rrggbb BEL
//
// Where n is the color index in hex (0-f), and rrggbb is the color in
// hexadecimal format (e.g., ff0000 for red).
//
// This sequence is specific to the Linux Console and may not work in other
// terminal emulators.
//
// See https://man7.org/linux/man-pages/man4/console_codes.4.html
func SetPalette(i int, c color.Color) string {
	if c == nil || i < 0 || i > 15 {
		return ""
	}
	r, g, b, _ := c.RGBA()
	return fmt.Sprintf("\x1b]P%x%02x%02x%02x\x07", i, r>>8, g>>8, b>>8)
}

// ResetPalette resets the color palette to the default values.
//
// This sequence is specific to the Linux Console and may not work in other
// terminal emulators.
//
// See https://man7.org/linux/man-pages/man4/console_codes.4.html
const ResetPalette = "\x1b]R\x07"