2016-02-19 15:43:37 +01:00
|
|
|
package gg
|
2016-02-18 22:02:57 +01:00
|
|
|
|
|
|
|
import (
|
2016-02-19 17:07:25 +01:00
|
|
|
"fmt"
|
2016-02-18 22:02:57 +01:00
|
|
|
"image"
|
2016-02-19 17:07:25 +01:00
|
|
|
"image/draw"
|
2016-12-14 20:26:04 +01:00
|
|
|
_ "image/jpeg"
|
2016-02-18 22:02:57 +01:00
|
|
|
"image/png"
|
2016-02-19 04:53:47 +01:00
|
|
|
"io/ioutil"
|
2016-02-19 19:16:40 +01:00
|
|
|
"math"
|
2016-02-18 22:02:57 +01:00
|
|
|
"os"
|
2016-02-19 17:07:25 +01:00
|
|
|
"strings"
|
2016-02-18 22:02:57 +01:00
|
|
|
|
2016-02-19 04:53:47 +01:00
|
|
|
"github.com/golang/freetype/truetype"
|
|
|
|
|
|
|
|
"golang.org/x/image/font"
|
2016-02-18 22:02:57 +01:00
|
|
|
"golang.org/x/image/math/fixed"
|
|
|
|
)
|
|
|
|
|
2016-02-19 19:16:40 +01:00
|
|
|
func Radians(degrees float64) float64 {
|
|
|
|
return degrees * math.Pi / 180
|
|
|
|
}
|
|
|
|
|
|
|
|
func Degrees(radians float64) float64 {
|
|
|
|
return radians * 180 / math.Pi
|
|
|
|
}
|
|
|
|
|
2016-02-25 21:06:04 +01:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2016-02-19 19:49:03 +01:00
|
|
|
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 {
|
2016-02-18 22:02:57 +01:00
|
|
|
file, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer file.Close()
|
|
|
|
return png.Encode(file, im)
|
|
|
|
}
|
|
|
|
|
2016-02-19 17:07:25 +01:00
|
|
|
func imageToRGBA(src image.Image) *image.RGBA {
|
|
|
|
dst := image.NewRGBA(src.Bounds())
|
|
|
|
draw.Draw(dst, dst.Rect, src, image.ZP, draw.Src)
|
|
|
|
return dst
|
|
|
|
}
|
|
|
|
|
2016-02-20 21:42:36 +01:00
|
|
|
func parseHexColor(x string) (r, g, b, a int) {
|
2016-02-19 17:07:25 +01:00
|
|
|
x = strings.TrimPrefix(x, "#")
|
2016-02-20 21:42:36 +01:00
|
|
|
a = 255
|
2016-02-19 17:07:25 +01:00
|
|
|
if len(x) == 3 {
|
|
|
|
format := "%1x%1x%1x"
|
|
|
|
fmt.Sscanf(x, format, &r, &g, &b)
|
|
|
|
r |= r << 4
|
|
|
|
g |= g << 4
|
|
|
|
b |= b << 4
|
|
|
|
}
|
|
|
|
if len(x) == 6 {
|
|
|
|
format := "%02x%02x%02x"
|
|
|
|
fmt.Sscanf(x, format, &r, &g, &b)
|
|
|
|
}
|
2016-02-20 21:42:36 +01:00
|
|
|
if len(x) == 8 {
|
|
|
|
format := "%02x%02x%02x%02x"
|
|
|
|
fmt.Sscanf(x, format, &r, &g, &b, &a)
|
|
|
|
}
|
2016-02-19 17:07:25 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-02-23 04:33:22 +01:00
|
|
|
func fixp(x, y float64) fixed.Point26_6 {
|
2016-02-23 04:34:11 +01:00
|
|
|
return fixed.Point26_6{fix(x), fix(y)}
|
2016-02-18 22:02:57 +01:00
|
|
|
}
|
|
|
|
|
2016-02-23 04:33:22 +01:00
|
|
|
func fix(x float64) fixed.Int26_6 {
|
2016-02-18 22:02:57 +01:00
|
|
|
return fixed.Int26_6(x * 64)
|
|
|
|
}
|
2016-02-19 04:53:47 +01:00
|
|
|
|
2016-02-22 04:57:37 +01:00
|
|
|
func unfix(x fixed.Int26_6) float64 {
|
|
|
|
const shift, mask = 6, 1<<6 - 1
|
|
|
|
if x >= 0 {
|
|
|
|
return float64(x>>shift) + float64(x&mask)/64
|
|
|
|
}
|
|
|
|
x = -x
|
|
|
|
if x >= 0 {
|
|
|
|
return -(float64(x>>shift) + float64(x&mask)/64)
|
|
|
|
}
|
|
|
|
return 0
|
|
|
|
}
|
|
|
|
|
2016-12-14 20:26:04 +01:00
|
|
|
func LoadFontFace(path string, points float64) (font.Face, error) {
|
2016-02-19 04:53:47 +01:00
|
|
|
fontBytes, err := ioutil.ReadFile(path)
|
|
|
|
if err != nil {
|
2016-02-23 04:36:34 +01:00
|
|
|
return nil, err
|
2016-02-19 04:53:47 +01:00
|
|
|
}
|
|
|
|
f, err := truetype.Parse(fontBytes)
|
|
|
|
if err != nil {
|
2016-02-23 04:36:34 +01:00
|
|
|
return nil, err
|
2016-02-19 04:53:47 +01:00
|
|
|
}
|
2016-02-23 04:36:34 +01:00
|
|
|
face := truetype.NewFace(f, &truetype.Options{
|
2016-02-20 05:03:39 +01:00
|
|
|
Size: points,
|
|
|
|
// Hinting: font.HintingFull,
|
2016-02-19 04:53:47 +01:00
|
|
|
})
|
2016-02-23 04:36:34 +01:00
|
|
|
return face, nil
|
2016-02-19 04:53:47 +01:00
|
|
|
}
|