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