Debian Codegraph @

ansi/color.go

raw
1 package ansi
2
3 import (
4 "image/color"
5
6 "github.com/lucasb-eyer/go-colorful"
7 )
8
9 // Color is a color that can be used in a terminal. ANSI (including
10 // ANSI256) and 24-bit "true colors" fall under this category.
11 type Color interface {
12 color.Color
13 }
14
15 // BasicColor is an ANSI 3-bit or 4-bit color with a value from 0 to 15.
16 type BasicColor uint8
17
18 var _ Color = BasicColor(0)
19
20 const (
21 // Black is the ANSI black color.
22 Black BasicColor = iota
23
24 // Red is the ANSI red color.
25 Red
26
27 // Green is the ANSI green color.
28 Green
29
30 // Yellow is the ANSI yellow color.
31 Yellow
32
33 // Blue is the ANSI blue color.
34 Blue
35
36 // Magenta is the ANSI magenta color.
37 Magenta
38
39 // Cyan is the ANSI cyan color.
40 Cyan
41
42 // White is the ANSI white color.
43 White
44
45 // BrightBlack is the ANSI bright black color.
46 BrightBlack
47
48 // BrightRed is the ANSI bright red color.
49 BrightRed
50
51 // BrightGreen is the ANSI bright green color.
52 BrightGreen
53
54 // BrightYellow is the ANSI bright yellow color.
55 BrightYellow
56
57 // BrightBlue is the ANSI bright blue color.
58 BrightBlue
59
60 // BrightMagenta is the ANSI bright magenta color.
61 BrightMagenta
62
63 // BrightCyan is the ANSI bright cyan color.
64 BrightCyan
65
66 // BrightWhite is the ANSI bright white color.
67 BrightWhite
68 )
69
70 // RGBA returns the red, green, blue and alpha components of the color. It
71 // satisfies the color.Color interface.
72 func (c BasicColor) RGBA() (uint32, uint32, uint32, uint32) {
73 ansi := uint32(c)
74 if ansi > 15 {
75 return 0, 0, 0, 0xffff
76 }
77
78 return ansiToRGB(byte(ansi)).RGBA()
79 }
80
81 // IndexedColor is an ANSI 256 (8-bit) color with a value from 0 to 255.
82 type IndexedColor uint8
83
84 var _ Color = IndexedColor(0)
85
86 // RGBA returns the red, green, blue and alpha components of the color. It
87 // satisfies the color.Color interface.
88 func (c IndexedColor) RGBA() (uint32, uint32, uint32, uint32) {
89 return ansiToRGB(byte(c)).RGBA()
90 }
91
92 // ExtendedColor is an ANSI 256 (8-bit) color with a value from 0 to 255.
93 //
94 // Deprecated: use [IndexedColor] instead.
95 type ExtendedColor = IndexedColor
96
97 // TrueColor is a 24-bit color that can be used in the terminal.
98 // This can be used to represent RGB colors.
99 //
100 // For example, the color red can be represented as:
101 //
102 // TrueColor(0xff0000)
103 //
104 // Deprecated: use [RGBColor] instead.
105 type TrueColor uint32
106
107 var _ Color = TrueColor(0)
108
109 // RGBA returns the red, green, blue and alpha components of the color. It
110 // satisfies the color.Color interface.
111 func (c TrueColor) RGBA() (uint32, uint32, uint32, uint32) {
112 r, g, b := hexToRGB(uint32(c))
113 return toRGBA(r, g, b)
114 }
115
116 // RGBColor is a 24-bit color that can be used in the terminal.
117 // This can be used to represent RGB colors.
118 type RGBColor struct {
119 R uint8
120 G uint8
121 B uint8
122 }
123
124 // RGBA returns the red, green, blue and alpha components of the color. It
125 // satisfies the color.Color interface.
126 func (c RGBColor) RGBA() (uint32, uint32, uint32, uint32) {
127 return toRGBA(uint32(c.R), uint32(c.G), uint32(c.B))
128 }
129
130 // ansiToRGB converts an ANSI color to a 24-bit RGB color.
131 //
132 // r, g, b := ansiToRGB(57)
133 func ansiToRGB(ansi byte) color.Color {
134 return ansiHex[ansi]
135 }
136
137 // hexToRGB converts a number in hexadecimal format to red, green, and blue
138 // values.
139 //
140 // r, g, b := hexToRGB(0x0000FF)
141 func hexToRGB(hex uint32) (uint32, uint32, uint32) {
142 return hex >> 16 & 0xff, hex >> 8 & 0xff, hex & 0xff
143 }
144
145 // toRGBA converts an RGB 8-bit color values to 32-bit color values suitable
146 // for color.Color.
147 //
148 // color.Color requires 16-bit color values, so we duplicate the 8-bit values
149 // to fill the 16-bit values.
150 //
151 // This always returns 0xffff (opaque) for the alpha channel.
152 func toRGBA(r, g, b uint32) (uint32, uint32, uint32, uint32) {
153 r |= r << 8
154 g |= g << 8
155 b |= b << 8
156 return r, g, b, 0xffff
157 }
158
159 //nolint:unused
160 func distSq(r1, g1, b1, r2, g2, b2 int) int {
161 return ((r1-r2)*(r1-r2) + (g1-g2)*(g1-g2) + (b1-b2)*(b1-b2))
162 }
163
164 func to6Cube[T int | float64](v T) int {
165 if v < 48 {
166 return 0
167 }
168 if v < 115 {
169 return 1
170 }
171 return int((v - 35) / 40)
172 }
173
174 // Convert256 converts a [color.Color], usually a 24-bit color, to xterm(1) 256
175 // color palette.
176 //
177 // xterm provides a 6x6x6 color cube (16 - 231) and 24 greys (232 - 255). We
178 // map our RGB color to the closest in the cube, also work out the closest
179 // grey, and use the nearest of the two based on the lightness of the color.
180 //
181 // Note that the xterm has much lower resolution for darker colors (they are
182 // not evenly spread out), so our 6 levels are not evenly spread: 0x0, 0x5f
183 // (95), 0x87 (135), 0xaf (175), 0xd7 (215) and 0xff (255). Greys are more
184 // evenly spread (8, 18, 28 ... 238).
185 func Convert256(c color.Color) IndexedColor {
186 // If the color is already an IndexedColor, return it.
187 if i, ok := c.(IndexedColor); ok {
188 return i
189 }
190
191 // Note: this is mostly ported from tmux/colour.c.
192 col, ok := colorful.MakeColor(c)
193 if !ok {
194 return IndexedColor(0)
195 }
196
197 r := col.R * 255
198 g := col.G * 255
199 b := col.B * 255
200
201 q2c := [6]int{0x00, 0x5f, 0x87, 0xaf, 0xd7, 0xff}
202
203 // Map RGB to 6x6x6 cube.
204 qr := to6Cube(r)
205 cr := q2c[qr]
206 qg := to6Cube(g)
207 cg := q2c[qg]
208 qb := to6Cube(b)
209 cb := q2c[qb]
210
211 // If we have hit the color exactly, return early.
212 ci := (36 * qr) + (6 * qg) + qb
213 if cr == int(r) && cg == int(g) && cb == int(b) {
214 return IndexedColor(16 + ci) //nolint:gosec
215 }
216
217 // Work out the closest grey (average of RGB).
218 greyAvg := int(r+g+b) / 3
219 var greyIdx int
220 if greyAvg > 238 {
221 greyIdx = 23
222 } else {
223 greyIdx = (greyAvg - 3) / 10
224 }
225 grey := 8 + (10 * greyIdx)
226
227 // Return the one which is nearer to the original input rgb value
228 // XXX: This is where it differs from tmux's implementation, we prefer the
229 // closer color to the original in terms of light distances rather than the
230 // cube distance.
231 c2 := colorful.Color{R: float64(cr) / 255.0, G: float64(cg) / 255.0, B: float64(cb) / 255.0}
232 g2 := colorful.Color{R: float64(grey) / 255.0, G: float64(grey) / 255.0, B: float64(grey) / 255.0}
233 colorDist := col.DistanceHSLuv(c2)
234 grayDist := col.DistanceHSLuv(g2)
235
236 if colorDist <= grayDist {
237 return IndexedColor(16 + ci) //nolint:gosec
238 }
239 return IndexedColor(232 + greyIdx) //nolint:gosec
240
241 // // Is grey or 6x6x6 color closest?
242 // d := distSq(cr, cg, cb, int(r), int(g), int(b))
243 // if distSq(grey, grey, grey, int(r), int(g), int(b)) < d {
244 // return IndexedColor(232 + greyIdx) //nolint:gosec
245 // }
246 // return IndexedColor(16 + ci) //nolint:gosec
247 }
248
249 // Convert16 converts a [color.Color] to a 16-color ANSI color. It will first
250 // try to find a match in the 256 xterm(1) color palette, and then map that to
251 // the 16-color ANSI palette.
252 func Convert16(c color.Color) BasicColor {
253 switch c := c.(type) {
254 case BasicColor:
255 // If the color is already a BasicColor, return it.
256 return c
257 case IndexedColor:
258 // If the color is already an IndexedColor, return the corresponding
259 // BasicColor.
260 return ansi256To16[c]
261 default:
262 c256 := Convert256(c)
263 return ansi256To16[c256]
264 }
265 }
266
267 // RGB values of ANSI colors (0-255).
268 var ansiHex = [...]color.RGBA{
269 0: {R: 0x00, G: 0x00, B: 0x00, A: 0xff}, // "#000000"
270 1: {R: 0x80, G: 0x00, B: 0x00, A: 0xff}, // "#800000"
271 2: {R: 0x00, G: 0x80, B: 0x00, A: 0xff}, // "#008000"
272 3: {R: 0x80, G: 0x80, B: 0x00, A: 0xff}, // "#808000"
273 4: {R: 0x00, G: 0x00, B: 0x80, A: 0xff}, // "#000080"
274 5: {R: 0x80, G: 0x00, B: 0x80, A: 0xff}, // "#800080"
275 6: {R: 0x00, G: 0x80, B: 0x80, A: 0xff}, // "#008080"
276 7: {R: 0xc0, G: 0xc0, B: 0xc0, A: 0xff}, // "#c0c0c0"
277 8: {R: 0x80, G: 0x80, B: 0x80, A: 0xff}, // "#808080"
278 9: {R: 0xff, G: 0x00, B: 0x00, A: 0xff}, // "#ff0000"
279 10: {R: 0x00, G: 0xff, B: 0x00, A: 0xff}, // "#00ff00"
280 11: {R: 0xff, G: 0xff, B: 0x00, A: 0xff}, // "#ffff00"
281 12: {R: 0x00, G: 0x00, B: 0xff, A: 0xff}, // "#0000ff"
282 13: {R: 0xff, G: 0x00, B: 0xff, A: 0xff}, // "#ff00ff"
283 14: {R: 0x00, G: 0xff, B: 0xff, A: 0xff}, // "#00ffff"
284 15: {R: 0xff, G: 0xff, B: 0xff, A: 0xff}, // "#ffffff"
285 16: {R: 0x00, G: 0x00, B: 0x00, A: 0xff}, // "#000000"
286 17: {R: 0x00, G: 0x00, B: 0x5f, A: 0xff}, // "#00005f"
287 18: {R: 0x00, G: 0x00, B: 0x87, A: 0xff}, // "#000087"
288 19: {R: 0x00, G: 0x00, B: 0xaf, A: 0xff}, // "#0000af"
289 20: {R: 0x00, G: 0x00, B: 0xd7, A: 0xff}, // "#0000d7"
290 21: {R: 0x00, G: 0x00, B: 0xff, A: 0xff}, // "#0000ff"
291 22: {R: 0x00, G: 0x5f, B: 0x00, A: 0xff}, // "#005f00"
292 23: {R: 0x00, G: 0x5f, B: 0x5f, A: 0xff}, // "#005f5f"
293 24: {R: 0x00, G: 0x5f, B: 0x87, A: 0xff}, // "#005f87"
294 25: {R: 0x00, G: 0x5f, B: 0xaf, A: 0xff}, // "#005faf"
295 26: {R: 0x00, G: 0x5f, B: 0xd7, A: 0xff}, // "#005fd7"
296 27: {R: 0x00, G: 0x5f, B: 0xff, A: 0xff}, // "#005fff"
297 28: {R: 0x00, G: 0x87, B: 0x00, A: 0xff}, // "#008700"
298 29: {R: 0x00, G: 0x87, B: 0x5f, A: 0xff}, // "#00875f"
299 30: {R: 0x00, G: 0x87, B: 0x87, A: 0xff}, // "#008787"
300 31: {R: 0x00, G: 0x87, B: 0xaf, A: 0xff}, // "#0087af"
301 32: {R: 0x00, G: 0x87, B: 0xd7, A: 0xff}, // "#0087d7"
302 33: {R: 0x00, G: 0x87, B: 0xff, A: 0xff}, // "#0087ff"
303 34: {R: 0x00, G: 0xaf, B: 0x00, A: 0xff}, // "#00af00"
304 35: {R: 0x00, G: 0xaf, B: 0x5f, A: 0xff}, // "#00af5f"
305 36: {R: 0x00, G: 0xaf, B: 0x87, A: 0xff}, // "#00af87"
306 37: {R: 0x00, G: 0xaf, B: 0xaf, A: 0xff}, // "#00afaf"
307 38: {R: 0x00, G: 0xaf, B: 0xd7, A: 0xff}, // "#00afd7"
308 39: {R: 0x00, G: 0xaf, B: 0xff, A: 0xff}, // "#00afff"
309 40: {R: 0x00, G: 0xd7, B: 0x00, A: 0xff}, // "#00d700"
310 41: {R: 0x00, G: 0xd7, B: 0x5f, A: 0xff}, // "#00d75f"
311 42: {R: 0x00, G: 0xd7, B: 0x87, A: 0xff}, // "#00d787"
312 43: {R: 0x00, G: 0xd7, B: 0xaf, A: 0xff}, // "#00d7af"
313 44: {R: 0x00, G: 0xd7, B: 0xd7, A: 0xff}, // "#00d7d7"
314 45: {R: 0x00, G: 0xd7, B: 0xff, A: 0xff}, // "#00d7ff"
315 46: {R: 0x00, G: 0xff, B: 0x00, A: 0xff}, // "#00ff00"
316 47: {R: 0x00, G: 0xff, B: 0x5f, A: 0xff}, // "#00ff5f"
317 48: {R: 0x00, G: 0xff, B: 0x87, A: 0xff}, // "#00ff87"
318 49: {R: 0x00, G: 0xff, B: 0xaf, A: 0xff}, // "#00ffaf"
319 50: {R: 0x00, G: 0xff, B: 0xd7, A: 0xff}, // "#00ffd7"
320 51: {R: 0x00, G: 0xff, B: 0xff, A: 0xff}, // "#00ffff"
321 52: {R: 0x5f, G: 0x00, B: 0x00, A: 0xff}, // "#5f0000"
322 53: {R: 0x5f, G: 0x00, B: 0x5f, A: 0xff}, // "#5f005f"
323 54: {R: 0x5f, G: 0x00, B: 0x87, A: 0xff}, // "#5f0087"
324 55: {R: 0x5f, G: 0x00, B: 0xaf, A: 0xff}, // "#5f00af"
325 56: {R: 0x5f, G: 0x00, B: 0xd7, A: 0xff}, // "#5f00d7"
326 57: {R: 0x5f, G: 0x00, B: 0xff, A: 0xff}, // "#5f00ff"
327 58: {R: 0x5f, G: 0x5f, B: 0x00, A: 0xff}, // "#5f5f00"
328 59: {R: 0x5f, G: 0x5f, B: 0x5f, A: 0xff}, // "#5f5f5f"
329 60: {R: 0x5f, G: 0x5f, B: 0x87, A: 0xff}, // "#5f5f87"
330 61: {R: 0x5f, G: 0x5f, B: 0xaf, A: 0xff}, // "#5f5faf"
331 62: {R: 0x5f, G: 0x5f, B: 0xd7, A: 0xff}, // "#5f5fd7"
332 63: {R: 0x5f, G: 0x5f, B: 0xff, A: 0xff}, // "#5f5fff"
333 64: {R: 0x5f, G: 0x87, B: 0x00, A: 0xff}, // "#5f8700"
334 65: {R: 0x5f, G: 0x87, B: 0x5f, A: 0xff}, // "#5f875f"
335 66: {R: 0x5f, G: 0x87, B: 0x87, A: 0xff}, // "#5f8787"
336 67: {R: 0x5f, G: 0x87, B: 0xaf, A: 0xff}, // "#5f87af"
337 68: {R: 0x5f, G: 0x87, B: 0xd7, A: 0xff}, // "#5f87d7"
338 69: {R: 0x5f, G: 0x87, B: 0xff, A: 0xff}, // "#5f87ff"
339 70: {R: 0x5f, G: 0xaf, B: 0x00, A: 0xff}, // "#5faf00"
340 71: {R: 0x5f, G: 0xaf, B: 0x5f, A: 0xff}, // "#5faf5f"
341 72: {R: 0x5f, G: 0xaf, B: 0x87, A: 0xff}, // "#5faf87"
342 73: {R: 0x5f, G: 0xaf, B: 0xaf, A: 0xff}, // "#5fafaf"
343 74: {R: 0x5f, G: 0xaf, B: 0xd7, A: 0xff}, // "#5fafd7"
344 75: {R: 0x5f, G: 0xaf, B: 0xff, A: 0xff}, // "#5fafff"
345 76: {R: 0x5f, G: 0xd7, B: 0x00, A: 0xff}, // "#5fd700"
346 77: {R: 0x5f, G: 0xd7, B: 0x5f, A: 0xff}, // "#5fd75f"
347 78: {R: 0x5f, G: 0xd7, B: 0x87, A: 0xff}, // "#5fd787"
348 79: {R: 0x5f, G: 0xd7, B: 0xaf, A: 0xff}, // "#5fd7af"
349 80: {R: 0x5f, G: 0xd7, B: 0xd7, A: 0xff}, // "#5fd7d7"
350 81: {R: 0x5f, G: 0xd7, B: 0xff, A: 0xff}, // "#5fd7ff"
351 82: {R: 0x5f, G: 0xff, B: 0x00, A: 0xff}, // "#5fff00"
352 83: {R: 0x5f, G: 0xff, B: 0x5f, A: 0xff}, // "#5fff5f"
353 84: {R: 0x5f, G: 0xff, B: 0x87, A: 0xff}, // "#5fff87"
354 85: {R: 0x5f, G: 0xff, B: 0xaf, A: 0xff}, // "#5fffaf"
355 86: {R: 0x5f, G: 0xff, B: 0xd7, A: 0xff}, // "#5fffd7"
356 87: {R: 0x5f, G: 0xff, B: 0xff, A: 0xff}, // "#5fffff"
357 88: {R: 0x87, G: 0x00, B: 0x00, A: 0xff}, // "#870000"
358 89: {R: 0x87, G: 0x00, B: 0x5f, A: 0xff}, // "#87005f"
359 90: {R: 0x87, G: 0x00, B: 0x87, A: 0xff}, // "#870087"
360 91: {R: 0x87, G: 0x00, B: 0xaf, A: 0xff}, // "#8700af"
361 92: {R: 0x87, G: 0x00, B: 0xd7, A: 0xff}, // "#8700d7"
362 93: {R: 0x87, G: 0x00, B: 0xff, A: 0xff}, // "#8700ff"
363 94: {R: 0x87, G: 0x5f, B: 0x00, A: 0xff}, // "#875f00"
364 95: {R: 0x87, G: 0x5f, B: 0x5f, A: 0xff}, // "#875f5f"
365 96: {R: 0x87, G: 0x5f, B: 0x87, A: 0xff}, // "#875f87"
366 97: {R: 0x87, G: 0x5f, B: 0xaf, A: 0xff}, // "#875faf"
367 98: {R: 0x87, G: 0x5f, B: 0xd7, A: 0xff}, // "#875fd7"
368 99: {R: 0x87, G: 0x5f, B: 0xff, A: 0xff}, // "#875fff"
369 100: {R: 0x87, G: 0x87, B: 0x00, A: 0xff}, // "#878700"
370 101: {R: 0x87, G: 0x87, B: 0x5f, A: 0xff}, // "#87875f"
371 102: {R: 0x87, G: 0x87, B: 0x87, A: 0xff}, // "#878787"
372 103: {R: 0x87, G: 0x87, B: 0xaf, A: 0xff}, // "#8787af"
373 104: {R: 0x87, G: 0x87, B: 0xd7, A: 0xff}, // "#8787d7"
374 105: {R: 0x87, G: 0x87, B: 0xff, A: 0xff}, // "#8787ff"
375 106: {R: 0x87, G: 0xaf, B: 0x00, A: 0xff}, // "#87af00"
376 107: {R: 0x87, G: 0xaf, B: 0x5f, A: 0xff}, // "#87af5f"
377 108: {R: 0x87, G: 0xaf, B: 0x87, A: 0xff}, // "#87af87"
378 109: {R: 0x87, G: 0xaf, B: 0xaf, A: 0xff}, // "#87afaf"
379 110: {R: 0x87, G: 0xaf, B: 0xd7, A: 0xff}, // "#87afd7"
380 111: {R: 0x87, G: 0xaf, B: 0xff, A: 0xff}, // "#87afff"
381 112: {R: 0x87, G: 0xd7, B: 0x00, A: 0xff}, // "#87d700"
382 113: {R: 0x87, G: 0xd7, B: 0x5f, A: 0xff}, // "#87d75f"
383 114: {R: 0x87, G: 0xd7, B: 0x87, A: 0xff}, // "#87d787"
384 115: {R: 0x87, G: 0xd7, B: 0xaf, A: 0xff}, // "#87d7af"
385 116: {R: 0x87, G: 0xd7, B: 0xd7, A: 0xff}, // "#87d7d7"
386 117: {R: 0x87, G: 0xd7, B: 0xff, A: 0xff}, // "#87d7ff"
387 118: {R: 0x87, G: 0xff, B: 0x00, A: 0xff}, // "#87ff00"
388 119: {R: 0x87, G: 0xff, B: 0x5f, A: 0xff}, // "#87ff5f"
389 120: {R: 0x87, G: 0xff, B: 0x87, A: 0xff}, // "#87ff87"
390 121: {R: 0x87, G: 0xff, B: 0xaf, A: 0xff}, // "#87ffaf"
391 122: {R: 0x87, G: 0xff, B: 0xd7, A: 0xff}, // "#87ffd7"
392 123: {R: 0x87, G: 0xff, B: 0xff, A: 0xff}, // "#87ffff"
393 124: {R: 0xaf, G: 0x00, B: 0x00, A: 0xff}, // "#af0000"
394 125: {R: 0xaf, G: 0x00, B: 0x5f, A: 0xff}, // "#af005f"
395 126: {R: 0xaf, G: 0x00, B: 0x87, A: 0xff}, // "#af0087"
396 127: {R: 0xaf, G: 0x00, B: 0xaf, A: 0xff}, // "#af00af"
397 128: {R: 0xaf, G: 0x00, B: 0xd7, A: 0xff}, // "#af00d7"
398 129: {R: 0xaf, G: 0x00, B: 0xff, A: 0xff}, // "#af00ff"
399 130: {R: 0xaf, G: 0x5f, B: 0x00, A: 0xff}, // "#af5f00"
400 131: {R: 0xaf, G: 0x5f, B: 0x5f, A: 0xff}, // "#af5f5f"
401 132: {R: 0xaf, G: 0x5f, B: 0x87, A: 0xff}, // "#af5f87"
402 133: {R: 0xaf, G: 0x5f, B: 0xaf, A: 0xff}, // "#af5faf"
403 134: {R: 0xaf, G: 0x5f, B: 0xd7, A: 0xff}, // "#af5fd7"
404 135: {R: 0xaf, G: 0x5f, B: 0xff, A: 0xff}, // "#af5fff"
405 136: {R: 0xaf, G: 0x87, B: 0x00, A: 0xff}, // "#af8700"
406 137: {R: 0xaf, G: 0x87, B: 0x5f, A: 0xff}, // "#af875f"
407 138: {R: 0xaf, G: 0x87, B: 0x87, A: 0xff}, // "#af8787"
408 139: {R: 0xaf, G: 0x87, B: 0xaf, A: 0xff}, // "#af87af"
409 140: {R: 0xaf, G: 0x87, B: 0xd7, A: 0xff}, // "#af87d7"
410 141: {R: 0xaf, G: 0x87, B: 0xff, A: 0xff}, // "#af87ff"
411 142: {R: 0xaf, G: 0xaf, B: 0x00, A: 0xff}, // "#afaf00"
412 143: {R: 0xaf, G: 0xaf, B: 0x5f, A: 0xff}, // "#afaf5f"
413 144: {R: 0xaf, G: 0xaf, B: 0x87, A: 0xff}, // "#afaf87"
414 145: {R: 0xaf, G: 0xaf, B: 0xaf, A: 0xff}, // "#afafaf"
415 146: {R: 0xaf, G: 0xaf, B: 0xd7, A: 0xff}, // "#afafd7"
416 147: {R: 0xaf, G: 0xaf, B: 0xff, A: 0xff}, // "#afafff"
417 148: {R: 0xaf, G: 0xd7, B: 0x00, A: 0xff}, // "#afd700"
418 149: {R: 0xaf, G: 0xd7, B: 0x5f, A: 0xff}, // "#afd75f"
419 150: {R: 0xaf, G: 0xd7, B: 0x87, A: 0xff}, // "#afd787"
420 151: {R: 0xaf, G: 0xd7, B: 0xaf, A: 0xff}, // "#afd7af"
421 152: {R: 0xaf, G: 0xd7, B: 0xd7, A: 0xff}, // "#afd7d7"
422 153: {R: 0xaf, G: 0xd7, B: 0xff, A: 0xff}, // "#afd7ff"
423 154: {R: 0xaf, G: 0xff, B: 0x00, A: 0xff}, // "#afff00"
424 155: {R: 0xaf, G: 0xff, B: 0x5f, A: 0xff}, // "#afff5f"
425 156: {R: 0xaf, G: 0xff, B: 0x87, A: 0xff}, // "#afff87"
426 157: {R: 0xaf, G: 0xff, B: 0xaf, A: 0xff}, // "#afffaf"
427 158: {R: 0xaf, G: 0xff, B: 0xd7, A: 0xff}, // "#afffd7"
428 159: {R: 0xaf, G: 0xff, B: 0xff, A: 0xff}, // "#afffff"
429 160: {R: 0xd7, G: 0x00, B: 0x00, A: 0xff}, // "#d70000"
430 161: {R: 0xd7, G: 0x00, B: 0x5f, A: 0xff}, // "#d7005f"
431 162: {R: 0xd7, G: 0x00, B: 0x87, A: 0xff}, // "#d70087"
432 163: {R: 0xd7, G: 0x00, B: 0xaf, A: 0xff}, // "#d700af"
433 164: {R: 0xd7, G: 0x00, B: 0xd7, A: 0xff}, // "#d700d7"
434 165: {R: 0xd7, G: 0x00, B: 0xff, A: 0xff}, // "#d700ff"
435 166: {R: 0xd7, G: 0x5f, B: 0x00, A: 0xff}, // "#d75f00"
436 167: {R: 0xd7, G: 0x5f, B: 0x5f, A: 0xff}, // "#d75f5f"
437 168: {R: 0xd7, G: 0x5f, B: 0x87, A: 0xff}, // "#d75f87"
438 169: {R: 0xd7, G: 0x5f, B: 0xaf, A: 0xff}, // "#d75faf"
439 170: {R: 0xd7, G: 0x5f, B: 0xd7, A: 0xff}, // "#d75fd7"
440 171: {R: 0xd7, G: 0x5f, B: 0xff, A: 0xff}, // "#d75fff"
441 172: {R: 0xd7, G: 0x87, B: 0x00, A: 0xff}, // "#d78700"
442 173: {R: 0xd7, G: 0x87, B: 0x5f, A: 0xff}, // "#d7875f"
443 174: {R: 0xd7, G: 0x87, B: 0x87, A: 0xff}, // "#d78787"
444 175: {R: 0xd7, G: 0x87, B: 0xaf, A: 0xff}, // "#d787af"
445 176: {R: 0xd7, G: 0x87, B: 0xd7, A: 0xff}, // "#d787d7"
446 177: {R: 0xd7, G: 0x87, B: 0xff, A: 0xff}, // "#d787ff"
447 178: {R: 0xd7, G: 0xaf, B: 0x00, A: 0xff}, // "#d7af00"
448 179: {R: 0xd7, G: 0xaf, B: 0x5f, A: 0xff}, // "#d7af5f"
449 180: {R: 0xd7, G: 0xaf, B: 0x87, A: 0xff}, // "#d7af87"
450 181: {R: 0xd7, G: 0xaf, B: 0xaf, A: 0xff}, // "#d7afaf"
451 182: {R: 0xd7, G: 0xaf, B: 0xd7, A: 0xff}, // "#d7afd7"
452 183: {R: 0xd7, G: 0xaf, B: 0xff, A: 0xff}, // "#d7afff"
453 184: {R: 0xd7, G: 0xd7, B: 0x00, A: 0xff}, // "#d7d700"
454 185: {R: 0xd7, G: 0xd7, B: 0x5f, A: 0xff}, // "#d7d75f"
455 186: {R: 0xd7, G: 0xd7, B: 0x87, A: 0xff}, // "#d7d787"
456 187: {R: 0xd7, G: 0xd7, B: 0xaf, A: 0xff}, // "#d7d7af"
457 188: {R: 0xd7, G: 0xd7, B: 0xd7, A: 0xff}, // "#d7d7d7"
458 189: {R: 0xd7, G: 0xd7, B: 0xff, A: 0xff}, // "#d7d7ff"
459 190: {R: 0xd7, G: 0xff, B: 0x00, A: 0xff}, // "#d7ff00"
460 191: {R: 0xd7, G: 0xff, B: 0x5f, A: 0xff}, // "#d7ff5f"
461 192: {R: 0xd7, G: 0xff, B: 0x87, A: 0xff}, // "#d7ff87"
462 193: {R: 0xd7, G: 0xff, B: 0xaf, A: 0xff}, // "#d7ffaf"
463 194: {R: 0xd7, G: 0xff, B: 0xd7, A: 0xff}, // "#d7ffd7"
464 195: {R: 0xd7, G: 0xff, B: 0xff, A: 0xff}, // "#d7ffff"
465 196: {R: 0xff, G: 0x00, B: 0x00, A: 0xff}, // "#ff0000"
466 197: {R: 0xff, G: 0x00, B: 0x5f, A: 0xff}, // "#ff005f"
467 198: {R: 0xff, G: 0x00, B: 0x87, A: 0xff}, // "#ff0087"
468 199: {R: 0xff, G: 0x00, B: 0xaf, A: 0xff}, // "#ff00af"
469 200: {R: 0xff, G: 0x00, B: 0xd7, A: 0xff}, // "#ff00d7"
470 201: {R: 0xff, G: 0x00, B: 0xff, A: 0xff}, // "#ff00ff"
471 202: {R: 0xff, G: 0x5f, B: 0x00, A: 0xff}, // "#ff5f00"
472 203: {R: 0xff, G: 0x5f, B: 0x5f, A: 0xff}, // "#ff5f5f"
473 204: {R: 0xff, G: 0x5f, B: 0x87, A: 0xff}, // "#ff5f87"
474 205: {R: 0xff, G: 0x5f, B: 0xaf, A: 0xff}, // "#ff5faf"
475 206: {R: 0xff, G: 0x5f, B: 0xd7, A: 0xff}, // "#ff5fd7"
476 207: {R: 0xff, G: 0x5f, B: 0xff, A: 0xff}, // "#ff5fff"
477 208: {R: 0xff, G: 0x87, B: 0x00, A: 0xff}, // "#ff8700"
478 209: {R: 0xff, G: 0x87, B: 0x5f, A: 0xff}, // "#ff875f"
479 210: {R: 0xff, G: 0x87, B: 0x87, A: 0xff}, // "#ff8787"
480 211: {R: 0xff, G: 0x87, B: 0xaf, A: 0xff}, // "#ff87af"
481 212: {R: 0xff, G: 0x87, B: 0xd7, A: 0xff}, // "#ff87d7"
482 213: {R: 0xff, G: 0x87, B: 0xff, A: 0xff}, // "#ff87ff"
483 214: {R: 0xff, G: 0xaf, B: 0x00, A: 0xff}, // "#ffaf00"
484 215: {R: 0xff, G: 0xaf, B: 0x5f, A: 0xff}, // "#ffaf5f"
485 216: {R: 0xff, G: 0xaf, B: 0x87, A: 0xff}, // "#ffaf87"
486 217: {R: 0xff, G: 0xaf, B: 0xaf, A: 0xff}, // "#ffafaf"
487 218: {R: 0xff, G: 0xaf, B: 0xd7, A: 0xff}, // "#ffafd7"
488 219: {R: 0xff, G: 0xaf, B: 0xff, A: 0xff}, // "#ffafff"
489 220: {R: 0xff, G: 0xd7, B: 0x00, A: 0xff}, // "#ffd700"
490 221: {R: 0xff, G: 0xd7, B: 0x5f, A: 0xff}, // "#ffd75f"
491 222: {R: 0xff, G: 0xd7, B: 0x87, A: 0xff}, // "#ffd787"
492 223: {R: 0xff, G: 0xd7, B: 0xaf, A: 0xff}, // "#ffd7af"
493 224: {R: 0xff, G: 0xd7, B: 0xd7, A: 0xff}, // "#ffd7d7"
494 225: {R: 0xff, G: 0xd7, B: 0xff, A: 0xff}, // "#ffd7ff"
495 226: {R: 0xff, G: 0xff, B: 0x00, A: 0xff}, // "#ffff00"
496 227: {R: 0xff, G: 0xff, B: 0x5f, A: 0xff}, // "#ffff5f"
497 228: {R: 0xff, G: 0xff, B: 0x87, A: 0xff}, // "#ffff87"
498 229: {R: 0xff, G: 0xff, B: 0xaf, A: 0xff}, // "#ffffaf"
499 230: {R: 0xff, G: 0xff, B: 0xd7, A: 0xff}, // "#ffffd7"
500 231: {R: 0xff, G: 0xff, B: 0xff, A: 0xff}, // "#ffffff"
501 232: {R: 0x08, G: 0x08, B: 0x08, A: 0xff}, // "#080808"
502 233: {R: 0x12, G: 0x12, B: 0x12, A: 0xff}, // "#121212"
503 234: {R: 0x1c, G: 0x1c, B: 0x1c, A: 0xff}, // "#1c1c1c"
504 235: {R: 0x26, G: 0x26, B: 0x26, A: 0xff}, // "#262626"
505 236: {R: 0x30, G: 0x30, B: 0x30, A: 0xff}, // "#303030"
506 237: {R: 0x3a, G: 0x3a, B: 0x3a, A: 0xff}, // "#3a3a3a"
507 238: {R: 0x44, G: 0x44, B: 0x44, A: 0xff}, // "#444444"
508 239: {R: 0x4e, G: 0x4e, B: 0x4e, A: 0xff}, // "#4e4e4e"
509 240: {R: 0x58, G: 0x58, B: 0x58, A: 0xff}, // "#585858"
510 241: {R: 0x62, G: 0x62, B: 0x62, A: 0xff}, // "#626262"
511 242: {R: 0x6c, G: 0x6c, B: 0x6c, A: 0xff}, // "#6c6c6c"
512 243: {R: 0x76, G: 0x76, B: 0x76, A: 0xff}, // "#767676"
513 244: {R: 0x80, G: 0x80, B: 0x80, A: 0xff}, // "#808080"
514 245: {R: 0x8a, G: 0x8a, B: 0x8a, A: 0xff}, // "#8a8a8a"
515 246: {R: 0x94, G: 0x94, B: 0x94, A: 0xff}, // "#949494"
516 247: {R: 0x9e, G: 0x9e, B: 0x9e, A: 0xff}, // "#9e9e9e"
517 248: {R: 0xa8, G: 0xa8, B: 0xa8, A: 0xff}, // "#a8a8a8"
518 249: {R: 0xb2, G: 0xb2, B: 0xb2, A: 0xff}, // "#b2b2b2"
519 250: {R: 0xbc, G: 0xbc, B: 0xbc, A: 0xff}, // "#bcbcbc"
520 251: {R: 0xc6, G: 0xc6, B: 0xc6, A: 0xff}, // "#c6c6c6"
521 252: {R: 0xd0, G: 0xd0, B: 0xd0, A: 0xff}, // "#d0d0d0"
522 253: {R: 0xda, G: 0xda, B: 0xda, A: 0xff}, // "#dadada"
523 254: {R: 0xe4, G: 0xe4, B: 0xe4, A: 0xff}, // "#e4e4e4"
524 255: {R: 0xee, G: 0xee, B: 0xee, A: 0xff}, // "#eeeeee"
525 }
526
527 var ansi256To16 = [...]BasicColor{
528 0: 0,
529 1: 1,
530 2: 2,
531 3: 3,
532 4: 4,
533 5: 5,
534 6: 6,
535 7: 7,
536 8: 8,
537 9: 9,
538 10: 10,
539 11: 11,
540 12: 12,
541 13: 13,
542 14: 14,
543 15: 15,
544 16: 0,
545 17: 4,
546 18: 4,
547 19: 4,
548 20: 12,
549 21: 12,
550 22: 2,
551 23: 6,
552 24: 4,
553 25: 4,
554 26: 12,
555 27: 12,
556 28: 2,
557 29: 2,
558 30: 6,
559 31: 4,
560 32: 12,
561 33: 12,
562 34: 2,
563 35: 2,
564 36: 2,
565 37: 6,
566 38: 12,
567 39: 12,
568 40: 10,
569 41: 10,
570 42: 10,
571 43: 10,
572 44: 14,
573 45: 12,
574 46: 10,
575 47: 10,
576 48: 10,
577 49: 10,
578 50: 10,
579 51: 14,
580 52: 1,
581 53: 5,
582 54: 4,
583 55: 4,
584 56: 12,
585 57: 12,
586 58: 3,
587 59: 8,
588 60: 4,
589 61: 4,
590 62: 12,
591 63: 12,
592 64: 2,
593 65: 2,
594 66: 6,
595 67: 4,
596 68: 12,
597 69: 12,
598 70: 2,
599 71: 2,
600 72: 2,
601 73: 6,
602 74: 12,
603 75: 12,
604 76: 10,
605 77: 10,
606 78: 10,
607 79: 10,
608 80: 14,
609 81: 12,
610 82: 10,
611 83: 10,
612 84: 10,
613 85: 10,
614 86: 10,
615 87: 14,
616 88: 1,
617 89: 1,
618 90: 5,
619 91: 4,
620 92: 12,
621 93: 12,
622 94: 1,
623 95: 1,
624 96: 5,
625 97: 4,
626 98: 12,
627 99: 12,
628 100: 3,
629 101: 3,
630 102: 8,
631 103: 4,
632 104: 12,
633 105: 12,
634 106: 2,
635 107: 2,
636 108: 2,
637 109: 6,
638 110: 12,
639 111: 12,
640 112: 10,
641 113: 10,
642 114: 10,
643 115: 10,
644 116: 14,
645 117: 12,
646 118: 10,
647 119: 10,
648 120: 10,
649 121: 10,
650 122: 10,
651 123: 14,
652 124: 1,
653 125: 1,
654 126: 1,
655 127: 5,
656 128: 12,
657 129: 12,
658 130: 1,
659 131: 1,
660 132: 1,
661 133: 5,
662 134: 12,
663 135: 12,
664 136: 1,
665 137: 1,
666 138: 1,
667 139: 5,
668 140: 12,
669 141: 12,
670 142: 3,
671 143: 3,
672 144: 3,
673 145: 7,
674 146: 12,
675 147: 12,
676 148: 10,
677 149: 10,
678 150: 10,
679 151: 10,
680 152: 14,
681 153: 12,
682 154: 10,
683 155: 10,
684 156: 10,
685 157: 10,
686 158: 10,
687 159: 14,
688 160: 9,
689 161: 9,
690 162: 9,
691 163: 9,
692 164: 13,
693 165: 12,
694 166: 9,
695 167: 9,
696 168: 9,
697 169: 9,
698 170: 13,
699 171: 12,
700 172: 9,
701 173: 9,
702 174: 9,
703 175: 9,
704 176: 13,
705 177: 12,
706 178: 9,
707 179: 9,
708 180: 9,
709 181: 9,
710 182: 13,
711 183: 12,
712 184: 11,
713 185: 11,
714 186: 11,
715 187: 11,
716 188: 7,
717 189: 12,
718 190: 10,
719 191: 10,
720 192: 10,
721 193: 10,
722 194: 10,
723 195: 14,
724 196: 9,
725 197: 9,
726 198: 9,
727 199: 9,
728 200: 9,
729 201: 13,
730 202: 9,
731 203: 9,
732 204: 9,
733 205: 9,
734 206: 9,
735 207: 13,
736 208: 9,
737 209: 9,
738 210: 9,
739 211: 9,
740 212: 9,
741 213: 13,
742 214: 9,
743 215: 9,
744 216: 9,
745 217: 9,
746 218: 9,
747 219: 13,
748 220: 9,
749 221: 9,
750 222: 9,
751 223: 9,
752 224: 9,
753 225: 13,
754 226: 11,
755 227: 11,
756 228: 11,
757 229: 11,
758 230: 11,
759 231: 15,
760 232: 0,
761 233: 0,
762 234: 0,
763 235: 0,
764 236: 0,
765 237: 0,
766 238: 8,
767 239: 8,
768 240: 8,
769 241: 8,
770 242: 8,
771 243: 8,
772 244: 7,
773 245: 7,
774 246: 7,
775 247: 7,
776 248: 7,
777 249: 7,
778 250: 15,
779 251: 15,
780 252: 15,
781 253: 15,
782 254: 15,
783 255: 15,
784 }