/
imageutils.go
256 lines (204 loc) · 5.25 KB
/
imageutils.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
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package gummyimage
import (
"bytes"
"errors"
"fmt"
"image"
"image/color"
"image/gif"
"image/jpeg"
"image/png"
"io"
"io/ioutil"
"math"
"math/rand"
"os"
"strconv"
"time"
"code.google.com/p/freetype-go/freetype"
"code.google.com/p/freetype-go/freetype/truetype"
)
type Gummy struct {
Img *image.RGBA
Color *color.Color
Font *truetype.Font
}
// Color in HEX format: FAFAFA
// If hexColor = "" then random color
func NewDefaultGummy(w, h int, hexColor string) (*Gummy, error) {
var bgColor color.Color
if hexColor == "" {
bgColor = randColor(255)
} else {
cr, _ := strconv.ParseUint(string(hexColor[:2]), 16, 64)
cg, _ := strconv.ParseUint(string(hexColor[2:4]), 16, 64)
cb, _ := strconv.ParseUint(string(hexColor[4:]), 16, 64)
bgColor = color.RGBA{R: uint8(cr), G: uint8(cg), B: uint8(cb), A: 255}
}
return NewGummy(0, 0, w, h, bgColor)
}
func NewGummy(x, y, w, h int, gummyColor color.Color) (*Gummy, error) {
img, err := createImg(x, y, w, h, gummyColor)
if err != nil {
return nil, err
}
return &Gummy{
Img: img,
Color: &gummyColor,
Font: nil,
}, nil
}
/*
Gets the image in the specified format (JPEG, GIF or PNG) in the specified writer
*/
func (g *Gummy) Get(format string, r io.Writer) error {
switch format {
case "jpeg", "JPEG", "jpg", "JPG":
jpeg.Encode(r, g.Img, nil)
case "png", "PNG":
png.Encode(r, g.Img)
case "gif", "GIF":
gif.Encode(r, g.Img, nil)
default:
return errors.New("Wrong format")
}
return nil
}
func (g *Gummy) SaveJpeg(path string) error {
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
return g.Get("jpeg", file)
}
func (g *Gummy) SaveGif(path string) error {
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
return g.Get("gif", file)
}
func (g *Gummy) SavePng(path string) error {
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
return g.Get("png", file)
}
func (g *Gummy) GetJpeg() ([]byte, error) {
b := new(bytes.Buffer)
err := g.Get("jpeg", b)
return b.Bytes(), err
}
func (g *Gummy) GetGif() ([]byte, error) {
b := new(bytes.Buffer)
err := g.Get("gif", b)
return b.Bytes(), err
}
func (g *Gummy) GetPng() ([]byte, error) {
b := new(bytes.Buffer)
err := g.Get("png", b)
return b.Bytes(), err
}
// Color in HEX format: FAFAFA
func (g *Gummy) DrawText(text, textColor string, fontSize, xPosition, yPosition int) error {
// Get black or white depending on the background
if textColor == "" {
c := (*g.Color).(color.RGBA)
if blackWithBackground(float64(c.R), float64(c.G), float64(c.B)) {
textColor = "000000"
} else {
textColor = "FFFFFF"
}
}
fc := freetype.NewContext()
fc.SetDst(g.Img)
fc.SetFont(g.Font)
fc.SetClip(g.Img.Bounds())
// Color parsing
cr, _ := strconv.ParseUint(string(textColor[:2]), 16, 64)
cg, _ := strconv.ParseUint(string(textColor[2:4]), 16, 64)
cb, _ := strconv.ParseUint(string(textColor[4:]), 16, 64)
c := image.NewUniform(color.RGBA{R: uint8(cr), G: uint8(cg), B: uint8(cb), A: 255})
fc.SetSrc(c)
fc.SetFontSize(float64(fontSize))
_, err := fc.DrawString(text, freetype.Pt(xPosition, yPosition))
return err
}
func (g *Gummy) DrawTextCenter(text string, textColor string) error {
// I can't get the text final size so more or less center the text with this
// manual awful stuff :/
size := g.Img.Rect.Max.Y
if g.Img.Rect.Max.X < g.Img.Rect.Max.Y {
size = g.Img.Rect.Max.X
}
textSize := (size - (size / 10 * 2))
fontSize := textSize / len(text) * 2
x := g.Img.Rect.Max.X/2 - textSize/2 - fontSize/8
y := g.Img.Rect.Max.Y/2 + textSize/10 + fontSize/16
return g.DrawText(
text,
textColor,
fontSize,
x,
y,
)
}
// Color in HEX format: FAFAFA
// If "" the color of the text is black or white depending on the brightness of the bg
func (g *Gummy) DrawTextSize(textColor string) error {
return g.DrawTextCenter(fmt.Sprintf("%dx%d", g.Img.Rect.Max.X, g.Img.Rect.Max.Y), textColor)
}
func LoadFont(path string) (*truetype.Font, error) {
bs, err := ioutil.ReadFile(path)
// quick debug
if err != nil {
fmt.Println(err)
}
f, err := truetype.Parse(bs)
return f, err
}
func (g *Gummy) SetFont(path string) error {
f, err := LoadFont(path)
g.Font = f
return err
}
func createImg(x, y, w, h int, gummyColor color.Color) (*image.RGBA, error) {
img := image.NewRGBA(image.Rect(x, y, w, h))
// Colorize!
for y := img.Rect.Min.Y; y < img.Rect.Max.Y; y++ {
for x := img.Rect.Min.X; x < img.Rect.Max.X; x++ {
img.Set(x, y, gummyColor)
}
}
return img, nil
}
func randColor(alpha int) color.Color {
random := func(min, max int) int {
rand.Seed(time.Now().UnixNano())
return rand.Intn(max-min) + min
}
r := uint8(random(0, 255))
g := uint8(random(0, 255))
b := uint8(random(0, 255))
return color.RGBA{r, g, b, uint8(alpha)}
}
func inverseColor(r, g, b int) (rr, rg, rb int) {
rr = 255 - r
rg = 255 - g
rb = 255 - b
return
}
// Returns false if white text with that background
// Rrturns true if black text with that background
// Calculates based on the brightness
// Source: http://stackoverflow.com/a/2241471
func blackWithBackground(r, g, b float64) bool {
perceivedBrightness := func(r, g, b float64) int {
return int(math.Sqrt(r*r*0.241 + g*g*0.691 + b*b*0.068))
}
return perceivedBrightness(r, g, b) > 130
}