gg/util.go

89 lines
1.6 KiB
Go
Raw Normal View History

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-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-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
}
func parseHexColor(x string) (r, g, b int) {
x = strings.TrimPrefix(x, "#")
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)
}
return
}
2016-02-18 22:02:57 +01:00
func fp(x, y float64) fixed.Point26_6 {
return fixed.Point26_6{fixed.Int26_6(x * 64), fixed.Int26_6(y * 64)}
}
func fi(x float64) fixed.Int26_6 {
return fixed.Int26_6(x * 64)
}
2016-02-19 04:53:47 +01:00
2016-02-19 20:03:52 +01:00
func loadFontFace(path string, points float64) font.Face {
2016-02-19 04:53:47 +01:00
fontBytes, err := ioutil.ReadFile(path)
if err != nil {
panic(err)
}
f, err := truetype.Parse(fontBytes)
if err != nil {
panic(err)
}
return truetype.NewFace(f, &truetype.Options{
2016-02-19 20:03:52 +01:00
Size: points,
2016-02-19 04:53:47 +01:00
Hinting: font.HintingFull,
})
}