commit 6919280a55dfe8341ec294c69e4926f7466e7a62
parent 4583937d7979e43a619f56ced50692ed865fcc6b
Author: n0tr1v <n0tr1v@protonmail.com>
Date: Sun, 28 May 2023 00:29:01 -0700
cleanup
Diffstat:
1 file changed, 19 insertions(+), 18 deletions(-)
diff --git a/pkg/captcha/image.go b/pkg/captcha/image.go
@@ -153,6 +153,16 @@ func angle(p1, p2 iPoint) float64 {
return math.Atan2(p1.GetY()-p2.GetY(), p1.GetX()-p2.GetX())
}
+type Points []Point
+
+func (p Points) Pairs(clb func(i int, p1, p2 Point)) {
+ for i := 1; i < len(p); i++ {
+ p1 := p[i-1]
+ p2 := p[i]
+ clb(i, p1, p2)
+ }
+}
+
type Point struct {
X int
Y int
@@ -664,7 +674,7 @@ func (m *Image) renderPath(points []Point) {
})
}
-func (m *Image) renderFakePath(points []Point) {
+func (m *Image) renderFakePath(points Points) {
if m.renderHelpImg {
return
}
@@ -675,33 +685,27 @@ func (m *Image) renderFakePath(points []Point) {
// Draw the whole line
m.withState(func() {
m.c.SetLineWidth(1)
- for i := 1; i < len(points); i++ {
- prev := points[i-1]
- pt := points[i]
+ points.Pairs(func(i int, prev, pt Point) {
grad := getGradient(prev, pt, gradColors, i)
m.c.SetStrokeStyle(grad)
m.strokeCubicLine(prev, pt, -d)
- }
+ })
})
// Semi transparent black line on top of the line
m.withState(func() {
m.c.SetLineWidth(4)
m.c.SetColor(color.RGBA{R: 15, G: 15, B: 15, A: 100})
- for i := 1; i < len(points); i++ {
- prev := points[i-1]
- pt := points[i]
+ points.Pairs(func(i int, prev, pt Point) {
m.strokeCubicLine(prev, pt, -d)
m.strokeCubicLine(pt, prev, d)
- }
+ })
})
// Draw the whole line again with dashes
m.withState(func() {
m.c.SetDash(5, 3)
- for i := 1; i < len(points); i++ {
- prev := points[i-1]
- pt := points[i]
+ points.Pairs(func(i int, prev, pt Point) {
grad := getGradient(prev, pt, gradColors, i)
m.c.SetStrokeStyle(grad)
@@ -715,23 +719,20 @@ func (m *Image) renderFakePath(points []Point) {
m.c.SetLineWidth(1.5)
m.strokeCubicLine(prev, pt, -d)
})
- }
+ })
})
// Draw line edges with longer dashes
m.withState(func() {
m.c.SetDash(30, 200)
m.c.SetLineWidth(1.5)
- for i := 1; i < len(points); i++ {
- prev := points[i-1]
- pt := points[i]
-
+ points.Pairs(func(i int, prev, pt Point) {
grad := getGradient(prev, pt, gradColors, i)
m.c.SetStrokeStyle(grad)
m.strokeCubicLine(prev, pt, -d)
m.strokeCubicLine(pt, prev, d)
- }
+ })
})
}