-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
color_windows.go
68 lines (57 loc) · 1.35 KB
/
color_windows.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
package zenity
import (
"image/color"
"sync"
"unsafe"
"github.com/ncruces/zenity/internal/win"
)
var (
savedColors [16]uint32
colorsMutex sync.Mutex
)
func init() {
for i := range savedColors {
savedColors[i] = 0xffffff
}
}
func selectColor(opts options) (color.Color, error) {
// load custom colors
colorsMutex.Lock()
customColors := savedColors
colorsMutex.Unlock()
var args win.CHOOSECOLOR
args.StructSize = uint32(unsafe.Sizeof(args))
args.Owner, _ = opts.attach.(win.HWND)
args.CustColors = &customColors
if opts.color != nil {
args.Flags |= win.CC_RGBINIT
n := color.NRGBAModel.Convert(opts.color).(color.NRGBA)
args.RgbResult = uint32(n.R) | uint32(n.G)<<8 | uint32(n.B)<<16
}
if opts.showPalette {
args.Flags |= win.CC_PREVENTFULLOPEN
} else {
args.Flags |= win.CC_FULLOPEN
}
defer setup(args.Owner)()
unhook, err := hookDialog(opts.ctx, opts.windowIcon, opts.title, nil)
if err != nil {
return nil, err
}
defer unhook()
ok := win.ChooseColor(&args)
if opts.ctx != nil && opts.ctx.Err() != nil {
return nil, opts.ctx.Err()
}
if !ok {
return nil, win.CommDlgError()
}
// save custom colors back
colorsMutex.Lock()
savedColors = customColors
colorsMutex.Unlock()
r := uint8(args.RgbResult >> 0)
g := uint8(args.RgbResult >> 8)
b := uint8(args.RgbResult >> 16)
return color.RGBA{R: r, G: g, B: b, A: 255}, nil
}