Color dialog (macos).

This commit is contained in:
Nuno Cruces 2020-01-19 04:28:06 +00:00
parent 2acdf8ef7f
commit f20505e1f4
9 changed files with 113 additions and 0 deletions

28
color_darwin.go Normal file
View file

@ -0,0 +1,28 @@
package zenity
import (
"image/color"
"os/exec"
"github.com/ncruces/zenity/internal/osa"
"github.com/ncruces/zenity/internal/zen"
)
func SelectColor(options ...Option) (color.Color, error) {
opts := optsParse(options)
var data osa.Color
if opts.color != nil {
r, g, b, _ := opts.color.RGBA()
data.Color = []float32{float32(r) / 0xffff, float32(g) / 0xffff, float32(b) / 0xffff}
}
out, err := osa.Run("color", data)
if err, ok := err.(*exec.ExitError); ok && err.ExitCode() == 1 {
return nil, nil
}
if err != nil {
return nil, err
}
return zen.ParseColor(string(out)), nil
}

2
go.mod
View file

@ -1,3 +1,5 @@
module github.com/ncruces/zenity
go 1.13
require golang.org/x/image v0.0.0-20191214001246-9130b4cfad52

3
go.sum
View file

@ -0,0 +1,3 @@
golang.org/x/image v0.0.0-20191214001246-9130b4cfad52 h1:2fktqPPvDiVEEVT/vSTeoUPXfmRxRaGy6GU8jypvEn0=
golang.org/x/image v0.0.0-20191214001246-9130b4cfad52/go.mod h1:FeLwcggjj3mMvU+oOTbSwawSJRM1uh48EjtB4UJZlP0=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=

View file

@ -6,6 +6,21 @@ package osa
import "html/template"
var scripts = template.Must(template.New("").Parse(`
{{define "color"}}<script>var app = Application.currentApplication()
app.includeStandardAdditions = true
app.activate()
var opts = {}
{{if .Color -}}
opts.defaultColor = {{.Color}}
{{end -}}
var res = app.chooseColor(opts)
if (Array.isArray(res)) {
res[0] = Math.round(255*res[0])
res[1] = Math.round(255*res[1])
res[2] = Math.round(255*res[2])
'rgb('+res+')'
}
</script>{{end}}
{{define "file"}}<script>var app = Application.currentApplication()
app.includeStandardAdditions = true
app.activate()

View file

@ -44,6 +44,10 @@ type File struct {
Multiple bool
}
type Color struct {
Color []float32
}
type Msg struct {
Operation string
Text string

View file

@ -0,0 +1,17 @@
var app = Application.currentApplication()
app.includeStandardAdditions = true
app.activate()
var opts = {}
{{if .Color -}}
opts.defaultColor = {{.Color}}
{{end -}}
var res = app.chooseColor(opts)
if (Array.isArray(res)) {
res[0] = Math.round(255*res[0])
res[1] = Math.round(255*res[1])
res[2] = Math.round(255*res[2])
'rgb('+res+')'
}

44
internal/zen/color.go Normal file
View file

@ -0,0 +1,44 @@
package zen
import (
"fmt"
"image/color"
"strings"
"golang.org/x/image/colornames"
)
func ParseColor(s string) color.Color {
if len(s) == 4 || len(s) == 5 {
c := color.NRGBA{A: 0xf}
n, _ := fmt.Sscanf(s, "#%1x%1x%1x%1x", &c.R, &c.G, &c.B, &c.A)
c.R, c.G, c.B, c.A = c.R*0x11, c.G*0x11, c.B*0x11, c.A*0x11
if n >= 3 {
return c
}
}
if len(s) == 7 || len(s) == 9 {
c := color.NRGBA{A: 0xff}
n, _ := fmt.Sscanf(s, "#%02x%02x%02x%02x", &c.R, &c.G, &c.B, &c.A)
if n >= 3 {
return c
}
}
if len(s) >= 10 {
c := color.NRGBA{A: 0xff}
if _, err := fmt.Sscanf(s, "rgb(%d,%d,%d)\n", &c.R, &c.G, &c.B); err == nil {
return c
}
var a float32
if _, err := fmt.Sscanf(s, "rgba(%d,%d,%d,%f)\n", &c.R, &c.G, &c.B, &a); err == nil {
c.A = uint8(255*a + 0.5)
return c
}
}
c, _ := colornames.Map[strings.ToLower(s)]
return c
}