-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcolor.go
176 lines (149 loc) · 3.46 KB
/
color.go
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
165
166
167
168
169
170
171
172
173
174
175
176
package termemu
import (
"fmt"
"strconv"
)
type Color uint32
type ColorMode uint32
type ColorType uint32
const (
mask256color Color = 0xff
maskRGBcolor Color = 0xffffff // 24-bit
ColDefault Color = mask256color + 1 // outside the range
ColBlack Color = 0
ColRed Color = 1
ColGreen Color = 2
ColYellow Color = 3
ColBlue Color = 4
ColMagenta Color = 5
ColCyan Color = 6
ColWhite Color = 7
ModeBold ColorMode = 1 << (0 + 24)
ModeDim ColorMode = 1 << (1 + 24)
ModeItalic ColorMode = 1 << (2 + 24)
ModeUnderline ColorMode = 1 << (3 + 24)
ModeBlink ColorMode = 1 << (4 + 24) // or
ModeReverse ColorMode = 1 << (5 + 24)
ModeInvisible ColorMode = 1 << (6 + 24)
colorTypeShift = 7 + 24
colorTypeMask = 1 << colorTypeShift
ColorType256 ColorType = 0
ColorTypeRGB ColorType = 1
)
var Colors8 = []Color{
ColBlack,
ColRed,
ColGreen,
ColYellow,
ColBlue,
ColMagenta,
ColCyan,
ColWhite,
}
var ColorModes = []ColorMode{
ModeBold,
ModeDim,
ModeItalic,
ModeUnderline,
ModeBlink,
ModeReverse,
ModeInvisible,
}
var colorMasks = []Color{
mask256color,
maskRGBcolor,
}
func (c Color) SetColor(color Color) Color {
c &= ^mask256color
color &= mask256color
return (c | color) & ^Color(colorTypeMask)
}
func (c Color) SetColorRGB(r int, g int, b int) Color {
r &= 0xff
g &= 0xff
b &= 0xff
c &= ^maskRGBcolor
return (c | Color(r<<16|g<<8|b)) & Color(colorTypeMask)
}
func (c Color) Color() int {
t := c.ColorType()
mask := colorMasks[int(t)]
return int(c & mask)
}
func (c Color) ColorRGB() (int, int, int) {
i := c.Color()
return i >> 16, (i >> 8) & 0xff, i & 0xff
}
func (c Color) ColorType() ColorType {
return ColorType(c&colorTypeMask) >> colorTypeShift
}
func (c Color) SetMode(mode ColorMode) Color {
return c | Color(mode)
}
func (c Color) ResetMode(mode ColorMode) Color {
return c & ^Color(mode)
}
func (c Color) ResetModes() {
for _, m := range ColorModes {
c.ResetMode(m)
}
}
func (c Color) TestMode(mode ColorMode) bool {
return c&Color(mode) != 0
}
func (c Color) Modes() []ColorMode {
count := 0
for _, m := range ColorModes {
if c.TestMode(m) {
count += 1
}
}
modes := make([]ColorMode, count)
i := 0
for _, m := range ColorModes {
if c.TestMode(m) {
modes[i] = m
i++
}
}
return modes
}
const ESC byte = 27
// only set color, no escape, no modes
func ansiEscapeColor(c Color, ctype byte) []byte {
var seq []byte
color := c.Color()
colorStr := strconv.Itoa(color)
t := c.ColorType()
// fmt.Printf("%v: %x %d %#v\n", t, c, color, colorStr)
switch t {
case ColorType256:
if color < 8 {
seq = append(seq, ESC, '[', ctype, byte(colorStr[0]), 'm')
} else if c == ColDefault {
// ANSIEscape below already resets colors, so we don't have to do anything here
} else {
seq = append(seq, ESC, '[', ctype, '8', ';', '5', ';')
seq = append(seq, []byte(colorStr+"m")...)
}
case ColorTypeRGB:
seq = append(seq, ESC, '[', ctype, '8', ';', '2', ';')
r, g, b := c.ColorRGB()
seq = append(seq, []byte(fmt.Sprint(r, ';', g, ';', b, 'm'))...)
}
return seq
}
// ANSI Escape sequence to set this color
func ANSIEscape(fg Color, bg Color) []byte {
seq := []byte{ESC, '[', '0', 'm'}
for i, m := range ColorModes {
if fg.TestMode(m) {
seq = append(seq, ESC, '[')
seq = append(seq, []byte(strconv.Itoa(1+i))...)
seq = append(seq, 'm')
}
}
seq = append(seq, ansiEscapeColor(fg, '3')...)
seq = append(seq, ansiEscapeColor(bg, '4')...)
return seq
}