zenity/color_windows.go

84 lines
1.7 KiB
Go
Raw Normal View History

2020-01-18 07:40:16 -05:00
package zenity
import (
"image/color"
"sync"
"unsafe"
)
var (
chooseColor = comdlg32.NewProc("ChooseColorW")
savedColors = [16]uint32{}
colorsMutex sync.Mutex
)
func init() {
for i := range savedColors {
savedColors[i] = 0xffffff
}
}
2021-03-04 07:42:30 -05:00
func selectColor(opts options) (color.Color, error) {
2020-01-18 07:40:16 -05:00
// load custom colors
colorsMutex.Lock()
customColors := savedColors
colorsMutex.Unlock()
2021-03-26 09:26:56 -04:00
var args _CHOOSECOLOR
2020-01-18 07:40:16 -05:00
args.StructSize = uint32(unsafe.Sizeof(args))
args.CustColors = &customColors
if opts.color != nil {
args.Flags |= 0x1 // CC_RGBINIT
2020-01-21 11:13:45 -05:00
n := color.NRGBAModel.Convert(opts.color).(color.NRGBA)
2021-04-25 19:36:15 -04:00
args.RgbResult = uint32(n.R) | uint32(n.G)<<8 | uint32(n.B)<<16
2020-01-18 07:40:16 -05:00
}
2020-01-24 07:52:45 -05:00
if opts.showPalette {
2020-01-27 08:44:47 -05:00
args.Flags |= 0x4 // CC_PREVENTFULLOPEN
2020-01-18 07:40:16 -05:00
} else {
2020-01-27 08:44:47 -05:00
args.Flags |= 0x2 // CC_FULLOPEN
2020-01-18 07:40:16 -05:00
}
2021-03-26 08:40:24 -04:00
defer setup()()
2020-01-18 07:40:16 -05:00
2021-03-03 22:25:45 -05:00
if opts.ctx != nil || opts.title != nil {
2020-01-30 09:14:42 -05:00
unhook, err := hookDialogTitle(opts.ctx, opts.title)
if err != nil {
2020-01-24 08:59:03 -05:00
return nil, err
}
2020-01-30 09:14:42 -05:00
defer unhook()
2020-01-24 08:59:03 -05:00
}
2020-01-29 09:15:21 -05:00
s, _, _ := chooseColor.Call(uintptr(unsafe.Pointer(&args)))
2020-01-30 09:14:42 -05:00
if opts.ctx != nil && opts.ctx.Err() != nil {
return nil, opts.ctx.Err()
}
2020-01-29 09:15:21 -05:00
if s == 0 {
2020-01-27 08:44:47 -05:00
return nil, commDlgError()
}
2020-01-18 07:40:16 -05:00
// 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
}
2021-03-10 09:49:09 -05:00
// https://docs.microsoft.com/en-us/windows/win32/api/commdlg/ns-commdlg-choosecolorw-r1
2021-03-26 09:26:56 -04:00
type _CHOOSECOLOR struct {
2020-01-18 07:40:16 -05:00
StructSize uint32
Owner uintptr
Instance uintptr
RgbResult uint32
CustColors *[16]uint32
Flags uint32
CustData uintptr
FnHook uintptr
TemplateName *uint16
}