/
color.go
354 lines (307 loc) · 7.99 KB
/
color.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
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
package dialog
import (
"fmt"
"image/color"
"math"
"strings"
"fyne.io/fyne"
"fyne.io/fyne/canvas"
"fyne.io/fyne/layout"
"fyne.io/fyne/theme"
"fyne.io/fyne/widget"
)
// ColorPickerDialog is a simple dialog window that displays a color picker.
//
// Since: 1.4
type ColorPickerDialog struct {
*dialog
Advanced bool
color color.Color
callback func(c color.Color)
advanced *widget.Accordion
picker *colorAdvancedPicker
}
// NewColorPicker creates a color dialog and returns the handle.
// Using the returned type you should call Show() and then set its color through SetColor().
// The callback is triggered when the user selects a color.
//
// Since: 1.4
func NewColorPicker(title, message string, callback func(c color.Color), parent fyne.Window) *ColorPickerDialog {
p := &ColorPickerDialog{
dialog: newDialog(title, message, theme.ColorPaletteIcon(), nil /*cancel?*/, parent),
color: theme.PrimaryColor(),
callback: callback,
}
return p
}
// ShowColorPicker creates and shows a color dialog.
// The callback is triggered when the user selects a color.
//
// Since: 1.4
func ShowColorPicker(title, message string, callback func(c color.Color), parent fyne.Window) {
NewColorPicker(title, message, callback, parent).Show()
}
// Refresh causes this dialog to be updated
func (p *ColorPickerDialog) Refresh() {
p.updateUI()
}
// SetColor updates the color of the color picker.
func (p *ColorPickerDialog) SetColor(c color.Color) {
p.picker.SetColor(c)
}
// Show causes this dialog to be displayed
func (p *ColorPickerDialog) Show() {
if p.win == nil || p.Advanced != (p.advanced != nil) {
p.updateUI()
}
p.dialog.Show()
}
func (p *ColorPickerDialog) createSimplePickers() (contents []fyne.CanvasObject) {
contents = append(contents, newColorBasicPicker(p.selectColor))
contents = append(contents, newColorGreyscalePicker(p.selectColor))
if recent := newColorRecentPicker(p.selectColor); len(recent.(*fyne.Container).Objects) > 0 {
// Add divider and recents if there are any
contents = append(contents, canvas.NewLine(theme.ShadowColor()), recent)
}
return
}
func (p *ColorPickerDialog) selectColor(c color.Color) {
p.dialog.Hide()
writeRecentColor(colorToString(p.color))
if f := p.callback; f != nil {
f(c)
}
}
func (p *ColorPickerDialog) updateUI() {
if w := p.win; w != nil {
w.Hide()
}
p.dialog.dismiss = &widget.Button{Text: "Cancel", Icon: theme.CancelIcon(),
OnTapped: p.dialog.Hide,
}
if p.Advanced {
p.picker = newColorAdvancedPicker(p.color, func(c color.Color) {
p.color = c
})
p.advanced = widget.NewAccordion(widget.NewAccordionItem("Advanced", p.picker))
p.dialog.content = fyne.NewContainerWithLayout(layout.NewVBoxLayout(),
fyne.NewContainerWithLayout(layout.NewCenterLayout(),
fyne.NewContainerWithLayout(layout.NewVBoxLayout(),
p.createSimplePickers()...,
),
),
widget.NewSeparator(),
p.advanced,
)
confirm := &widget.Button{Text: "Confirm", Icon: theme.ConfirmIcon(), Style: widget.PrimaryButton,
OnTapped: func() {
p.selectColor(p.color)
},
}
p.dialog.setButtons(newButtonList(p.dialog.dismiss, confirm))
} else {
p.dialog.content = fyne.NewContainerWithLayout(layout.NewVBoxLayout(), p.createSimplePickers()...)
p.dialog.setButtons(newButtonList(p.dialog.dismiss))
}
}
func clamp(value, min, max int) int {
if value < min {
value = min
}
if value > max {
value = max
}
return value
}
func wrapHue(hue int) int {
for hue < 0 {
hue += 360
}
for hue > 360 {
hue -= 360
}
return hue
}
func newColorButtonBox(colors []color.Color, icon fyne.Resource, callback func(color.Color)) fyne.CanvasObject {
var objects []fyne.CanvasObject
if icon != nil && len(colors) > 0 {
objects = append(objects, widget.NewIcon(icon))
}
for _, c := range colors {
objects = append(objects, newColorButton(c, callback))
}
return fyne.NewContainerWithLayout(layout.NewGridLayoutWithColumns(8), objects...)
}
func newCheckeredBackground() *canvas.Raster {
return canvas.NewRasterWithPixels(func(x, y, _, _ int) color.Color {
const boxSize = 10
if (x/boxSize)%2 == (y/boxSize)%2 {
return color.Gray{Y: 58}
}
return color.Gray{Y: 84}
})
}
const (
preferenceRecents = "color_recents"
preferenceMaxRecents = 8
)
func readRecentColors() (recents []string) {
for _, r := range strings.Split(fyne.CurrentApp().Preferences().String(preferenceRecents), ",") {
if r != "" {
recents = append(recents, r)
}
}
return
}
func writeRecentColor(color string) {
recents := []string{color}
for _, r := range readRecentColors() {
if r == color {
continue // Color already in recents
}
recents = append(recents, r)
}
if len(recents) > preferenceMaxRecents {
recents = recents[:preferenceMaxRecents]
}
fyne.CurrentApp().Preferences().SetString(preferenceRecents, strings.Join(recents, ","))
}
func colorToString(c color.Color) string {
red, green, blue, alpha := colorToRGBA(c)
if alpha == 0xff {
return fmt.Sprintf("#%02x%02x%02x", red, green, blue)
}
return fmt.Sprintf("#%02x%02x%02x%02x", red, green, blue, alpha)
}
func stringToColor(s string) (color.Color, error) {
var c color.NRGBA
var err error
if len(s) == 7 {
c.A = 0xFF
_, err = fmt.Sscanf(s, "#%02x%02x%02x", &c.R, &c.G, &c.B)
} else {
_, err = fmt.Sscanf(s, "#%02x%02x%02x%02x", &c.R, &c.G, &c.B, &c.A)
}
return c, err
}
func stringsToColors(ss ...string) (colors []color.Color) {
for _, s := range ss {
if s == "" {
continue
}
c, err := stringToColor(s)
if err != nil {
fyne.LogError("Couldn't parse color:", err)
} else {
colors = append(colors, c)
}
}
return
}
func colorToHSLA(c color.Color) (int, int, int, int) {
r, g, b, a := colorToRGBA(c)
h, s, l := rgbToHsl(r, g, b)
return h, s, l, a
}
func colorToRGBA(c color.Color) (r, g, b, a int) {
switch col := c.(type) {
case color.NRGBA:
r = int(col.R)
g = int(col.G)
b = int(col.B)
a = int(col.A)
case *color.NRGBA:
r = int(col.R)
g = int(col.G)
b = int(col.B)
a = int(col.A)
default:
red, green, blue, alpha := c.RGBA()
if alpha != 0 && alpha != 1 {
red /= alpha
green /= alpha
blue /= alpha
}
// Convert from range 0-65535 to range 0-255
r = int(float64(red) / 255.0)
g = int(float64(green) / 255.0)
b = int(float64(blue) / 255.0)
a = int(float64(alpha) / 255.0)
}
return
}
// https://www.niwa.nu/2013/05/math-behind-colorspace-conversions-rgb-hsl/
func rgbToHsl(r, g, b int) (int, int, int) {
red := float64(r) / 255.0
green := float64(g) / 255.0
blue := float64(b) / 255.0
min := math.Min(red, math.Min(green, blue))
max := math.Max(red, math.Max(green, blue))
lightness := (max + min) / 2.0
delta := max - min
if delta == 0.0 {
// Achromatic
return 0, 0, int(lightness * 100.0)
}
// Chromatic
var saturation float64
if lightness < 0.5 {
saturation = (max - min) / (max + min)
} else {
saturation = (max - min) / (2.0 - max - min)
}
var hue float64
if red == max {
hue = (green - blue) / delta
} else if green == max {
hue = 2.0 + (blue-red)/delta
} else if blue == max {
hue = 4.0 + (red-green)/delta
}
h := wrapHue(int(hue * 60.0))
s := int(saturation * 100.0)
l := int(lightness * 100.0)
return h, s, l
}
func hslToRgb(h, s, l int) (int, int, int) {
hue := float64(h) / 360.0
saturation := float64(s) / 100.0
lightness := float64(l) / 100.0
if saturation == 0.0 {
// Greyscale
g := int(lightness * 255.0)
return g, g, g
}
var v1 float64
if lightness < 0.5 {
v1 = lightness * (1.0 + saturation)
} else {
v1 = (lightness + saturation) - (lightness * saturation)
}
v2 := 2.0*lightness - v1
red := hueToChannel(hue+(1.0/3.0), v1, v2)
green := hueToChannel(hue, v1, v2)
blue := hueToChannel(hue-(1.0/3.0), v1, v2)
r := int(math.Round(255.0 * red))
g := int(math.Round(255.0 * green))
b := int(math.Round(255.0 * blue))
return r, g, b
}
func hueToChannel(h, v1, v2 float64) float64 {
for h < 0.0 {
h += 1.0
}
for h > 1.0 {
h -= 1.0
}
if 6.0*h < 1.0 {
return v2 + (v1-v2)*6*h
}
if 2.0*h < 1.0 {
return v1
}
if 3.0*h < 2.0 {
return v2 + (v1-v2)*6*((2.0/3.0)-h)
}
return v2
}