-
Notifications
You must be signed in to change notification settings - Fork 61
/
utils.go
79 lines (62 loc) · 1.52 KB
/
utils.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
package gocaptcha
import (
"image/color"
"math"
"math/rand"
"time"
)
//随机生成深色系.
func randDeepColor() color.RGBA {
randColor := randColor()
increase := float64(30 + r.Intn(255))
red := math.Abs(math.Min(float64(randColor.R)-increase, 255))
green := math.Abs(math.Min(float64(randColor.G)-increase, 255))
blue := math.Abs(math.Min(float64(randColor.B)-increase, 255))
return color.RGBA{R: uint8(red), G: uint8(green), B: uint8(blue), A: uint8(255)}
}
//随机生成浅色.
func RandLightColor() color.RGBA {
red := r.Intn(55) + 200
green := r.Intn(55) + 200
blue := r.Intn(55) + 200
return color.RGBA{R: uint8(red), G: uint8(green), B: uint8(blue), A: uint8(255)}
}
//生成随机颜色.
func randColor() color.RGBA {
red := r.Intn(255)
green := r.Intn(255)
blue := r.Intn(255)
if (red + green) > 400 {
blue = 0
} else {
blue = 400 - green - red
}
if blue > 255 {
blue = 255
}
return color.RGBA{R: uint8(red), G: uint8(green), B: uint8(blue), A: uint8(255)}
}
//生成随机字体.
func RandText(num int) string {
textNum := len(txtChars)
text := ""
r := rand.New(rand.NewSource(time.Now().UnixNano()))
for i := 0; i < num; i++ {
text = text + string(txtChars[r.Intn(textNum)])
}
return text
}
// 颜色代码转换为RGB
//input int
//output int red, green, blue.
func ColorToRGB(colorVal int) color.RGBA {
red := colorVal >> 16
green := (colorVal & 0x00FF00) >> 8
blue := colorVal & 0x0000FF
return color.RGBA{
R: uint8(red),
G: uint8(green),
B: uint8(blue),
A: uint8(255),
}
}