|
1 | 1 | package gg |
2 | 2 |
|
3 | | -// Brightness 调整亮度 范围:±100% |
4 | | -func (dc *Context) Brightness(per int) { |
5 | | - if per == 0 { |
| 3 | +import ( |
| 4 | + "image" |
| 5 | + "math" |
| 6 | + |
| 7 | + "github.com/disintegration/imaging" |
| 8 | +) |
| 9 | + |
| 10 | +// AdjustBrightness 调整亮度 范围:±100% |
| 11 | +func (dc *Context) AdjustBrightness(s float64) { |
| 12 | + if math.Abs(s) < 0.001 { |
| 13 | + return |
| 14 | + } |
| 15 | + dc.im = (*image.RGBA)(imaging.AdjustBrightness(dc.im, s)) |
| 16 | +} |
| 17 | + |
| 18 | +// AdjustContrast 调整对比度 范围:±100% |
| 19 | +func (dc *Context) AdjustContrast(s float64) { |
| 20 | + if math.Abs(s) < 0.001 { |
6 | 21 | return |
7 | 22 | } |
8 | | - per = clamp(per, -100, 100) |
9 | | - gain := 255 * per / 100 |
10 | | - for i, v := range dc.im.Pix { |
11 | | - if i%4 == 3 { // alpha |
12 | | - continue |
13 | | - } |
14 | | - dc.im.Pix[i] = uint8(clamp(int(v)+gain, 0, 255)) |
| 23 | + dc.im = (*image.RGBA)(imaging.AdjustContrast(dc.im, s)) |
| 24 | +} |
| 25 | + |
| 26 | +// AdjustContrast 调整饱和度 范围:±100% |
| 27 | +func (dc *Context) AdjustSaturation(s float64) { |
| 28 | + if math.Abs(s) < 0.001 { |
| 29 | + return |
15 | 30 | } |
| 31 | + dc.im = (*image.RGBA)(imaging.AdjustSaturation(dc.im, s)) |
16 | 32 | } |
17 | 33 |
|
18 | | -// Contrast 调整对比度 范围:±100% |
19 | | -func (dc *Context) Contrast(per int) { |
20 | | - if per == 0 { |
| 34 | +// Sharpen 锐化 范围:±100% |
| 35 | +func (dc *Context) Sharpen(s float64) { |
| 36 | + if math.Abs(s) < 0.001 { |
21 | 37 | return |
22 | 38 | } |
23 | | - per = clamp(per, -100, 100) + 100 |
24 | | - switch { |
25 | | - case 0 <= per && per < 100: // 损益 |
26 | | - gain := per |
27 | | - for i, v := range dc.im.Pix { |
28 | | - if i%4 == 3 { // alpha |
29 | | - continue |
30 | | - } |
31 | | - dc.im.Pix[i] = uint8(clamp(int(v)*gain/100, 0, 255)) |
32 | | - } |
33 | | - case 100 < per && per <= 200: // 增益 |
34 | | - gain := 200 - per |
35 | | - if gain == 0 { |
36 | | - gain = 1 |
37 | | - } |
38 | | - for i, v := range dc.im.Pix { |
39 | | - if i%4 == 3 { // alpha |
40 | | - continue |
41 | | - } |
42 | | - dc.im.Pix[i] = uint8(clamp(int(v)*100/gain, 0, 255)) |
43 | | - } |
44 | | - default: |
45 | | - panic("unreachable") |
| 39 | + dc.im = (*image.RGBA)(imaging.Sharpen(dc.im, s)) |
| 40 | +} |
| 41 | + |
| 42 | +// Blur 模糊图像 正数 |
| 43 | +func (dc *Context) Blur(s float64) { |
| 44 | + if math.Abs(s) < 0.001 { |
| 45 | + return |
46 | 46 | } |
| 47 | + dc.im = (*image.RGBA)(imaging.Blur(dc.im, s)) |
47 | 48 | } |
0 commit comments