/
color_picker.go
299 lines (263 loc) · 7.59 KB
/
color_picker.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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
package dialog
import (
"image/color"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
col "fyne.io/fyne/v2/internal/color"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
// newColorBasicPicker returns a component for selecting basic colors.
func newColorBasicPicker(callback func(color.Color)) fyne.CanvasObject {
return newColorButtonBox([]color.Color{
theme.PrimaryColorNamed(theme.ColorRed),
theme.PrimaryColorNamed(theme.ColorOrange),
theme.PrimaryColorNamed(theme.ColorYellow),
theme.PrimaryColorNamed(theme.ColorGreen),
theme.PrimaryColorNamed(theme.ColorBlue),
theme.PrimaryColorNamed(theme.ColorPurple),
theme.PrimaryColorNamed(theme.ColorBrown),
}, theme.ColorChromaticIcon(), callback)
}
// newColorGreyscalePicker returns a component for selecting greyscale colors.
func newColorGreyscalePicker(callback func(color.Color)) fyne.CanvasObject {
return newColorButtonBox(stringsToColors([]string{
"#ffffff",
"#cccccc",
"#aaaaaa",
"#888888",
"#555555",
"#333333",
"#000000",
}...), theme.ColorAchromaticIcon(), callback)
}
// newColorRecentPicker returns a component for selecting recent colors.
func newColorRecentPicker(callback func(color.Color)) fyne.CanvasObject {
return newColorButtonBox(stringsToColors(readRecentColors()...), theme.HistoryIcon(), callback)
}
var _ fyne.Widget = (*colorAdvancedPicker)(nil)
// colorAdvancedPicker widget is a component for selecting a color.
type colorAdvancedPicker struct {
widget.BaseWidget
Red, Green, Blue, Alpha int // Range 0-255
Hue int // Range 0-360 (degrees)
Saturation, Lightness int // Range 0-100 (percent)
ColorModel string
onChange func(color.Color)
}
// newColorAdvancedPicker returns a new color widget set to the given color.
func newColorAdvancedPicker(color color.Color, onChange func(color.Color)) *colorAdvancedPicker {
c := &colorAdvancedPicker{
onChange: onChange,
}
c.ExtendBaseWidget(c)
c.updateColor(color)
return c
}
// Color returns the currently selected color.
func (p *colorAdvancedPicker) Color() color.Color {
return &color.NRGBA{
uint8(p.Red),
uint8(p.Green),
uint8(p.Blue),
uint8(p.Alpha),
}
}
// SetColor updates the color selected in this color widget.
func (p *colorAdvancedPicker) SetColor(color color.Color) {
if p.updateColor(color) {
p.Refresh()
if f := p.onChange; f != nil {
f(color)
}
}
}
// SetHSLA updated the Hue, Saturation, Lightness, and Alpha components of the currently selected color.
func (p *colorAdvancedPicker) SetHSLA(h, s, l, a int) {
if p.updateHSLA(h, s, l, a) {
p.Refresh()
if f := p.onChange; f != nil {
f(p.Color())
}
}
}
// SetRGBA updated the Red, Green, Blue, and Alpha components of the currently selected color.
func (p *colorAdvancedPicker) SetRGBA(r, g, b, a int) {
if p.updateRGBA(r, g, b, a) {
p.Refresh()
if f := p.onChange; f != nil {
f(p.Color())
}
}
}
// MinSize returns the size that this widget should not shrink below.
func (p *colorAdvancedPicker) MinSize() fyne.Size {
p.ExtendBaseWidget(p)
return p.BaseWidget.MinSize()
}
// CreateRenderer is a private method to Fyne which links this widget to its renderer.
func (p *colorAdvancedPicker) CreateRenderer() fyne.WidgetRenderer {
p.ExtendBaseWidget(p)
// Preview
preview := &canvas.Rectangle{}
// HSL
hueChannel := newColorChannel("H", 0, 360, p.Hue, func(h int) {
p.SetHSLA(h, p.Saturation, p.Lightness, p.Alpha)
})
saturationChannel := newColorChannel("S", 0, 100, p.Saturation, func(s int) {
p.SetHSLA(p.Hue, s, p.Lightness, p.Alpha)
})
lightnessChannel := newColorChannel("L", 0, 100, p.Lightness, func(l int) {
p.SetHSLA(p.Hue, p.Saturation, l, p.Alpha)
})
hslBox := container.NewVBox(
hueChannel,
saturationChannel,
lightnessChannel,
)
// RGB
redChannel := newColorChannel("R", 0, 255, p.Red, func(r int) {
p.SetRGBA(r, p.Green, p.Blue, p.Alpha)
})
greenChannel := newColorChannel("G", 0, 255, p.Green, func(g int) {
p.SetRGBA(p.Red, g, p.Blue, p.Alpha)
})
blueChannel := newColorChannel("B", 0, 255, p.Blue, func(b int) {
p.SetRGBA(p.Red, p.Green, b, p.Alpha)
})
rgbBox := container.NewVBox(
redChannel,
greenChannel,
blueChannel,
)
// Wheel
wheel := newColorWheel(func(hue, saturation, lightness, alpha int) {
p.SetHSLA(hue, saturation, lightness, alpha)
})
// Alpha
alphaChannel := newColorChannel("A", 0, 255, p.Alpha, func(a int) {
p.SetRGBA(p.Red, p.Green, p.Blue, a)
})
// Hex
hex := newUserChangeEntry("")
hex.setOnChanged(func(text string) {
c, err := stringToColor(text)
if err != nil {
fyne.LogError("Error parsing color: "+text, err)
// TODO trigger entry invalid state
} else {
p.SetColor(c)
}
})
contents := container.NewPadded(container.NewVBox(
container.NewGridWithColumns(3,
container.NewPadded(wheel),
hslBox,
rgbBox),
container.NewGridWithColumns(3,
container.NewPadded(
container.NewMax(
newCheckeredBackground(),
preview,
),
),
hex,
alphaChannel,
),
))
r := &colorPickerRenderer{
WidgetRenderer: widget.NewSimpleRenderer(contents),
picker: p,
redChannel: redChannel,
greenChannel: greenChannel,
blueChannel: blueChannel,
hueChannel: hueChannel,
saturationChannel: saturationChannel,
lightnessChannel: lightnessChannel,
wheel: wheel,
preview: preview,
alphaChannel: alphaChannel,
hex: hex,
contents: contents,
}
r.updateObjects()
return r
}
func (p *colorAdvancedPicker) updateColor(color color.Color) bool {
r, g, b, a := col.ToNRGBA(color)
if p.Red == r && p.Green == g && p.Blue == b && p.Alpha == a {
return false
}
return p.updateRGBA(r, g, b, a)
}
func (p *colorAdvancedPicker) updateHSLA(h, s, l, a int) bool {
h = wrapHue(h)
s = clamp(s, 0, 100)
l = clamp(l, 0, 100)
a = clamp(a, 0, 255)
if p.Hue == h && p.Saturation == s && p.Lightness == l && p.Alpha == a {
return false
}
p.Hue = h
p.Saturation = s
p.Lightness = l
p.Alpha = a
p.Red, p.Green, p.Blue = hslToRgb(p.Hue, p.Saturation, p.Lightness)
return true
}
func (p *colorAdvancedPicker) updateRGBA(r, g, b, a int) bool {
r = clamp(r, 0, 255)
g = clamp(g, 0, 255)
b = clamp(b, 0, 255)
a = clamp(a, 0, 255)
if p.Red == r && p.Green == g && p.Blue == b && p.Alpha == a {
return false
}
p.Red = r
p.Green = g
p.Blue = b
p.Alpha = a
p.Hue, p.Saturation, p.Lightness = rgbToHsl(p.Red, p.Green, p.Blue)
return true
}
var _ fyne.WidgetRenderer = (*colorPickerRenderer)(nil)
type colorPickerRenderer struct {
fyne.WidgetRenderer
picker *colorAdvancedPicker
redChannel *colorChannel
greenChannel *colorChannel
blueChannel *colorChannel
hueChannel *colorChannel
saturationChannel *colorChannel
lightnessChannel *colorChannel
wheel *colorWheel
preview *canvas.Rectangle
alphaChannel *colorChannel
hex *userChangeEntry
contents fyne.CanvasObject
}
func (r *colorPickerRenderer) Refresh() {
r.updateObjects()
r.WidgetRenderer.Refresh()
}
func (r *colorPickerRenderer) updateObjects() {
// HSL
r.hueChannel.SetValue(r.picker.Hue)
r.saturationChannel.SetValue(r.picker.Saturation)
r.lightnessChannel.SetValue(r.picker.Lightness)
// RGB
r.redChannel.SetValue(r.picker.Red)
r.greenChannel.SetValue(r.picker.Green)
r.blueChannel.SetValue(r.picker.Blue)
// Wheel
r.wheel.SetHSLA(r.picker.Hue, r.picker.Saturation, r.picker.Lightness, r.picker.Alpha)
color := r.picker.Color()
// Preview
r.preview.FillColor = color
r.preview.Refresh()
// Alpha
r.alphaChannel.SetValue(r.picker.Alpha)
// Hex
r.hex.SetText(colorToString(color))
}