-
Notifications
You must be signed in to change notification settings - Fork 6
/
m_fade.go
54 lines (47 loc) · 1.22 KB
/
m_fade.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
package main
import (
"image"
"image/color"
"image/draw"
"fyne.io/fyne/v2/canvas"
)
type cropMask struct {
bounds image.Rectangle
crop image.Rectangle
}
func (c *cropMask) ColorModel() color.Model {
return color.AlphaModel
}
func (c *cropMask) Bounds() image.Rectangle {
return c.bounds
}
func (c *cropMask) At(x, y int) color.Color {
if p := image.Pt(x, y); p.In(c.crop) {
return color.Alpha{255}
}
fade := uint8(256 * (1 - DefaultTranslucency))
return color.Alpha{fade}
}
// fade photo image outside crop bounds
func (p *Photo) fadeByCrop(m *canvas.Image) {
src := m.Image
dst := image.NewNRGBA(src.Bounds())
if !p.isCropped() { // TODO maybe simply return src?
draw.Draw(dst, dst.Bounds(), src, image.Point{}, draw.Src)
m.Image = dst
return
}
factor := float32(p.height) / float32(src.Bounds().Dy())
crop := image.Rectangle{
Min: image.Point{
X: int(float32(p.CropRectangle.Min.X) / factor),
Y: int(float32(p.CropRectangle.Min.Y) / factor),
},
Max: image.Point{
X: int(float32(p.CropRectangle.Max.X) / factor),
Y: int(float32(p.CropRectangle.Max.Y) / factor),
},
}
draw.DrawMask(dst, dst.Bounds(), src, image.Point{}, &cropMask{src.Bounds(), crop}, image.Point{}, draw.Over)
m.Image = dst
}