zenity/color_windows.go

69 lines
1.4 KiB
Go
Raw Normal View History

2020-01-18 07:40:16 -05:00
package zenity
import (
"image/color"
"sync"
"unsafe"
2022-06-17 22:45:49 -04:00
"github.com/ncruces/zenity/internal/win"
2020-01-18 07:40:16 -05:00
)
var (
2021-04-27 20:27:28 -04:00
savedColors [16]uint32
2020-01-18 07:40:16 -05:00
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()
2022-06-17 22:45:49 -04:00
var args win.CHOOSECOLOR
2020-01-18 07:40:16 -05:00
args.StructSize = uint32(unsafe.Sizeof(args))
2022-06-18 07:37:39 -04:00
args.Owner, _ = opts.attach.(win.HWND)
2020-01-18 07:40:16 -05:00
args.CustColors = &customColors
if opts.color != nil {
2022-06-17 22:45:49 -04:00
args.Flags |= win.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 {
2022-06-17 22:45:49 -04:00
args.Flags |= win.CC_PREVENTFULLOPEN
2020-01-18 07:40:16 -05:00
} else {
2022-06-17 22:45:49 -04:00
args.Flags |= win.CC_FULLOPEN
2020-01-18 07:40:16 -05:00
}
2022-06-28 09:52:02 -04:00
defer setup(args.Owner)()
2022-06-23 09:49:08 -04:00
unhook, err := hookDialog(opts.ctx, opts.windowIcon, opts.title, nil)
if err != nil {
return nil, err
2020-01-24 08:59:03 -05:00
}
2022-06-23 09:49:08 -04:00
defer unhook()
2020-01-24 08:59:03 -05:00
2022-06-17 22:45:49 -04:00
ok := win.ChooseColor(&args)
2020-01-30 09:14:42 -05:00
if opts.ctx != nil && opts.ctx.Err() != nil {
return nil, opts.ctx.Err()
}
2022-06-17 22:45:49 -04:00
if !ok {
return nil, win.CommDlgError()
2020-01-27 08:44:47 -05:00
}
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
}