-
Notifications
You must be signed in to change notification settings - Fork 38
/
color.go
180 lines (170 loc) · 3.65 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
177
178
179
180
package terminal
import (
"image/color"
"log"
"strconv"
"strings"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/theme"
)
var (
basicColors = []color.Color{
color.Black,
&color.RGBA{170, 0, 0, 255},
&color.RGBA{0, 170, 0, 255},
&color.RGBA{170, 170, 0, 255},
&color.RGBA{0, 0, 170, 255},
&color.RGBA{170, 0, 170, 255},
&color.RGBA{0, 255, 255, 255},
&color.RGBA{170, 170, 170, 255},
}
brightColors = []color.Color{
&color.RGBA{85, 85, 85, 255},
&color.RGBA{255, 85, 85, 255},
&color.RGBA{85, 255, 85, 255},
&color.RGBA{255, 255, 85, 255},
&color.RGBA{85, 85, 255, 255},
&color.RGBA{255, 85, 255, 255},
&color.RGBA{85, 255, 255, 255},
&color.RGBA{255, 255, 255, 255},
}
colourBands = []uint8{
0x00,
0x5f,
0x87,
0xaf,
0xd7,
0xff,
}
)
func (t *Terminal) handleColorEscape(message string) {
if message == "" || message == "0" {
t.currentBG = nil
t.currentFG = nil
t.bold = false
t.blinking = false
return
}
modes := strings.Split(message, ";")
for i := 0; i < len(modes); i++ {
mode := modes[i]
if mode == "" {
continue
}
if (mode == "38" || mode == "48") && i+1 < len(modes) {
nextMode := modes[i+1]
if nextMode == "5" && i+2 < len(modes) {
t.handleColorModeMap(mode, modes[i+2])
i += 2
} else if nextMode == "2" && i+4 < len(modes) {
t.handleColorModeRGB(mode, modes[i+2], modes[i+3], modes[i+4])
i += 4
}
} else {
t.handleColorMode(mode)
}
}
}
func (t *Terminal) handleColorMode(modeStr string) {
mode, err := strconv.Atoi(modeStr)
if err != nil {
fyne.LogError("Failed to parse color mode: "+modeStr, err)
return
}
switch mode {
case 0:
t.currentBG, t.currentFG = nil, nil
t.bold = false
t.blinking = false
case 1:
t.bold = true
case 4, 24: //italic
case 5:
t.blinking = true
case 7: // reverse
bg, fg := t.currentBG, t.currentFG
if fg == nil {
t.currentBG = theme.Color(theme.ColorNameForeground)
} else {
t.currentBG = fg
}
if bg == nil {
t.currentFG = theme.Color(theme.ColorNameDisabledButton)
} else {
t.currentFG = bg
}
case 27: // reverse off
bg, fg := t.currentBG, t.currentFG
if fg != nil {
t.currentBG = nil
} else {
t.currentBG = fg
}
if bg != nil {
t.currentFG = nil
} else {
t.currentFG = bg
}
case 30, 31, 32, 33, 34, 35, 36, 37:
t.currentFG = basicColors[mode-30]
case 39:
t.currentFG = nil
case 40, 41, 42, 43, 44, 45, 46, 47:
t.currentBG = basicColors[mode-40]
case 49:
t.currentBG = nil
case 90, 91, 92, 93, 94, 95, 96, 97:
t.currentFG = brightColors[mode-90]
case 100, 101, 102, 103, 104, 105, 106, 107:
t.currentBG = brightColors[mode-100]
default:
if t.debug {
log.Println("Unsupported graphics mode", mode)
}
}
}
func (t *Terminal) handleColorModeMap(mode, ids string) {
var c color.Color
id, err := strconv.Atoi(ids)
if err != nil {
if t.debug {
log.Println("Invalid color map ID", ids)
}
return
}
if id <= 7 {
c = basicColors[id]
} else if id <= 15 {
c = brightColors[id-8]
} else if id <= 231 {
id -= 16
b := id % 6
id = (id - b) / 6
g := id % 6
r := (id - g) / 6
c = &color.RGBA{colourBands[r], colourBands[g], colourBands[b], 255}
} else if id <= 255 {
id -= 232
inc := 256 / 24
y := id * inc
c = &color.Gray{uint8(y)}
} else if t.debug {
log.Println("Invalid colour map ID", id)
}
if mode == "38" {
t.currentFG = c
} else if mode == "48" {
t.currentBG = c
}
}
func (t *Terminal) handleColorModeRGB(mode, rs, gs, bs string) {
r, _ := strconv.Atoi(rs)
g, _ := strconv.Atoi(gs)
b, _ := strconv.Atoi(bs)
c := &color.RGBA{uint8(r), uint8(g), uint8(b), 255}
if mode == "38" {
t.currentFG = c
} else if mode == "48" {
t.currentBG = c
}
}