forked from quackduck/devzat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
colors.go
246 lines (226 loc) · 7.38 KB
/
colors.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
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package main
import (
"errors"
"fmt"
"math/rand"
"strconv"
"strings"
"github.com/acarl005/stripansi"
chromastyles "github.com/alecthomas/chroma/styles"
"github.com/jwalton/gchalk"
markdown "github.com/quackduck/go-term-markdown"
)
func makeFlag(colors []string) func(a string) string {
flag := make([]*gchalk.Builder, len(colors))
for i := range colors {
flag[i] = chalk.WithHex(colors[i])
}
return func(a string) string {
return applyRainbow(flag, a)
}
}
func applyRainbow(rainbow []*gchalk.Builder, a string) string {
a = stripansi.Strip(a)
buf := ""
colorOffset := rand.Intn(len(rainbow))
for i, r := range []rune(a) {
buf += rainbow[(colorOffset+i)%len(rainbow)].Paint(string(r))
}
return buf
}
var (
chalk = gchalk.New(gchalk.ForceLevel(gchalk.LevelAnsi256))
green = ansi256(1, 5, 1)
red = ansi256(5, 1, 1)
cyan = ansi256(1, 5, 5)
magenta = ansi256(5, 1, 5)
yellow = ansi256(5, 5, 1)
orange = ansi256(5, 3, 0)
blue = ansi256(0, 3, 5)
white = ansi256(5, 5, 5)
styles = []*style{
{"white", buildStyle(white)},
{"red", buildStyle(red)},
{"coral", buildStyle(ansi256(5, 2, 2))},
{"green", buildStyle(green)},
{"sky", buildStyle(ansi256(3, 5, 5))},
{"cyan", buildStyle(cyan)},
{"magenta", buildStyle(magenta)},
{"pink", buildStyle(ansi256(5, 3, 4))},
{"rose", buildStyle(ansi256(5, 0, 2))},
{"lavender", buildStyle(ansi256(4, 2, 5))},
{"fire", buildStyle(ansi256(5, 2, 0))},
{"pastel green", buildStyle(ansi256(0, 5, 3))},
{"olive", buildStyle(ansi256(4, 5, 1))},
{"yellow", buildStyle(yellow)},
{"orange", buildStyle(orange)},
{"blue", buildStyle(blue)}}
secretStyles = []*style{
{"ukraine", buildStyle(chalk.WithHex("#005bbb").WithBgHex("#ffd500"))},
{"easter", buildStyle(chalk.WithRGB(255, 51, 255).WithBgRGB(255, 255, 0))},
{"baby", buildStyle(chalk.WithRGB(255, 51, 255).WithBgRGB(102, 102, 255))},
{"hacker", buildStyle(chalk.WithRGB(0, 255, 0).WithBgRGB(0, 0, 0))},
{"l33t", buildStyleNoStrip(chalk.WithBgBrightBlack())},
{"whiten", buildStyleNoStrip(chalk.WithBgWhite())},
{"trans", makeFlag([]string{"#55CDFC", "#F7A8B8", "#FFFFFF", "#F7A8B8", "#55CDFC"})},
{"gay", makeFlag([]string{"#FF0018", "#FFA52C", "#FFFF41", "#008018", "#0000F9", "#86007D"})},
{"lesbian", makeFlag([]string{"#D62E02", "#FD9855", "#FFFFFF", "#D161A2", "#A20160"})},
{"bi", makeFlag([]string{"#D60270", "#D60270", "#9B4F96", "#0038A8", "#0038A8"})},
{"ace", makeFlag([]string{"#333333", "#A4A4A4", "#FFFFFF", "#810081"})},
{"pan", makeFlag([]string{"#FF1B8D", "#FFDA00", "#1BB3FF"})},
{"enby", makeFlag([]string{"#FFF430", "#FFFFFF", "#9C59D1", "#000000"})},
{"aro", makeFlag([]string{"#3AA63F", "#A8D47A", "#FFFFFF", "#AAAAAA", "#000000"})},
{"genderfluid", makeFlag([]string{"#FE75A1", "#FFFFFF", "#BE18D6", "#333333", "#333EBC"})},
{"agender", makeFlag([]string{"#333333", "#BCC5C6", "#FFFFFF", "#B5F582", "#FFFFFF", "#BCC5C6", "#333333"})},
{"rainbow", func(a string) string {
rainbow := []*gchalk.Builder{red, orange, yellow, green, cyan, blue, ansi256(2, 2, 5), magenta}
return applyRainbow(rainbow, a)
}}}
)
func init() {
markdown.CurrentTheme = chromastyles.ParaisoDark
}
type style struct {
name string
apply func(string) string
}
func buildStyle(c *gchalk.Builder) func(string) string {
return func(s string) string {
return c.Paint(stripansi.Strip(s))
}
}
func buildStyleNoStrip(c *gchalk.Builder) func(string) string {
return func(s string) string {
return c.Paint(s)
}
}
// with r, g and b values from 0 to 5
func ansi256(r, g, b uint8) *gchalk.Builder {
return chalk.WithRGB(255/5*r, 255/5*g, 255/5*b)
}
func bgAnsi256(r, g, b uint8) *gchalk.Builder {
return chalk.WithBgRGB(255/5*r, 255/5*g, 255/5*b)
}
// Applies color from name
func (u *user) changeColor(colorName string) error {
style, err := getStyle(colorName)
if err != nil {
return err
}
if strings.HasPrefix(colorName, "bg-") {
u.colorBG = style.name // update bg color
} else {
u.color = style.name // update fg color
}
//if colorName == "random" {
// u.room.broadcast("", "You're now using "+u.color)
//}
u.name, _ = applyColorToData(u.name, u.color, u.colorBG) // error can be discarded as it has already been checked earlier
u.term.SetPrompt(u.name + ": ")
saveBans()
return nil
}
func applyColorToData(data string, color string, colorBG string) (string, error) {
styleFG, err := getStyle(color)
if err != nil {
return "", err
}
styleBG, err := getStyle(colorBG)
if err != nil {
return "", err
}
return styleBG.apply(styleFG.apply(data)), nil // fg clears the bg color
}
// Sets either the foreground or the background with a random color if the
// given name is correct.
func getRandomColor(name string) *style {
var foreground bool
if name == "random" {
foreground = true
} else if name == "bg-random" {
foreground = false
} else {
return nil
}
r := rand.Intn(6)
g := rand.Intn(6)
b := rand.Intn(6)
if foreground {
return &style{fmt.Sprintf("%03d", r*100+g*10+b), buildStyle(ansi256(uint8(r), uint8(g), uint8(b)))}
}
return &style{fmt.Sprintf("bg-%03d", r*100+g*10+b), buildStyleNoStrip(bgAnsi256(uint8(r), uint8(g), uint8(b)))}
}
// If the input is a named style, returns it. Otherwise, returns nil.
func getNamedColor(name string) *style {
for i := range styles {
if styles[i].name == name {
return styles[i]
}
}
for i := range secretStyles {
if secretStyles[i].name == name {
return secretStyles[i]
}
}
return nil
}
func getCustomColor(name string) (*style, error) {
if strings.HasPrefix(name, "#") {
return &style{name, buildStyle(chalk.WithHex(name))}, nil
}
if strings.HasPrefix(name, "bg-#") {
return &style{name, buildStyleNoStrip(chalk.WithBgHex(strings.TrimPrefix(name, "bg-")))}, nil
}
if len(name) == 3 || len(name) == 6 {
rgbCode := name
if strings.HasPrefix(name, "bg-") {
rgbCode = strings.TrimPrefix(rgbCode, "bg-")
}
a, err := strconv.Atoi(rgbCode)
if err == nil {
r := (a / 100) % 10
g := (a / 10) % 10
b := a % 10
if r > 5 || g > 5 || b > 5 || r < 0 || g < 0 || b < 0 {
return nil, errors.New("custom colors have values from 0 to 5 smh")
}
if strings.HasPrefix(name, "bg-") {
return &style{name, buildStyleNoStrip(bgAnsi256(uint8(r), uint8(g), uint8(b)))}, nil
}
return &style{name, buildStyle(ansi256(uint8(r), uint8(g), uint8(b)))}, nil
}
return nil, err
}
return nil, nil
}
// Turns name into a style (defaults to nil)
func getStyle(name string) (*style, error) {
randomColor := getRandomColor(name)
if randomColor != nil {
return randomColor, nil
}
if name == "bg-off" {
return &style{"bg-off", func(a string) string { return a }}, nil // Used to remove one's background
}
namedColor := getNamedColor(name)
if namedColor != nil {
return namedColor, nil
}
if strings.HasPrefix(name, "#") {
return &style{name, buildStyle(chalk.WithHex(name))}, nil
}
customColor, err := getCustomColor(name)
if err != nil {
return nil, err
}
if customColor != nil {
return customColor, nil
}
return nil, errors.New("Which color? Choose from random, " + strings.Join(func() []string {
colors := make([]string, 0, len(styles))
for i := range styles {
colors = append(colors, styles[i].name)
}
return colors
}(), ", ") + " \nMake your own colors using hex (#A0FFFF, etc) or RGB values from 0 to 5 (for example, `color 530`, a pretty nice orange). Set bg color like this: color bg-530; remove bg color with color bg-off.\nThere's also a few secret colors :)")
}