-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
137 lines (118 loc) · 3.31 KB
/
main.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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
package main
import (
"fmt"
"image"
"image/color"
"math"
"time"
"github.com/BurntSushi/xgb/xproto"
"github.com/BurntSushi/xgbutil"
"fyne.io/fyne/v2"
"fyne.io/fyne/v2/app"
"fyne.io/fyne/v2/canvas"
"fyne.io/fyne/v2/container"
"fyne.io/fyne/v2/dialog"
"fyne.io/fyne/v2/driver/desktop"
"fyne.io/fyne/v2/theme"
"fyne.io/fyne/v2/widget"
)
const (
pixCount = 25
pixMin = 5
)
func main() {
a := app.NewWithID("com.fyshos.focus")
w := a.NewWindow("Focus")
preview := &canvas.Image{}
preview.ScaleMode = canvas.ImageScalePixels
preview.FillMode = canvas.ImageFillContain
preview.SetMinSize(fyne.NewSquareSize(pixCount * pixMin))
highlight := canvas.NewRectangle(color.Transparent)
highlight.StrokeColor = theme.ErrorColor()
highlight.StrokeWidth = 1
highlight.SetMinSize(fyne.NewSquareSize(pixMin + 2))
output := widget.NewLabel("#000000")
choose := widget.NewSelect([]string{"Hex", "rgb"}, nil)
choose.PlaceHolder = "Hex"
choose.Selected = "Hex"
copyAction := func() {
c := w.Clipboard()
if c == nil {
return // can happen in some cases...
}
c.SetContent(output.Text)
}
copyToClip := widget.NewButtonWithIcon("", theme.ContentCopyIcon(), copyAction)
bar := container.NewBorder(nil, nil, choose, copyToClip, output)
w.SetContent(container.NewBorder(nil, bar, nil, nil,
container.NewStack(preview, container.NewCenter(highlight))))
hold := false
w.Canvas().AddShortcut(&fyne.ShortcutCopy{}, func(_ fyne.Shortcut) { copyAction() })
w.Canvas().AddShortcut(&desktop.CustomShortcut{KeyName: fyne.KeyH, Modifier: fyne.KeyModifierShortcutDefault},
func(_ fyne.Shortcut) {
hold = !hold
})
go func() {
halfSize := (pixCount - 1) / 2
for pix := range pollPixels(w) {
if hold {
continue
}
preview.Image = pix
preview.Refresh()
center := (pixCount*halfSize + halfSize) * 4
r, g, b := pix.Pix[center], pix.Pix[center+1], pix.Pix[center+2]
if choose.Selected == "rgb" {
output.SetText(fmt.Sprintf("rgb(%d, %d, %d)", r, g, b))
} else {
output.SetText(fmt.Sprintf("#%02x%02x%02x", r, g, b))
}
}
}()
w.ShowAndRun()
}
func pollPixels(w fyne.Window) <-chan *image.NRGBA {
ch := make(chan *image.NRGBA)
x11, err := xgbutil.NewConn()
if err != nil {
dialog.ShowError(err, w)
return nil
}
halfSize := (pixCount - 1) / 2
go func() {
t := time.NewTicker(time.Second / 10)
for range t.C {
r, _ := xproto.QueryPointer(x11.Conn(), x11.RootWin()).Reply()
g, _ := xproto.GetGeometry(x11.Conn(), xproto.Drawable(x11.RootWin())).Reply()
x, y := r.RootX-int16(halfSize), r.RootY-int16(halfSize)
if x < 0 {
x = 0
}
if y < 0 {
y = 0
}
if x >= int16(g.Width)-pixCount {
x = int16(g.Width) - pixCount
}
if y >= int16(g.Height)-pixCount {
y = int16(g.Height) - pixCount
}
pix, err := xproto.GetImage(x11.Conn(), xproto.ImageFormatZPixmap, xproto.Drawable(x11.RootWin()),
x, y, pixCount, pixCount, math.MaxUint32).Reply()
if err != nil {
fyne.LogError("failed to read pixel data", err)
continue
}
img := image.NewNRGBA(image.Rect(0, 0, pixCount, pixCount))
// b, g, r, a
for i := 0; i < 625; i++ {
img.Pix[i*4] = pix.Data[i*4+2]
img.Pix[i*4+1] = pix.Data[i*4+1]
img.Pix[i*4+2] = pix.Data[i*4]
img.Pix[i*4+3] = 0xff // no transparency on the overall screen
}
ch <- img
}
}()
return ch
}