-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcolor.go
More file actions
164 lines (151 loc) · 6.51 KB
/
color.go
File metadata and controls
164 lines (151 loc) · 6.51 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package color
import (
"fmt"
"strconv"
)
// Color contains ANSI escape sequences for text styling and coloring.
const (
// Reset
Reset = "\x1b[0m"
// Standard Colors
Black = "\x1b[30m"
Red = "\x1b[31m"
Green = "\x1b[32m"
Yellow = "\x1b[33m"
Blue = "\x1b[34m"
Magenta = "\x1b[35m"
Cyan = "\x1b[36m"
White = "\x1b[37m"
// Bright Colors
BrightBlack = "\x1b[90m"
BrightRed = "\x1b[91m"
BrightGreen = "\x1b[92m"
BrightYellow = "\x1b[93m"
BrightBlue = "\x1b[94m"
BrightMagenta = "\x1b[95m"
BrightCyan = "\x1b[96m"
BrightWhite = "\x1b[97m"
// Background Colors
BgBlack = "\x1b[40m"
BgRed = "\x1b[41m"
BgGreen = "\x1b[42m"
BgYellow = "\x1b[43m"
BgBlue = "\x1b[44m"
BgMagenta = "\x1b[45m"
BgCyan = "\x1b[46m"
BgWhite = "\x1b[47m"
// Bright Background Colors
BgBrightBlack = "\x1b[100m"
BgBrightRed = "\x1b[101m"
BgBrightGreen = "\x1b[102m"
BgBrightYellow = "\x1b[103m"
BgBrightBlue = "\x1b[104m"
BgBrightMagenta = "\x1b[105m"
BgBrightCyan = "\x1b[106m"
BgBrightWhite = "\x1b[107m"
// Text Styles
Bold = "\x1b[1m"
Dim = "\x1b[2m"
Italic = "\x1b[3m"
Underline = "\x1b[4m"
Blink = "\x1b[5m"
Reverse = "\x1b[7m"
Hidden = "\x1b[8m"
Strikethrough = "\x1b[9m"
)
// Color256Table contains 256 precomputed ANSI 8-bit color codes.
var color256Table = [256]string{
"\x1b[38;5;0m", "\x1b[38;5;1m", "\x1b[38;5;2m", "\x1b[38;5;3m",
"\x1b[38;5;4m", "\x1b[38;5;5m", "\x1b[38;5;6m", "\x1b[38;5;7m",
"\x1b[38;5;8m", "\x1b[38;5;9m", "\x1b[38;5;10m", "\x1b[38;5;11m",
"\x1b[38;5;12m", "\x1b[38;5;13m", "\x1b[38;5;14m", "\x1b[38;5;15m",
"\x1b[38;5;16m", "\x1b[38;5;17m", "\x1b[38;5;18m", "\x1b[38;5;19m",
"\x1b[38;5;20m", "\x1b[38;5;21m", "\x1b[38;5;22m", "\x1b[38;5;23m",
"\x1b[38;5;24m", "\x1b[38;5;25m", "\x1b[38;5;26m", "\x1b[38;5;27m",
"\x1b[38;5;28m", "\x1b[38;5;29m", "\x1b[38;5;30m", "\x1b[38;5;31m",
"\x1b[38;5;32m", "\x1b[38;5;33m", "\x1b[38;5;34m", "\x1b[38;5;35m",
"\x1b[38;5;36m", "\x1b[38;5;37m", "\x1b[38;5;38m", "\x1b[38;5;39m",
"\x1b[38;5;40m", "\x1b[38;5;41m", "\x1b[38;5;42m", "\x1b[38;5;43m",
"\x1b[38;5;44m", "\x1b[38;5;45m", "\x1b[38;5;46m", "\x1b[38;5;47m",
"\x1b[38;5;48m", "\x1b[38;5;49m", "\x1b[38;5;50m", "\x1b[38;5;51m",
"\x1b[38;5;52m", "\x1b[38;5;53m", "\x1b[38;5;54m", "\x1b[38;5;55m",
"\x1b[38;5;56m", "\x1b[38;5;57m", "\x1b[38;5;58m", "\x1b[38;5;59m",
"\x1b[38;5;60m", "\x1b[38;5;61m", "\x1b[38;5;62m", "\x1b[38;5;63m",
"\x1b[38;5;64m", "\x1b[38;5;65m", "\x1b[38;5;66m", "\x1b[38;5;67m",
"\x1b[38;5;68m", "\x1b[38;5;69m", "\x1b[38;5;70m", "\x1b[38;5;71m",
"\x1b[38;5;72m", "\x1b[38;5;73m", "\x1b[38;5;74m", "\x1b[38;5;75m",
"\x1b[38;5;76m", "\x1b[38;5;77m", "\x1b[38;5;78m", "\x1b[38;5;79m",
"\x1b[38;5;80m", "\x1b[38;5;81m", "\x1b[38;5;82m", "\x1b[38;5;83m",
"\x1b[38;5;84m", "\x1b[38;5;85m", "\x1b[38;5;86m", "\x1b[38;5;87m",
"\x1b[38;5;88m", "\x1b[38;5;89m", "\x1b[38;5;90m", "\x1b[38;5;91m",
"\x1b[38;5;92m", "\x1b[38;5;93m", "\x1b[38;5;94m", "\x1b[38;5;95m",
"\x1b[38;5;96m", "\x1b[38;5;97m", "\x1b[38;5;98m", "\x1b[38;5;99m",
"\x1b[38;5;100m", "\x1b[38;5;101m", "\x1b[38;5;102m", "\x1b[38;5;103m",
"\x1b[38;5;104m", "\x1b[38;5;105m", "\x1b[38;5;106m", "\x1b[38;5;107m",
"\x1b[38;5;108m", "\x1b[38;5;109m", "\x1b[38;5;110m", "\x1b[38;5;111m",
"\x1b[38;5;112m", "\x1b[38;5;113m", "\x1b[38;5;114m", "\x1b[38;5;115m",
"\x1b[38;5;116m", "\x1b[38;5;117m", "\x1b[38;5;118m", "\x1b[38;5;119m",
"\x1b[38;5;120m", "\x1b[38;5;121m", "\x1b[38;5;122m", "\x1b[38;5;123m",
"\x1b[38;5;124m", "\x1b[38;5;125m", "\x1b[38;5;126m", "\x1b[38;5;127m",
"\x1b[38;5;128m", "\x1b[38;5;129m", "\x1b[38;5;130m", "\x1b[38;5;131m",
"\x1b[38;5;132m", "\x1b[38;5;133m", "\x1b[38;5;134m", "\x1b[38;5;135m",
"\x1b[38;5;136m", "\x1b[38;5;137m", "\x1b[38;5;138m", "\x1b[38;5;139m",
"\x1b[38;5;140m", "\x1b[38;5;141m", "\x1b[38;5;142m", "\x1b[38;5;143m",
"\x1b[38;5;144m", "\x1b[38;5;145m", "\x1b[38;5;146m", "\x1b[38;5;147m",
"\x1b[38;5;148m", "\x1b[38;5;149m", "\x1b[38;5;150m", "\x1b[38;5;151m",
"\x1b[38;5;152m", "\x1b[38;5;153m", "\x1b[38;5;154m", "\x1b[38;5;155m",
"\x1b[38;5;156m", "\x1b[38;5;157m", "\x1b[38;5;158m", "\x1b[38;5;159m",
"\x1b[38;5;160m", "\x1b[38;5;161m", "\x1b[38;5;162m", "\x1b[38;5;163m",
"\x1b[38;5;164m", "\x1b[38;5;165m", "\x1b[38;5;166m", "\x1b[38;5;167m",
"\x1b[38;5;168m", "\x1b[38;5;169m", "\x1b[38;5;170m", "\x1b[38;5;171m",
"\x1b[38;5;172m", "\x1b[38;5;173m", "\x1b[38;5;174m", "\x1b[38;5;175m",
"\x1b[38;5;176m", "\x1b[38;5;177m", "\x1b[38;5;178m", "\x1b[38;5;179m",
"\x1b[38;5;180m", "\x1b[38;5;181m", "\x1b[38;5;182m", "\x1b[38;5;183m",
"\x1b[38;5;184m", "\x1b[38;5;185m", "\x1b[38;5;186m", "\x1b[38;5;187m",
"\x1b[38;5;188m", "\x1b[38;5;189m", "\x1b[38;5;190m", "\x1b[38;5;191m",
"\x1b[38;5;192m", "\x1b[38;5;193m", "\x1b[38;5;194m", "\x1b[38;5;195m",
"\x1b[38;5;196m", "\x1b[38;5;197m", "\x1b[38;5;198m", "\x1b[38;5;199m",
"\x1b[38;5;200m", "\x1b[38;5;201m", "\x1b[38;5;202m", "\x1b[38;5;203m",
"\x1b[38;5;204m", "\x1b[38;5;205m", "\x1b[38;5;206m", "\x1b[38;5;207m",
"\x1b[38;5;208m", "\x1b[38;5;209m", "\x1b[38;5;210m", "\x1b[38;5;211m",
"\x1b[38;5;212m", "\x1b[38;5;213m", "\x1b[38;5;214m", "\x1b[38;5;215m",
"\x1b[38;5;216m", "\x1b[38;5;217m", "\x1b[38;5;218m", "\x1b[38;5;219m",
"\x1b[38;5;220m", "\x1b[38;5;221m", "\x1b[38;5;222m", "\x1b[38;5;223m",
"\x1b[38;5;224m", "\x1b[38;5;225m", "\x1b[38;5;226m", "\x1b[38;5;227m",
"\x1b[38;5;228m", "\x1b[38;5;229m", "\x1b[38;5;230m", "\x1b[38;5;231m",
"\x1b[38;5;232m", "\x1b[38;5;233m", "\x1b[38;5;234m", "\x1b[38;5;235m",
"\x1b[38;5;236m", "\x1b[38;5;237m", "\x1b[38;5;238m", "\x1b[38;5;239m",
"\x1b[38;5;240m", "\x1b[38;5;241m", "\x1b[38;5;242m", "\x1b[38;5;243m",
"\x1b[38;5;244m", "\x1b[38;5;245m", "\x1b[38;5;246m", "\x1b[38;5;247m",
"\x1b[38;5;248m", "\x1b[38;5;249m", "\x1b[38;5;250m", "\x1b[38;5;251m",
"\x1b[38;5;252m", "\x1b[38;5;253m", "\x1b[38;5;254m", "\x1b[38;5;255m",
}
func Color256(i uint8) string {
if i < 0 {
return color256Table[0]
}
if i > 255 {
return color256Table[255]
}
return color256Table[i]
}
func RGB(r, g, b uint8) string {
return fmt.Sprintf("\x1b[38;2;%d;%d;%dm", r, g, b)
}
// HEX takes a hex string like "FF00AA" or "#FF00AA"
// and returns the corresponding ANSI 24-bit color escape code.
func Hex(hex string) string {
if len(hex) == 0 {
return ""
}
if hex[0] == '#' {
hex = hex[1:]
}
if len(hex) != 6 {
return ""
}
r, _ := strconv.ParseUint(hex[0:2], 16, 8)
g, _ := strconv.ParseUint(hex[2:4], 16, 8)
b, _ := strconv.ParseUint(hex[4:6], 16, 8)
return fmt.Sprintf("\x1b[38;2;%d;%d;%dm", r, g, b)
}