-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpalette.go
52 lines (46 loc) · 1.13 KB
/
palette.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
package captcha
import (
"image"
"image/color"
"math"
)
type Palette struct {
*image.Paletted
}
func NewPalette(r image.Rectangle, p color.Palette) *Palette {
return &Palette{
image.NewPaletted(r, p),
}
}
//Rotate 旋转角度
func (p *Palette) Rotate(angle int) {
width := p.Bounds().Max.X
height := p.Bounds().Max.Y
r := width / 2
retImg := image.NewPaletted(image.Rect(0, 0, width, height), p.Palette)
for x := 0; x <= retImg.Bounds().Max.X; x++ {
for y := 0; y <= retImg.Bounds().Max.Y; y++ {
tx, ty := p.angleSwapPoint(float64(x), float64(y), float64(r), float64(angle))
retImg.SetColorIndex(x, y, p.ColorIndexAt(int(tx), int(ty)))
}
}
nW := retImg.Bounds().Max.X
nH := retImg.Bounds().Max.Y
for x := 0; x < nW; x++ {
for y := 0; y < nH; y++ {
p.SetColorIndex(x, y, retImg.ColorIndexAt(x, y))
}
}
}
//angleSwapPoint 坐标转换
func (p *Palette) angleSwapPoint(x, y, r, angle float64) (tarX, tarY float64) {
x -= r
y = r - y
sinVal := math.Sin(angle * (math.Pi / 180))
cosVal := math.Cos(angle * (math.Pi / 180))
tarX = x*cosVal + y*sinVal
tarY = -x*sinVal + y*cosVal
tarX += r
tarY = r - tarY
return
}