-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage.go
148 lines (131 loc) · 3.63 KB
/
image.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
package lorem
import (
"fmt"
"regexp"
"strconv"
"strings"
"github.com/CiroLee/gear/gearstring"
)
const placeholder_image_url = "https://dummyimage.com"
const picsum_image_url = "https://picsum.photos"
const loremflicker_url = "https://loremflickr.com"
type Hsl = [3]float32
type Size struct {
Width uint
Height uint
}
type PlaceholderOption struct {
Width uint
Height uint
Text string
BgColor string
FontColor string
}
type PicsumOption struct {
Width uint
Height uint
Grayscale bool
Blur uint
}
type ClassifyOption struct {
Type string
Width uint
Height uint
Lock bool
}
func isDark(hsl Hsl) bool {
return hsl[2] < 0.5
}
func initSize(option Size) Size {
if option.Width > 0 && option.Height <= 0 {
option.Height = option.Width
} else if option.Width <= 0 && option.Height > 0 {
option.Width = option.Height
} else if option.Width <= 0 && option.Height <= 0 {
w, _ := randomInteger(320, 1024)
h, _ := randomInteger(320, 1024)
option.Width = uint(w)
option.Height = uint(h)
}
return Size{
Width: option.Width,
Height: option.Height,
}
}
func hexToHsl(hex string) ([3]float32, error) {
rgb, err := hexToRgb(hex)
if err != nil {
return rgb, err
}
return rgbToHsl(rgb), nil
}
// return a random placeholder image with pure color background with special struct
func Placeholder(option PlaceholderOption) (string, error) {
if option.BgColor != "" && option.FontColor != "" && (!isHex(option.BgColor) || !isHex(option.FontColor)) {
return "", fmt.Errorf("BgColor or FontColor is not hex format color")
}
if option.BgColor == "" {
option.BgColor = Hex(false)
}
var size Size = initSize(Size{Width: option.Width, Height: option.Height})
bgHsl, _ := hexToHsl(option.BgColor)
dark := isDark(bgHsl)
if option.FontColor == "" {
if dark {
option.FontColor = "#ffffff"
} else {
option.FontColor = "#000000"
}
}
imgUrl := gearstring.Contact(placeholder_image_url, "/", strconv.Itoa(int(size.Width)), "x", strconv.Itoa(int(size.Height)), "/", strings.TrimPrefix(option.BgColor, "#"))
if option.Text != "" {
return gearstring.Contact(imgUrl, "/", strings.TrimPrefix(option.FontColor, "#"), "&text=", option.Text), nil
}
return imgUrl, nil
}
// return a random placeholder image without params
func SimplePlaceholder() string {
img, _ := Placeholder(PlaceholderOption{Text: "image"})
return img
}
// return a random image from unSplash by special param struct
func Picsum(option PicsumOption) string {
var size = initSize(Size{option.Width, option.Height})
re := regexp.MustCompile(`\/?grayscale$`)
var blur string
if option.Blur >= 10 {
blur = "10"
} else {
blur = strconv.Itoa(int(option.Blur))
}
imgUrl := gearstring.Contact(picsum_image_url, "/", strconv.Itoa(int(size.Width)), "/", strconv.Itoa(int(size.Height)))
if option.Grayscale {
imgUrl += "?grayscale"
}
if option.Blur > 0 {
if re.MatchString(imgUrl) {
imgUrl += "&blur=" + blur
} else {
imgUrl += "?blur=" + blur
}
}
return imgUrl
}
// return a random image. simple use of Picsum without params
func SimplePicsum() string {
return Picsum(PicsumOption{})
}
// return a random image classified image
func Classify(option ClassifyOption) string {
var size = initSize(Size{option.Width, option.Height})
baseUrl := gearstring.Contact(loremflicker_url, "/", strconv.Itoa(int(size.Width)), "/", strconv.Itoa(int(size.Height)), "/", option.Type)
if option.Lock {
lockNum, _ := randomInteger(10000, 99999)
return baseUrl + "?lock=" + strconv.Itoa(lockNum)
}
return strings.TrimRight(baseUrl, "/")
}
// simple use of Classify without params
func SimpleClassify() string {
return Classify(ClassifyOption{})
}