forked from gizak/termui
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathstyle_gradient.go
More file actions
112 lines (93 loc) · 2.49 KB
/
style_gradient.go
File metadata and controls
112 lines (93 loc) · 2.49 KB
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
package gotui
import (
"fmt"
)
func InterpolateColor(c1, c2 Color, step, steps int) Color {
if steps <= 1 {
return c1
}
if step >= steps {
return c2
}
r1, g1, b1 := c1.RGB()
r2, g2, b2 := c2.RGB()
factor := float64(step) / float64(steps-1)
r := int32(float64(r1) + factor*float64(r2-r1))
g := int32(float64(g1) + factor*float64(g2-g1))
b := int32(float64(b1) + factor*float64(b2-b1))
return NewRGBColor(r, g, b)
}
// GenerateGradient generates a gradient from start to end color.
func GenerateGradient(start, end Color, length int) []Color {
if length <= 0 {
return []Color{}
}
colors := make([]Color, length)
for i := range length {
colors[i] = InterpolateColor(start, end, i, length)
}
return colors
}
// ApplyGradientToText applies a gradient to a string of text.
func ApplyGradientToText(text string, start, end Color) []Cell {
runes := []rune(text)
colors := GenerateGradient(start, end, len(runes))
cells := make([]Cell, len(runes))
for i, r := range runes {
cells[i] = Cell{
Rune: r,
Style: NewStyle(colors[i]),
}
}
return cells
}
// HexToColor converts a hex color string to a Color.
func HexToColor(hex string) (Color, error) {
if len(hex) > 0 && hex[0] == '#' {
hex = hex[1:]
}
if len(hex) != 6 {
return Color(0), fmt.Errorf("invalid hex color length: %d, expected 6", len(hex))
}
var r, g, b int32
n, err := fmt.Sscanf(hex, "%02x%02x%02x", &r, &g, &b)
if err != nil {
return Color(0), err
}
if n != 3 {
return Color(0), fmt.Errorf("invalid hex color format")
}
return NewRGBColor(r, g, b), nil
}
// GenerateMultiGradient generates a gradient that transitions through multiple colors.
func GenerateMultiGradient(length int, colors ...Color) []Color {
if length <= 0 || len(colors) == 0 {
return []Color{}
}
if len(colors) == 1 || length == 1 {
res := make([]Color, length)
for i := range res {
res[i] = colors[0]
}
return res
}
result := make([]Color, length)
segments := len(colors) - 1
for i := range length {
t := float64(i) / float64(length-1) * float64(segments)
segmentIdx := int(t)
if segmentIdx >= segments {
segmentIdx = segments - 1
}
segmentT := t - float64(segmentIdx)
startColor := colors[segmentIdx]
endColor := colors[segmentIdx+1]
r1, g1, b1 := startColor.RGB()
r2, g2, b2 := endColor.RGB()
r := int32(float64(r1) + segmentT*float64(r2-r1))
g := int32(float64(g1) + segmentT*float64(g2-g1))
b := int32(float64(b1) + segmentT*float64(b2-b1))
result[i] = NewRGBColor(r, g, b)
}
return result
}