forked from ae0000/avatar
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbackgrounds.go
73 lines (57 loc) · 1.66 KB
/
backgrounds.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
package avatar
import (
"image"
"image/draw"
"github.com/anthonynsimon/bild/adjust"
"github.com/anthonynsimon/bild/blend"
"github.com/anthonynsimon/bild/effect"
"github.com/anthonynsimon/bild/noise"
)
type BGMethod int
const (
Fast BGMethod = iota
LinenLike
Drops
)
var defaultBGType = Fast
func SetDefaultBGType(bgtype BGMethod) {
defaultBGType = bgtype
}
func GetDefaultBG(bg image.Uniform) *image.RGBA {
return GetBG(bg, defaultBGType)
}
func GetBG(bg image.Uniform, method BGMethod) *image.RGBA {
switch method {
case Fast:
return FastBG(bg)
case LinenLike:
return LinenLikeBG(bg)
case Drops:
return DropsBG(bg)
default:
return FastBG(bg)
}
}
func FastBG(bg image.Uniform) *image.RGBA {
bgc := image.NewRGBA(image.Rect(0, 0, imageWidth, imageHeight))
draw.Draw(bgc, bgc.Bounds(), &bg, image.Point{}, draw.Src)
return bgc
}
func DropsBG(bg image.Uniform) *image.RGBA {
bgc := image.NewRGBA(image.Rect(0, 0, imageWidth, imageHeight))
draw.Draw(bgc, bgc.Bounds(), &bg, image.Point{}, draw.Src)
bgi := noise.Generate(imageWidth, imageHeight, &noise.Options{Monochrome: true, NoiseFn: noise.Gaussian})
bgi = effect.Dilate(bgi, 6)
bgi = adjust.Contrast(bgi, -0.3)
bgi = adjust.Brightness(bgi, 0.1)
return blend.Multiply(bgc, bgi)
}
func LinenLikeBG(bg image.Uniform) *image.RGBA {
bgc := image.NewRGBA(image.Rect(0, 0, imageWidth, imageHeight))
draw.Draw(bgc, bgc.Bounds(), &bg, image.Point{}, draw.Src)
bgi := noise.Generate(imageWidth, imageHeight, &noise.Options{Monochrome: true, NoiseFn: noise.Binary})
bgi = effect.Median(bgi, 10.0)
bgi = adjust.Contrast(bgi, -0.95)
bgi = adjust.Brightness(bgi, 0.5)
return blend.Multiply(bgc, bgi)
}