-
Notifications
You must be signed in to change notification settings - Fork 1
/
util.go
83 lines (72 loc) · 1.47 KB
/
util.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
package image2tiles
import (
"fmt"
"image"
"image/color"
"image/draw"
"image/jpeg"
"image/png"
"os"
"strings"
)
func LoadImage(path string) (image.Image, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
im, _, err := image.Decode(file)
return im, err
}
func LoadPNG(path string) (image.Image, error) {
file, err := os.Open(path)
if err != nil {
return nil, err
}
defer file.Close()
return png.Decode(file)
}
func SavePNG(path string, im image.Image) error {
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
return png.Encode(file, im)
}
func SaveJPG(path string, im image.Image, quality int) error {
file, err := os.Create(path)
if err != nil {
return err
}
defer file.Close()
var opt jpeg.Options
opt.Quality = quality
return jpeg.Encode(file, im, &opt)
}
func ImageToRGBA(src image.Image) *image.RGBA {
bounds := src.Bounds()
dst := image.NewRGBA(bounds)
draw.Draw(dst, bounds, src, bounds.Min, draw.Src)
return dst
}
func ParseHexColor(x string) color.RGBA {
x = strings.TrimPrefix(x, "#")
c := color.RGBA{R: 255, G: 255, B: 255, A: 255}
if len(x) == 3 {
format := "%1x%1x%1x"
fmt.Sscanf(x, format, &c.R, &c.G, &c.B)
c.R |= c.R << 4
c.G |= c.G << 4
c.B |= c.B << 4
}
if len(x) == 6 {
format := "%02x%02x%02x"
fmt.Sscanf(x, format, &c.R, &c.G, &c.B)
}
if len(x) == 8 {
format := "%02x%02x%02x%02x"
fmt.Sscanf(x, format, &c.R, &c.G, &c.B, &c.A)
}
return c
}