Skip to content

Commit

Permalink
update code
Browse files Browse the repository at this point in the history
  • Loading branch information
wenlng committed Jun 7, 2024
1 parent cafca88 commit 8b0c826
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 36 deletions.
24 changes: 12 additions & 12 deletions v2/base/canvas/palette.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ type Palette interface {
Rotate(angle int)
Distort(amplude float64, period float64)
DrawBeeline(point1 image.Point, point2 image.Point, lineColor color.RGBA)
DrawCircle(x, y, radius int, colorIdx uint8)
DrawHorizLine(fromX, toX, y int, colorIdx uint8)
DrawCircle(x, y, radius int, c color.RGBA)
DrawHorizLine(fromX, toX, y int, c color.RGBA)
AngleSwapPoint(x, y, r, angle float64) (tarX, tarY float64)
DrawString(params *DrawStringParams, pt fixed.Point26_6) error
}
Expand Down Expand Up @@ -78,16 +78,16 @@ func (p *palette) Rotate(angle int) {
}

// DrawCircle is drawing circle
func (p *palette) DrawCircle(x, y, radius int, colorIdx uint8) {
func (p *palette) DrawCircle(x, y, radius int, c color.RGBA) {
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)
p.Set(x, y+radius, c)
p.Set(x, y-radius, c)
p.DrawHorizLine(x-radius, x+radius, y, c)

for xo < yo {
if f >= 0 {
Expand All @@ -98,17 +98,17 @@ func (p *palette) DrawCircle(x, y, radius int, colorIdx uint8) {
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)
p.DrawHorizLine(x-xo, x+xo, y+yo, c)
p.DrawHorizLine(x-xo, x+xo, y-yo, c)
p.DrawHorizLine(x-yo, x+yo, y+xo, c)
p.DrawHorizLine(x-yo, x+yo, y-xo, c)
}
}

// DrawHorizLine is drawing horiz line
func (p *palette) DrawHorizLine(fromX, toX, y int, colorIdx uint8) {
func (p *palette) DrawHorizLine(fromX, toX, y int, c color.RGBA) {
for x := fromX; x <= toX; x++ {
p.SetColorIndex(x, y, colorIdx)
p.Set(x, y, c)
}
}

Expand Down
1 change: 1 addition & 0 deletions v2/click/click.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ func (c *captcha) genThumbImage(size *option.Size, dots map[int]*Dot) (image.Ima
BackgroundDistort: c.randDistortWithLevel(c.opts.thumbBgDistort),
BackgroundCirclesNum: c.opts.thumbBgCirclesNum,
BackgroundSlimLineNum: c.opts.thumbBgSlimLineNum,
ThumbDisturbAlpha: c.opts.thumbDisturbAlpha,
}

if len(c.resources.rangThumbBackgrounds) > 0 {
Expand Down
15 changes: 8 additions & 7 deletions v2/click/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ var colors = []string{
}

var thumbColors = []string{
"#006600",
"#005db9",
"#aa002a",
"#875400",
"#6e3700",
"#660033",
"#1f62ee",
"#4e87ff",
"#9f0bd9",
"#db1042",
"#b15c07",
"#b7055e",
}

var shadowColor = "#101010"
Expand Down Expand Up @@ -73,7 +73,7 @@ func defaultOptions() Option {
opts.displayShadow = true
opts.shadowColor = getDefaultShadowColor()
opts.shadowPoint = &option.Point{X: -1, Y: -1}
opts.imageSize = &option.Size{Width: 300, Height: 240}
opts.imageSize = &option.Size{Width: 300, Height: 220}
opts.imageAlpha = 1

opts.rangeVerifyLen = &option.RangeVal{Min: 2, Max: 4}
Expand All @@ -85,6 +85,7 @@ func defaultOptions() Option {
opts.thumbBgCirclesNum = 24
opts.thumbBgSlimLineNum = 2
opts.isThumbNonDeformAbility = true
opts.thumbDisturbAlpha = 1
}
}

Expand Down
37 changes: 27 additions & 10 deletions v2/click/draw.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type DrawImageParams struct {
ShowShadow bool
ShadowColor string
ShadowPoint *option.Point
ThumbDisturbAlpha float32
}

type DrawImage interface {
Expand Down Expand Up @@ -107,14 +108,21 @@ func (d *drawImage) DrawWithPalette(params *DrawImageParams, tColors []color.Col
color.RGBA{R: 0xFF, G: 0xFF, B: 0xFF, A: 0x00},
}
p = append(p, tColors...)
p = append(p, bgColors...)

nBgColors := make([]color.Color, 0, len(bgColors))
for _, bgColor := range bgColors {
r, g, b, _ := bgColor.RGBA()
aa := helper.FormatAlpha(params.ThumbDisturbAlpha)
nBgColors = append(nBgColors, color.RGBA{R: uint8(r), G: uint8(g), B: uint8(b), A: aa})
}
p = append(p, nBgColors...)

cvs := canvas.NewPalette(image.Rect(0, 0, params.Width, params.Height), p)
if params.BackgroundCirclesNum > 0 {
d.randomFillWithCircles(cvs, params.BackgroundCirclesNum, 1, 2)
d.randomFillWithCircles(cvs, params.BackgroundCirclesNum, 1, nBgColors)
}
if params.BackgroundSlimLineNum > 0 {
d.randomDrawSlimLine(cvs, params.BackgroundSlimLineNum, bgColors)
d.randomDrawSlimLine(cvs, params.BackgroundSlimLineNum, nBgColors)
}

for i := 0; i < len(dots); i++ {
Expand Down Expand Up @@ -174,11 +182,19 @@ func (d *drawImage) DrawWithPalette(params *DrawImageParams, tColors []color.Col
// DrawWithNRGBA2 is drawing with a NRGBA
func (d *drawImage) DrawWithNRGBA2(params *DrawImageParams, tColors []color.Color, bgColors []color.Color) (image.Image, error) {
dots := params.CaptchaDrawDot

p := []color.Color{
color.RGBA{R: 0xFF, G: 0xFF, B: 0xFF, A: 0x00},
}
p = append(p, tColors...)
p = append(p, bgColors...)

nBgColors := make([]color.Color, 0, len(bgColors))
for _, bgColor := range bgColors {
r, g, b, _ := bgColor.RGBA()
aa := helper.FormatAlpha(params.ThumbDisturbAlpha)
nBgColors = append(nBgColors, color.RGBA{R: uint8(r), G: uint8(g), B: uint8(b), A: aa})
}
p = append(p, nBgColors...)

ccvs := canvas.NewNRGBA(image.Rect(0, 0, params.Width, params.Height), true)
if params.Background != nil {
Expand All @@ -193,10 +209,10 @@ func (d *drawImage) DrawWithNRGBA2(params *DrawImageParams, tColors []color.Colo

cvs := canvas.NewPalette(image.Rect(0, 0, params.Width, params.Height), p)
if params.BackgroundCirclesNum > 0 {
d.randomFillWithCircles(cvs, params.BackgroundCirclesNum, 1, 2)
d.randomFillWithCircles(cvs, params.BackgroundCirclesNum, 1, nBgColors)
}
if params.BackgroundSlimLineNum > 0 {
d.randomDrawSlimLine(cvs, params.BackgroundSlimLineNum, bgColors)
d.randomDrawSlimLine(cvs, params.BackgroundSlimLineNum, nBgColors)
}
if params.BackgroundDistort > 0 {
cvs.Distort(float64(random.RandInt(5, 10)), float64(params.BackgroundDistort))
Expand Down Expand Up @@ -255,13 +271,14 @@ func (d *drawImage) DrawWithNRGBA2(params *DrawImageParams, tColors []color.Colo
}

// randomFillWithCircles is to draw circle randomly
func (d *drawImage) randomFillWithCircles(m canvas.Palette, n, maxRadius int, circleCount int) {
func (d *drawImage) randomFillWithCircles(m canvas.Palette, n, maxRadius int, colorB []color.Color) {
maxx := m.Bounds().Max.X
maxy := m.Bounds().Max.Y
for i := 0; i < n; i++ {
colorIdx := uint8(random.RandInt(1, circleCount-1))
co := randgen.RandColor(colorB)
//co.A = uint8(0xee)
r := random.RandInt(1, maxRadius)
m.DrawCircle(random.RandInt(r, maxx-r), random.RandInt(r, maxy-r), r, colorIdx)
m.DrawCircle(random.RandInt(r, maxx-r), random.RandInt(r, maxy-r), r, co)
}
}

Expand All @@ -283,7 +300,7 @@ func (d *drawImage) randomDrawSlimLine(m canvas.Palette, num int, colorB []color
}

co := randgen.RandColor(colorB)
co.A = uint8(0xee)
//co.A = uint8(0xee)
m.DrawBeeline(point1, point2, co)
}
}
Expand Down
16 changes: 16 additions & 0 deletions v2/click/option.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ type Options struct {
thumbBgCirclesNum int
thumbBgSlimLineNum int
isThumbNonDeformAbility bool
thumbDisturbAlpha float32

useShapeOriginalColor bool
}
Expand Down Expand Up @@ -169,6 +170,11 @@ func (o *Options) GetIsThumbNonDeformAbility() bool {
return o.isThumbNonDeformAbility
}

// GetThumbDisturbAlpha .
func (o *Options) GetThumbDisturbAlpha() float32 {
return o.thumbDisturbAlpha
}

type Option func(*Options)

// NewOptions .
Expand Down Expand Up @@ -256,6 +262,9 @@ func WithShadowPoint(val option.Point) Option {
// WithImageAlpha .
func WithImageAlpha(val float32) Option {
return func(opts *Options) {
if val > 1 {
val = 1
}
opts.imageAlpha = val
}
}
Expand Down Expand Up @@ -351,3 +360,10 @@ func WithIsThumbNonDeformAbility(val bool) Option {
opts.isThumbNonDeformAbility = val
}
}

// WithThumbDisturbAlpha .
func WithThumbDisturbAlpha(val float32) Option {
return func(opts *Options) {
opts.thumbDisturbAlpha = val
}
}
8 changes: 3 additions & 5 deletions v2/rotate/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,13 @@ import (
// defaultOptions is the default configuration
func defaultOptions() Option {
return func(opts *Options) {
opts.imageSquareSize = 240
opts.imageSquareSize = 220
opts.rangeAnglePos = []*option.RangeVal{
{Min: 290, Max: 305},
{Min: 305, Max: 325},
{Min: 325, Max: 330},
{Min: 30, Max: 330},
}

opts.thumbImageAlpha = 1
opts.rangeThumbImageSquareSize = []int{150, 160, 170, 180}
opts.rangeThumbImageSquareSize = []int{140, 150, 160, 170}
}
}

Expand Down
4 changes: 2 additions & 2 deletions v2/slide/default.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import (
// defaultOptions is the default configuration
func defaultOptions() Option {
return func(opts *Options) {
opts.imageSize = &option.Size{Width: 300, Height: 240}
opts.imageSize = &option.Size{Width: 300, Height: 220}
opts.imageAlpha = 1
opts.rangeDeadZoneDirections = []DeadZoneDirectionType{
DeadZoneDirectionTypeLeft,
Expand All @@ -27,7 +27,7 @@ func defaultOptions() Option {
opts.rangeGraphAnglePos = []*option.RangeVal{
{Min: 0, Max: 0},
}
opts.rangeGraphSize = &option.RangeVal{Min: 62, Max: 72}
opts.rangeGraphSize = &option.RangeVal{Min: 60, Max: 70}
}
}

Expand Down

0 comments on commit 8b0c826

Please sign in to comment.