| 1 |
package ansi |
| 2 |
|
| 3 |
import ( |
| 4 |
"fmt" |
| 5 |
"image/color" |
| 6 |
|
| 7 |
"github.com/lucasb-eyer/go-colorful" |
| 8 |
) |
| 9 |
|
| 10 |
// HexColor is a [color.Color] that can be formatted as a hex string. |
| 11 |
type HexColor string |
| 12 |
|
| 13 |
// RGBA returns the RGBA values of the color. |
| 14 |
func (h HexColor) RGBA() (r, g, b, a uint32) { |
| 15 |
hex := h.color() |
| 16 |
if hex == nil { |
| 17 |
return 0, 0, 0, 0 |
| 18 |
} |
| 19 |
return hex.RGBA() |
| 20 |
} |
| 21 |
|
| 22 |
// Hex returns the hex representation of the color. If the color is invalid, it |
| 23 |
// returns an empty string. |
| 24 |
func (h HexColor) Hex() string { |
| 25 |
hex := h.color() |
| 26 |
if hex == nil { |
| 27 |
return "" |
| 28 |
} |
| 29 |
return hex.Hex() |
| 30 |
} |
| 31 |
|
| 32 |
// String returns the color as a hex string. If the color is nil, an empty |
| 33 |
// string is returned. |
| 34 |
func (h HexColor) String() string { |
| 35 |
return h.Hex() |
| 36 |
} |
| 37 |
|
| 38 |
// color returns the underlying color of the HexColor. |
| 39 |
func (h HexColor) color() *colorful.Color { |
| 40 |
hex, err := colorful.Hex(string(h)) |
| 41 |
if err != nil { |
| 42 |
return nil |
| 43 |
} |
| 44 |
return &hex |
| 45 |
} |
| 46 |
|
| 47 |
// XRGBColor is a [color.Color] that can be formatted as an XParseColor |
| 48 |
// rgb: string. |
| 49 |
// |
| 50 |
// See: https://linux.die.net/man/3/xparsecolor |
| 51 |
type XRGBColor struct { |
| 52 |
color.Color |
| 53 |
} |
| 54 |
|
| 55 |
// RGBA returns the RGBA values of the color. |
| 56 |
func (x XRGBColor) RGBA() (r, g, b, a uint32) { |
| 57 |
if x.Color == nil { |
| 58 |
return 0, 0, 0, 0 |
| 59 |
} |
| 60 |
return x.Color.RGBA() |
| 61 |
} |
| 62 |
|
| 63 |
// String returns the color as an XParseColor rgb: string. If the color is nil, |
| 64 |
// an empty string is returned. |
| 65 |
func (x XRGBColor) String() string { |
| 66 |
if x.Color == nil { |
| 67 |
return "" |
| 68 |
} |
| 69 |
r, g, b, _ := x.Color.RGBA() |
| 70 |
// Get the lower 8 bits |
| 71 |
return fmt.Sprintf("rgb:%04x/%04x/%04x", r, g, b) |
| 72 |
} |
| 73 |
|
| 74 |
// XRGBAColor is a [color.Color] that can be formatted as an XParseColor |
| 75 |
// rgba: string. |
| 76 |
// |
| 77 |
// See: https://linux.die.net/man/3/xparsecolor |
| 78 |
type XRGBAColor struct { |
| 79 |
color.Color |
| 80 |
} |
| 81 |
|
| 82 |
// RGBA returns the RGBA values of the color. |
| 83 |
func (x XRGBAColor) RGBA() (r, g, b, a uint32) { |
| 84 |
if x.Color == nil { |
| 85 |
return 0, 0, 0, 0 |
| 86 |
} |
| 87 |
return x.Color.RGBA() |
| 88 |
} |
| 89 |
|
| 90 |
// String returns the color as an XParseColor rgba: string. If the color is nil, |
| 91 |
// an empty string is returned. |
| 92 |
func (x XRGBAColor) String() string { |
| 93 |
if x.Color == nil { |
| 94 |
return "" |
| 95 |
} |
| 96 |
r, g, b, a := x.RGBA() |
| 97 |
// Get the lower 8 bits |
| 98 |
return fmt.Sprintf("rgba:%04x/%04x/%04x/%04x", r, g, b, a) |
| 99 |
} |
| 100 |
|
| 101 |
// SetForegroundColor returns a sequence that sets the default terminal |
| 102 |
// foreground color. |
| 103 |
// |
| 104 |
// OSC 10 ; color ST |
| 105 |
// OSC 10 ; color BEL |
| 106 |
// |
| 107 |
// Where color is the encoded color number. Most terminals support hex, |
| 108 |
// XParseColor rgb: and rgba: strings. You could use [HexColor], [XRGBColor], |
| 109 |
// or [XRGBAColor] to format the color. |
| 110 |
// |
| 111 |
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands |
| 112 |
func SetForegroundColor(s string) string { |
| 113 |
return "\x1b]10;" + s + "\x07" |
| 114 |
} |
| 115 |
|
| 116 |
// RequestForegroundColor is a sequence that requests the current default |
| 117 |
// terminal foreground color. |
| 118 |
// |
| 119 |
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands |
| 120 |
const RequestForegroundColor = "\x1b]10;?\x07" |
| 121 |
|
| 122 |
// ResetForegroundColor is a sequence that resets the default terminal |
| 123 |
// foreground color. |
| 124 |
// |
| 125 |
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands |
| 126 |
const ResetForegroundColor = "\x1b]110\x07" |
| 127 |
|
| 128 |
// SetBackgroundColor returns a sequence that sets the default terminal |
| 129 |
// background color. |
| 130 |
// |
| 131 |
// OSC 11 ; color ST |
| 132 |
// OSC 11 ; color BEL |
| 133 |
// |
| 134 |
// Where color is the encoded color number. Most terminals support hex, |
| 135 |
// XParseColor rgb: and rgba: strings. You could use [HexColor], [XRGBColor], |
| 136 |
// or [XRGBAColor] to format the color. |
| 137 |
// |
| 138 |
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands |
| 139 |
func SetBackgroundColor(s string) string { |
| 140 |
return "\x1b]11;" + s + "\x07" |
| 141 |
} |
| 142 |
|
| 143 |
// RequestBackgroundColor is a sequence that requests the current default |
| 144 |
// terminal background color. |
| 145 |
// |
| 146 |
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands |
| 147 |
const RequestBackgroundColor = "\x1b]11;?\x07" |
| 148 |
|
| 149 |
// ResetBackgroundColor is a sequence that resets the default terminal |
| 150 |
// background color. |
| 151 |
// |
| 152 |
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands |
| 153 |
const ResetBackgroundColor = "\x1b]111\x07" |
| 154 |
|
| 155 |
// SetCursorColor returns a sequence that sets the terminal cursor color. |
| 156 |
// |
| 157 |
// OSC 12 ; color ST |
| 158 |
// OSC 12 ; color BEL |
| 159 |
// |
| 160 |
// Where color is the encoded color number. Most terminals support hex, |
| 161 |
// XParseColor rgb: and rgba: strings. You could use [HexColor], [XRGBColor], |
| 162 |
// or [XRGBAColor] to format the color. |
| 163 |
// |
| 164 |
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands |
| 165 |
func SetCursorColor(s string) string { |
| 166 |
return "\x1b]12;" + s + "\x07" |
| 167 |
} |
| 168 |
|
| 169 |
// RequestCursorColor is a sequence that requests the current terminal cursor |
| 170 |
// color. |
| 171 |
// |
| 172 |
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands |
| 173 |
const RequestCursorColor = "\x1b]12;?\x07" |
| 174 |
|
| 175 |
// ResetCursorColor is a sequence that resets the terminal cursor color. |
| 176 |
// |
| 177 |
// See: https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Operating-System-Commands |
| 178 |
const ResetCursorColor = "\x1b]112\x07" |