-
Notifications
You must be signed in to change notification settings - Fork 129
/
tool.go
216 lines (198 loc) · 4.01 KB
/
tool.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
214
215
216
/**
* @Author Awen
* @Description Captcha Tool
* @Date 2021/7/18
* @Email wengaolng@gmail.com
**/
package captcha
import (
"crypto/rand"
"errors"
"image/color"
"math"
"math/big"
"os"
"path/filepath"
"regexp"
"strconv"
"strings"
"unicode"
"unicode/utf8"
)
/**
* @Description: 转16进制
* @param t
* @return string
*/
func t2x ( t int64 ) string {
result := strconv.FormatInt(t, 16)
if len(result) == 1{
result = "0" + result
}
return result
}
/**
* @Description: RBG颜色转十六进制颜色
* @param red
* @param green
* @param blue
* @return CaptchaColorHEX
*/
func RgbToHex(red int64, green int64, blue int64) string {
r := t2x(red)
g := t2x(green)
b := t2x(blue)
return r+g+b
}
/**
* @Description: 十六进制转RBG颜色
* @param hex
* @return int64
* @return int64
* @return int64
*/
func HexToRgb(hex string) (int64, int64, int64) {
r, _ := strconv.ParseInt(hex[:2], 16, 10)
g, _ := strconv.ParseInt(hex[2:4], 16, 18)
b, _ := strconv.ParseInt(hex[4:], 16, 10)
return r, g, b
}
/**
* @Description: 把十六进制颜色转 color.RGBA
* @param v
* @return out
* @return err
*/
func ParseHexColor(s string) (c color.RGBA, err error) {
c.A = 0xff
if s[0] != '#' {
return c, errors.New("hex color must start with '#'")
}
hexToByte := func(b byte) byte {
switch {
case b >= '0' && b <= '9':
return b - '0'
case b >= 'a' && b <= 'f':
return b - 'a' + 10
case b >= 'A' && b <= 'F':
return b - 'A' + 10
}
err = errors.New("hexToByte component invalid")
return 0
}
switch len(s) {
case 7:
c.R = hexToByte(s[1])<<4 + hexToByte(s[2])
c.G = hexToByte(s[3])<<4 + hexToByte(s[4])
c.B = hexToByte(s[5])<<4 + hexToByte(s[6])
case 4:
c.R = hexToByte(s[1]) * 17
c.G = hexToByte(s[2]) * 17
c.B = hexToByte(s[3]) * 17
default:
err = errors.New("hexToByte component invalid")
}
return
}
/**
* @Description: 检测文件是否存在
* @param path
* @return bool
* @return error
*/
func PathExists(path string) (bool, error) {
_, err := os.Stat(path)
if err == nil {
return true, nil
}
if os.IsNotExist(err) {
return false, nil
}
return false, err
}
/**
* @Description: 获取路径
* @param path
* @return []string
*/
func Child(path string) []string {
fullPath, _ := filepath.Abs(path)
listStr := make([]string, 0)
_ = filepath.Walk(fullPath, func(path string, fi os.FileInfo, err error) error {
if nil == fi {
return err
}
if fi.IsDir() {
return nil
}
if strings.HasSuffix(path, ".ttf") {
listStr = append(listStr, path)
}
return err
})
return listStr
}
/**
* @Description: InArrayS 如果 s 在 items 中,返回 true;否则,返回 false。
* @param items
* @param s
* @return bool
*/
func InArrayWithStr(items []string, s string) bool {
for _, item := range items {
if item == s {
return true
}
}
return false
}
/**
* @Description: 生成区间[-m, n]的安全随机数
* @param min
* @param max
* @return int
*/
func RandInt(min, max int) (result int) {
if min > max {
return max
}
if min < 0 {
f64Min := math.Abs(float64(min))
i64Min := int(f64Min)
result, _ := rand.Int(rand.Reader, big.NewInt(int64(max + 1 + i64Min)))
return int(result.Int64() - int64(i64Min))
} else {
result, _ := rand.Int(rand.Reader, big.NewInt(int64(max - min+1)))
return int(int64(min) + result.Int64())
}
}
/**
* @Description: 随机浮点数
* @param min
* @param max
* @return float64
*/
func RandFloat(min, max int) float64 {
return float64(RandInt(min, max))
}
/**
* @Description: 判断是否是中文
* @param str
* @return bool
*/
func IsChineseChar(str string) bool {
for _, r := range str {
if unicode.Is(unicode.Scripts["Han"], r) || (regexp.MustCompile("[\u3002\uff1b\uff0c\uff1a\u201c\u201d\uff08\uff09\u3001\uff1f\u300a\u300b]").MatchString(string(r))) {
return true
}
}
return false
}
/**
* @Description: 计算中文及字母长度,例如:“你好hello” = 7
* @param str
* @return int
*/
func LenChineseChar(str string) int {
return utf8.RuneCountInString(str)
}