-
Notifications
You must be signed in to change notification settings - Fork 129
/
palette.go
215 lines (199 loc) · 4.27 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
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
/**
* @Author Awen
* @Description Captcha Palette
* @Date 2021/7/18
* @Email wengaolng@gmail.com
**/
package captcha
import (
"image"
"image/color"
"math"
)
/**
* @Description: 点
*/
type Point struct {
X int
Y int
}
/**
* @Description: 调色板
*/
type Palette struct{
*image.Paletted
}
/**
* @Description: 创建调色板
* @param r
* @param p
* @return *Palette
*/
func NewPalette(r image.Rectangle, p color.Palette) *Palette {
return &Palette{
image.NewPaletted(r, p),
}
}
/**
* @Description: 画圆点
* @receiver p
* @param x
* @param y
* @param radius
* @param colorIdx
*/
func (p *Palette)drawCircle(x, y, radius int, colorIdx uint8) {
f := 1 - radius
dfx := 1
dfy := -2 * radius
xo := 0
yo := radius
p.SetColorIndex(x, y+radius, colorIdx)
p.SetColorIndex(x, y-radius, colorIdx)
p.drawHorizLine(x-radius, x+radius, y, colorIdx)
for xo < yo {
if f >= 0 {
yo--
dfy += 2
f += dfy
}
xo++
dfx += 2
f += dfx
p.drawHorizLine(x-xo, x+xo, y+yo, colorIdx)
p.drawHorizLine(x-xo, x+xo, y-yo, colorIdx)
p.drawHorizLine(x-yo, x+yo, y+xo, colorIdx)
p.drawHorizLine(x-yo, x+yo, y-xo, colorIdx)
}
}
/**
* @Description: 画线
* @receiver p
* @param fromX
* @param toX
* @param y
* @param colorIdx
*/
func (p *Palette)drawHorizLine(fromX, toX, y int, colorIdx uint8) {
for x := fromX; x <= toX; x++ {
p.SetColorIndex(x, y, colorIdx)
}
}
/**
* @Description: 扭曲
* @receiver p
* @param amplude
* @param period
*/
func (p *Palette)distort(amplude float64, period float64) {
w := p.Bounds().Max.X
h := p.Bounds().Max.Y
newP := NewPalette(image.Rect(0, 0, w, h), p.Palette)
dx := 2.0 * math.Pi / period
for x := 0; x < w; x++ {
for y := 0; y < h; y++ {
xo := amplude * math.Sin(float64(y)*dx)
yo := amplude * math.Cos(float64(x)*dx)
newP.SetColorIndex(x, y, p.ColorIndexAt(x + int(xo), y + int(yo)))
}
}
nW := newP.Bounds().Max.X
nH := newP.Bounds().Max.Y
for x := 0; x < nW; x++ {
for y := 0; y < nH; y++ {
p.SetColorIndex(x, y, newP.ColorIndexAt(x, y))
}
}
}
/**
* @Description: 画点到点直线
* @receiver p
* @param point1
* @param point2
* @param lineColor
*/
func (p *Palette)drawBeeline(point1 Point, point2 Point, lineColor color.RGBA) {
dx := math.Abs(float64(point1.X - point2.X))
dy := math.Abs(float64(point2.Y - point1.Y))
sx, sy := 1, 1
if point1.X >= point2.X {
sx = -1
}
if point1.Y >= point2.Y {
sy = -1
}
err := dx - dy
for {
p.Set(point1.X, point1.Y, lineColor)
p.Set(point1.X+1, point1.Y, lineColor)
p.Set(point1.X-1, point1.Y, lineColor)
p.Set(point1.X+2, point1.Y, lineColor)
p.Set(point1.X-2, point1.Y, lineColor)
if point1.X == point2.X && point1.Y == point2.Y {
return
}
e2 := err * 2
if e2 > -dy {
err -= dy
point1.X += sx
}
if e2 < dx {
err += dx
point1.Y += sy
}
}
}
/**
* @Description: 角度转换点坐标
* @receiver p
* @param x
* @param y
* @param r
* @param angle
* @return tarX
* @return tarY
*/
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
}
/**
* @Description: 旋转任意角度
* @receiver p
* @param angle
*/
func (p *Palette)Rotate(angle int) {
tarImg := p
width := tarImg.Bounds().Max.X
height := tarImg.Bounds().Max.Y
r := width / 2
//trX, trY := tarImg.Bounds().Max.X / 2, tarImg.Bounds().Max.Y / 2
retImg := image.NewPaletted(image.Rect(0, 0, width, height), tarImg.Palette)
for x := 0; x <= retImg.Bounds().Max.X; x++ {
for y := 0; y <= retImg.Bounds().Max.Y; y++ {
//if (x - trX)*(x - trX) + (y - trY)*(y - trY) <= r*r {
// tx, ty := p.angleSwapPoint(float64(x), float64(y), float64(r), float64(angle))
// retImg.SetColorIndex(x, y, tarImg.ColorIndexAt(int(tx), int(ty)))
//}
tx, ty := p.angleSwapPoint(float64(x), float64(y), float64(r), float64(angle))
at := tarImg.ColorIndexAt(int(tx), int(ty))
if at > 0 {
retImg.SetColorIndex(x, y, tarImg.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))
}
}
}