-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgradient.go
277 lines (241 loc) · 6.54 KB
/
gradient.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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package gradient
import (
"fmt"
"strings"
)
// FontColor Convert a string (RGB code), slice (RGB array), or individual RGB values to ANSI font color
func FontColor(input ...interface{}) string {
var rgb []int
if len(input) == 1 {
switch v := input[0].(type) {
case string:
rgb, _ = CodeToRGB(v)
case []int:
rgb = v
if len(rgb) != 3 {
return ""
}
case [3]int:
rgb = v[:]
default:
return ""
}
} else if len(input) == 3 {
for _, v := range input {
if value, ok := v.(int); ok {
rgb = append(rgb, value)
} else {
return ""
}
}
} else {
return ""
}
return RGBToANSI(rgb...)
}
// BackgroundColor Convert a string (RGB code), slice (RGB array), or individual RGB values to ANSI background color
func BackgroundColor(input ...interface{}) string {
var rgb []int
var err error
if len(input) == 1 {
switch v := input[0].(type) {
case string:
if strings.HasPrefix(v, "\033[") {
rgb, err = ANSIToRGB(v)
if err != nil {
return ""
}
} else {
rgb, _ = CodeToRGB(v)
}
case []int:
rgb = v
if len(rgb) != 3 {
return ""
}
case [3]int:
rgb = v[:]
default:
return ""
}
} else if len(input) == 3 {
for _, v := range input {
if value, ok := v.(int); ok {
rgb = append(rgb, value)
} else {
return ""
}
}
} else {
return ""
}
return RGBToBgANSI(rgb...)
}
// GradientText Generate text with a gradient font color
func GradientText(text string, colors ...interface{}) string {
if len(colors) < 2 {
return text
}
// Convert all colors to RGB
var rgbColors [][]int
for _, color := range colors {
rgb, err := ConvertToRGB(color)
if err != nil {
return text
}
rgbColors = append(rgbColors, rgb)
}
// Split text into lines
lines := strings.Split(text, "\n")
totalChars := 0
for _, line := range lines {
totalChars += len(line)
}
// Calculate gradient steps
steps := totalChars - 1
segments := len(rgbColors) - 1
segmentLength := steps / segments
var result strings.Builder
charIndex := 0
for _, line := range lines {
for _, char := range line {
segment := charIndex / (segmentLength + 1)
if segment >= segments {
segment = segments - 1
}
startColor := rgbColors[segment]
endColor := rgbColors[segment+1]
// Calculate the interpolated color
ratio := float64(charIndex%(segmentLength+1)) / float64(segmentLength+1)
r := int(float64(startColor[0])*(1-ratio) + float64(endColor[0])*ratio)
g := int(float64(startColor[1])*(1-ratio) + float64(endColor[1])*ratio)
b := int(float64(startColor[2])*(1-ratio) + float64(endColor[2])*ratio)
// Append the colored character to the result
result.WriteString(fmt.Sprintf("\033[38;2;%d;%d;%dm%c", r, g, b, char))
charIndex++
}
// result.WriteString("\n")
}
// Reset color at the end
result.WriteString("\033[0m")
return result.String()
}
// GradientBackgroundText Generate text with a gradient background color
func GradientBackgroundText(text string, colors ...interface{}) string {
if len(colors) < 2 {
return text
}
// Convert all colors to RGB
var rgbColors [][]int
for _, color := range colors {
rgb, err := ConvertToRGB(color)
if err != nil {
return text
}
rgbColors = append(rgbColors, rgb)
}
// Split text into lines
lines := strings.Split(text, "\n")
totalChars := 0
for _, line := range lines {
totalChars += len(line)
}
// Calculate gradient steps
steps := totalChars - 1
segments := len(rgbColors) - 1
segmentLength := steps / segments
var result strings.Builder
charIndex := 0
for _, line := range lines {
for _, char := range line {
segment := charIndex / (segmentLength + 1)
if segment >= segments {
segment = segments - 1
}
startColor := rgbColors[segment]
endColor := rgbColors[segment+1]
// Calculate the interpolated color
ratio := float64(charIndex%(segmentLength+1)) / float64(segmentLength+1)
r := int(float64(startColor[0])*(1-ratio) + float64(endColor[0])*ratio)
g := int(float64(startColor[1])*(1-ratio) + float64(endColor[1])*ratio)
b := int(float64(startColor[2])*(1-ratio) + float64(endColor[2])*ratio)
// Append the colored character to the result
result.WriteString(fmt.Sprintf("\033[48;2;%d;%d;%dm%c", r, g, b, char))
charIndex++
}
// Reset color at the end of each line
result.WriteString("\033[0m")
}
return result.String()
}
// GradientTextWithBackground Generate text with both gradient font and background
func GradientTextWithBackground(text string, fontColors []interface{}, bgColors []interface{}) string {
if len(fontColors) < 2 || len(bgColors) < 2 {
return text
}
// Generate gradient font text
fontText := GradientText(text, fontColors...)
// Generate gradient background text
bgText := GradientBackgroundText(text, bgColors...)
// Combine font and background gradients
var result strings.Builder
fontLines := strings.Split(fontText, "\n")
bgLines := strings.Split(bgText, "\n")
for i := range fontLines {
if i < len(bgLines) {
result.WriteString(bgLines[i])
result.WriteString(fontLines[i])
result.WriteString("\033[0m\n")
}
}
return result.String()
}
// BlendColors Blends multiple colors into one average color
func BlendColors(colors ...interface{}) ([]int, error) {
if len(colors) == 0 {
return nil, fmt.Errorf("at least one color is required")
}
var totalR, totalG, totalB int
for _, color := range colors {
rgb, err := ConvertToRGB(color)
if err != nil {
return nil, err
}
totalR += rgb[0]
totalG += rgb[1]
totalB += rgb[2]
}
numColors := len(colors)
averageColor := []int{
totalR / numColors,
totalG / numColors,
totalB / numColors,
}
return averageColor, nil
}
// Convert a slice of strings to a slice of interfaces
func convertStringsToInterfaces(colors []string) []interface{} {
var result []interface{}
for _, color := range colors {
result = append(result, color)
}
return result
}
// GradientTextWithTemplate Generate text with a gradient font color using a predefined template
func GradientTextWithTemplate(text string, templateName string) string {
colors, ok := GradientTemplates[templateName]
if !ok {
return text
}
interfaceColors := convertStringsToInterfaces(colors)
return GradientText(text, interfaceColors...)
}
// GradientBackgroundTextWithTemplate Generate text with a gradient background color using a predefined template
func GradientBackgroundTextWithTemplate(text string, templateName string) string {
colors, ok := GradientTemplates[templateName]
if !ok {
return text
}
interfaceColors := convertStringsToInterfaces(colors)
return GradientBackgroundText(text, interfaceColors...)
}