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
|
package picker
import (
"fmt"
"github.com/charmbracelet/bubbles/key"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/bubbletea-app-template/internal/colors"
"github.com/charmbracelet/bubbletea-app-template/internal/slider"
)
type Model struct {
title string
active int
sliders []slider.Model
}
func (m *Model) Next() int {
m.active = m.fixSel(m.active + 1)
return m.active
}
func (m *Model) Prev() int {
m.active = m.fixSel(m.active - 1)
return m.active
}
func (m *Model) Sel(i int) int {
m.active = m.fixSel(i)
return m.active
}
func (m Model) fixSel(val int) int {
size := len(m.sliders)
return (val%size + size) % size
}
func New(sliders []slider.Model, title string) *Model {
return &Model{
title: title,
active: 0,
sliders: sliders,
}
}
func (m Model) Title() string {
return m.title
}
func (m Model) GetColor() colors.ColorSpace {
switch m.title {
case "RGB":
return colors.RGB{
R: m.sliders[0].Val(),
G: m.sliders[1].Val(),
B: m.sliders[2].Val(),
}
case "CMYK":
return colors.CMYK{
C: m.sliders[0].Val(),
M: m.sliders[1].Val(),
Y: m.sliders[2].Val(),
K: m.sliders[3].Val(),
}
// TODO: HSL
default: // Default to white if we don't know the color space
return colors.RGB{
R: 255,
G: 255,
B: 255,
}
}
}
func (m Model) SetColor(c colors.ColorSpace) {
p := c.ToPrecise()
switch m.title {
case "RGB":
rgb := colors.RGB{}.FromPrecise(p).(colors.RGB)
m.sliders[0].Set(rgb.R)
m.sliders[1].Set(rgb.G)
m.sliders[2].Set(rgb.B)
case "CMYK":
cmyk := colors.CMYK{}.FromPrecise(p).(colors.CMYK)
m.sliders[0].Set(cmyk.C)
m.sliders[1].Set(cmyk.M)
m.sliders[2].Set(cmyk.Y)
m.sliders[3].Set(cmyk.K)
}
}
func (m Model) Init() tea.Cmd {
cmds := []tea.Cmd{}
for _, s := range m.sliders {
cmds = append(cmds, s.Init())
}
return tea.Batch(cmds...)
}
func (m Model) View() string {
var s string
for i, slider := range m.sliders {
if i == m.active {
s += fmt.Sprintf("\n-> %s", slider.View())
} else {
s += fmt.Sprintf("\n %s", slider.View())
}
}
return s
}
func (m Model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
keys := newKeybinds()
cmds := []tea.Cmd{}
switch msg := msg.(type) {
case tea.KeyMsg:
switch {
case key.Matches(msg, keys.next):
m.Next()
case key.Matches(msg, keys.prev):
m.Prev()
default:
newActive, cmd := m.sliders[m.active].Update(msg)
m.sliders[m.active] = newActive.(slider.Model)
cmds = append(cmds, cmd)
return m, tea.Batch(cmds...)
}
}
// Keys are only sent to the active sliders
// However, other messages (ex: tick, resize) must be sent to all
for i, s := range m.sliders {
newSlider, cmd := s.Update(msg)
m.sliders[i] = newSlider.(slider.Model)
cmds = append(cmds, cmd)
}
return m, tea.Batch(cmds...)
}
|