-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathcolorpicker.go
93 lines (74 loc) · 2.33 KB
/
colorpicker.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
package nanogui
import (
"fmt"
"github.com/shibukawa/nanovgo"
)
type ColorPicker struct {
PopupButton
callback func(color nanovgo.Color)
colorWheel *ColorWheel
pickButton *Button
}
func NewColorPicker(parent Widget, colors ...nanovgo.Color) *ColorPicker {
var color nanovgo.Color
switch len(colors) {
case 0:
color = nanovgo.RGBAf(1.0, 0.0, 0.0, 1.0)
case 1:
color = colors[0]
default:
panic("NewColorPicker can accept only one extra parameter (color)")
}
colorPicker := &ColorPicker{}
// init PopupButton member
colorPicker.chevronIcon = IconRightOpen
colorPicker.SetIconPosition(ButtonIconLeftCentered)
colorPicker.SetFlags(ToggleButtonType | PopupButtonType)
parentWindow := parent.FindWindow()
colorPicker.popup = NewPopup(parentWindow.Parent(), parentWindow)
colorPicker.popup.panel.SetLayout(NewGroupLayout())
colorPicker.colorWheel = NewColorWheel(colorPicker.popup.panel)
colorPicker.pickButton = NewButton(colorPicker.popup.panel, "Pick")
colorPicker.pickButton.SetFixedSize(100, 25)
InitWidget(colorPicker, parent)
colorPicker.SetColor(color)
colorPicker.PopupButton.SetChangeCallback(func(flag bool) {
colorPicker.SetColor(colorPicker.BackgroundColor())
if colorPicker.callback != nil {
colorPicker.callback(colorPicker.BackgroundColor())
}
})
colorPicker.colorWheel.SetCallback(func(color nanovgo.Color) {
colorPicker.pickButton.SetBackgroundColor(color)
colorPicker.pickButton.SetTextColor(color.ContrastingColor())
})
colorPicker.pickButton.SetCallback(func() {
color := colorPicker.colorWheel.Color()
colorPicker.SetPushed(false)
colorPicker.SetColor(color)
if colorPicker.callback != nil {
colorPicker.callback(colorPicker.BackgroundColor())
}
})
return colorPicker
}
func (c *ColorPicker) SetCallback(callback func(color nanovgo.Color)) {
c.callback = callback
}
func (c *ColorPicker) Color() nanovgo.Color {
return c.BackgroundColor()
}
func (c *ColorPicker) SetColor(color nanovgo.Color) {
if !c.pushed {
fgColor := color.ContrastingColor()
c.SetBackgroundColor(color)
c.SetTextColor(fgColor)
c.colorWheel.SetColor(color)
c.pickButton.SetBackgroundColor(color)
c.pickButton.SetTextColor(fgColor)
}
}
func (c *ColorPicker) String() string {
cw := c.colorWheel
return c.StringHelper("ColorPicker", fmt.Sprintf("h:%f s:%f l:%f", cw.hue, cw.saturation, cw.lightness))
}