| 1 |
package ansi |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"image/color" |
| 6 |
) |
| 7 |
|
| 8 |
// SetPalette sets the palette color for the given index. The index is a 16 |
| 9 |
// color index between 0 and 15. The color is a 24-bit RGB color. |
| 10 |
// |
| 11 |
// OSC P n rrggbb BEL |
| 12 |
// |
| 13 |
// Where n is the color index in hex (0-f), and rrggbb is the color in |
| 14 |
// hexadecimal format (e.g., ff0000 for red). |
| 15 |
// |
| 16 |
// This sequence is specific to the Linux Console and may not work in other |
| 17 |
// terminal emulators. |
| 18 |
// |
| 19 |
// See https://man7.org/linux/man-pages/man4/console_codes.4.html |
| 20 |
func SetPalette(i int, c color.Color) string { |
| 21 |
if c == nil || i < 0 || i > 15 { |
| 22 |
return "" |
| 23 |
} |
| 24 |
r, g, b, _ := c.RGBA() |
| 25 |
return fmt.Sprintf("\x1b]P%x%02x%02x%02x\x07", i, r>>8, g>>8, b>>8) |
| 26 |
} |
| 27 |
|
| 28 |
// ResetPalette resets the color palette to the default values. |
| 29 |
// |
| 30 |
// This sequence is specific to the Linux Console and may not work in other |
| 31 |
// terminal emulators. |
| 32 |
// |
| 33 |
// See https://man7.org/linux/man-pages/man4/console_codes.4.html |
| 34 |
const ResetPalette = "\x1b]R\x07" |