-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patheffects.go
102 lines (91 loc) · 1.91 KB
/
effects.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
package imgfactory
import (
"image/color"
"github.com/disintegration/imaging"
)
// float64转uint8
func floatUint8(a float64) uint8 {
b := int64(a + 0.5)
if b > 255 {
return 255
}
if b > 0 {
return uint8(b)
}
return 0
}
// AdjustBrightness 亮度(-100, 100)
func (dst *Factory) AdjustBrightness(s float64) *Factory {
return &Factory{
im: imaging.AdjustBrightness(dst.im, s),
}
}
// AdjustContrast 对比度(-100, 100)
func (dst *Factory) AdjustContrast(a float64) *Factory {
return &Factory{
im: imaging.AdjustContrast(dst.im, a),
}
}
// AdjustSaturation 饱和度(-100, 100)
func (dst *Factory) AdjustSaturation(a float64) *Factory {
return &Factory{
im: imaging.AdjustSaturation(dst.im, a),
}
}
// Sharpen 锐化
func (dst *Factory) Sharpen(a float64) *Factory {
return &Factory{
im: imaging.Sharpen(dst.im, a),
}
}
// Blur 模糊图像 正数
func (dst *Factory) Blur(a float64) *Factory {
return &Factory{
im: imaging.Blur(dst.im, a),
}
}
// Grayscale 灰度
func (dst *Factory) Grayscale() *Factory {
b := dst.im.Bounds()
for y1 := b.Min.Y; y1 <= b.Max.Y; y1++ {
for x1 := b.Min.X; x1 <= b.Max.X; x1++ {
a := dst.im.At(x1, y1)
c := color.NRGBAModel.Convert(a).(color.NRGBA)
f := 0.299*float64(c.R) + 0.587*float64(c.G) + 0.114*float64(c.B)
c.R = floatUint8(f)
c.G = floatUint8(f)
c.B = floatUint8(f)
dst.im.Set(x1, y1, c)
}
}
return dst
}
// Invert 反色
func (dst *Factory) Invert() *Factory {
b := dst.im.Bounds()
for y1 := b.Min.Y; y1 <= b.Max.Y; y1++ {
for x1 := b.Min.X; x1 <= b.Max.X; x1++ {
a := dst.im.At(x1, y1)
c := color.NRGBAModel.Convert(a).(color.NRGBA)
c.R = 255 - c.R
c.G = 255 - c.G
c.B = 255 - c.B
dst.im.Set(x1, y1, c)
}
}
return dst
}
// Relief 浮雕
func (dst *Factory) Relief() *Factory {
return &Factory{
im: imaging.Convolve3x3(
dst.im,
[9]float64{
-1, -1, 0,
-1, 1, 1,
0, 1, 1,
},
nil,
),
}
}