-
Notifications
You must be signed in to change notification settings - Fork 1
/
effects.go
65 lines (61 loc) · 1.04 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
package gg
func limitper(per int) int {
if per > 100 {
per = 100
}
if per < -100 {
per = -100
}
return per
}
func limit2uint8(n int) int {
if n < 0 {
n = 0
}
if n > 255 {
n = 255
}
return n
}
// Brightness 调整亮度 范围:±100%
func (dc *Context) Brightness(per int) {
if per == 0 {
return
}
per = limitper(per)
gain := 255 * per / 100
for i, v := range dc.im.Pix {
if i%4 == 3 { // alpha
continue
}
dc.im.Pix[i] = uint8(limit2uint8(int(v) + gain))
}
}
// Contrast 调整对比度 范围:±100%
func (dc *Context) Contrast(per int) {
if per == 0 {
return
}
per = limitper(per) + 100
gain := 0
switch {
case 0 <= per && per <= 100: // 损益
gain = per
for i, v := range dc.im.Pix {
if i%4 == 3 { // alpha
continue
}
dc.im.Pix[i] = uint8(limit2uint8(int(v) * gain / 100))
}
case 1 < per && per < 2: // 增益
gain = 200 - per
for i, v := range dc.im.Pix {
if i%4 == 3 { // alpha
continue
}
dc.im.Pix[i] = uint8(limit2uint8(int(v) * 100 / gain))
}
default:
return
}
}