-
-
Notifications
You must be signed in to change notification settings - Fork 73
/
color.go
204 lines (169 loc) · 4.29 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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
package termenv
import (
"errors"
"fmt"
"math"
"strings"
"github.com/lucasb-eyer/go-colorful"
)
var (
// ErrInvalidColor gets returned when a color is invalid.
ErrInvalidColor = errors.New("invalid color")
)
// Foreground and Background sequence codes.
const (
Foreground = "38"
Background = "48"
)
// Color is an interface implemented by all colors that can be converted to an
// ANSI sequence.
type Color interface {
// Sequence returns the ANSI Sequence for the color.
Sequence(bg bool) string
}
// NoColor is a nop for terminals that don't support colors.
type NoColor struct{}
func (c NoColor) String() string {
return ""
}
// ANSIColor is a color (0-15) as defined by the ANSI Standard.
type ANSIColor int
func (c ANSIColor) String() string {
return ansiHex[c]
}
// ANSI256Color is a color (16-255) as defined by the ANSI Standard.
type ANSI256Color int
func (c ANSI256Color) String() string {
return ansiHex[c]
}
// RGBColor is a hex-encoded color, e.g. "#abcdef".
type RGBColor string
// ConvertToRGB converts a Color to a colorful.Color.
func ConvertToRGB(c Color) colorful.Color {
var hex string
switch v := c.(type) {
case RGBColor:
hex = string(v)
case ANSIColor:
hex = ansiHex[v]
case ANSI256Color:
hex = ansiHex[v]
}
ch, _ := colorful.Hex(hex)
return ch
}
// Sequence returns the ANSI Sequence for the color.
func (c NoColor) Sequence(_ bool) string {
return ""
}
// Sequence returns the ANSI Sequence for the color.
func (c ANSIColor) Sequence(bg bool) string {
col := int(c)
bgMod := func(c int) int {
if bg {
return c + 10
}
return c
}
if col < 8 {
return fmt.Sprintf("%d", bgMod(col)+30)
}
return fmt.Sprintf("%d", bgMod(col-8)+90)
}
// Sequence returns the ANSI Sequence for the color.
func (c ANSI256Color) Sequence(bg bool) string {
prefix := Foreground
if bg {
prefix = Background
}
return fmt.Sprintf("%s;5;%d", prefix, c)
}
// Sequence returns the ANSI Sequence for the color.
func (c RGBColor) Sequence(bg bool) string {
f, err := colorful.Hex(string(c))
if err != nil {
return ""
}
prefix := Foreground
if bg {
prefix = Background
}
return fmt.Sprintf("%s;2;%d;%d;%d", prefix, uint8(f.R*255), uint8(f.G*255), uint8(f.B*255))
}
func xTermColor(s string) (RGBColor, error) {
if len(s) < 24 || len(s) > 25 {
return RGBColor(""), ErrInvalidColor
}
switch {
case strings.HasSuffix(s, string(BEL)):
s = strings.TrimSuffix(s, string(BEL))
case strings.HasSuffix(s, string(ESC)):
s = strings.TrimSuffix(s, string(ESC))
case strings.HasSuffix(s, ST):
s = strings.TrimSuffix(s, ST)
default:
return RGBColor(""), ErrInvalidColor
}
s = s[4:]
prefix := ";rgb:"
if !strings.HasPrefix(s, prefix) {
return RGBColor(""), ErrInvalidColor
}
s = strings.TrimPrefix(s, prefix)
h := strings.Split(s, "/")
hex := fmt.Sprintf("#%s%s%s", h[0][:2], h[1][:2], h[2][:2])
return RGBColor(hex), nil
}
func ansi256ToANSIColor(c ANSI256Color) ANSIColor {
var r int
md := math.MaxFloat64
h, _ := colorful.Hex(ansiHex[c])
for i := 0; i <= 15; i++ {
hb, _ := colorful.Hex(ansiHex[i])
d := h.DistanceHSLuv(hb)
if d < md {
md = d
r = i
}
}
return ANSIColor(r)
}
func hexToANSI256Color(c colorful.Color) ANSI256Color {
v2ci := func(v float64) int {
if v < 48 {
return 0
}
if v < 115 {
return 1
}
return int((v - 35) / 40)
}
// Calculate the nearest 0-based color index at 16..231
r := v2ci(c.R * 255.0) // 0..5 each
g := v2ci(c.G * 255.0)
b := v2ci(c.B * 255.0)
ci := 36*r + 6*g + b /* 0..215 */
// Calculate the represented colors back from the index
i2cv := [6]int{0, 0x5f, 0x87, 0xaf, 0xd7, 0xff}
cr := i2cv[r] // r/g/b, 0..255 each
cg := i2cv[g]
cb := i2cv[b]
// Calculate the nearest 0-based gray index at 232..255
var grayIdx int
average := (r + g + b) / 3
if average > 238 {
grayIdx = 23
} else {
grayIdx = (average - 3) / 10 // 0..23
}
gv := 8 + 10*grayIdx // same value for r/g/b, 0..255
// Return the one which is nearer to the original input rgb value
c2 := colorful.Color{R: float64(cr) / 255.0, G: float64(cg) / 255.0, B: float64(cb) / 255.0}
g2 := colorful.Color{R: float64(gv) / 255.0, G: float64(gv) / 255.0, B: float64(gv) / 255.0}
colorDist := c.DistanceHSLuv(c2)
grayDist := c.DistanceHSLuv(g2)
if colorDist <= grayDist {
return ANSI256Color(16 + ci)
}
return ANSI256Color(232 + grayIdx)
}