65 lines
1.2 KiB
Go
65 lines
1.2 KiB
Go
package generator
|
|
|
|
import (
|
|
"fmt"
|
|
"image"
|
|
"image/color"
|
|
|
|
"golang.org/x/image/font"
|
|
"golang.org/x/image/font/basicfont"
|
|
"golang.org/x/image/math/fixed"
|
|
)
|
|
|
|
var (
|
|
errInvalidFormat = fmt.Errorf("invalid format")
|
|
)
|
|
|
|
func parseHexColorFast(s string) (color.RGBA, error) {
|
|
var err error
|
|
c := color.RGBA{}
|
|
c.A = 0xff
|
|
|
|
if s[0] != '#' {
|
|
return c, errInvalidFormat
|
|
}
|
|
|
|
hexToByte := func(b byte) byte {
|
|
switch {
|
|
case b >= '0' && b <= '9':
|
|
return b - '0'
|
|
case b >= 'a' && b <= 'f':
|
|
return b - 'a' + 10
|
|
case b >= 'A' && b <= 'F':
|
|
return b - 'A' + 10
|
|
}
|
|
err = errInvalidFormat
|
|
return 0
|
|
}
|
|
|
|
switch len(s) {
|
|
case 7:
|
|
c.R = hexToByte(s[1])<<4 + hexToByte(s[2])
|
|
c.G = hexToByte(s[3])<<4 + hexToByte(s[4])
|
|
c.B = hexToByte(s[5])<<4 + hexToByte(s[6])
|
|
case 4:
|
|
c.R = hexToByte(s[1]) * 17
|
|
c.G = hexToByte(s[2]) * 17
|
|
c.B = hexToByte(s[3]) * 17
|
|
default:
|
|
err = errInvalidFormat
|
|
}
|
|
return c, err
|
|
}
|
|
|
|
func drawText(img *image.RGBA, x, y int, col color.RGBA, text string) {
|
|
point := fixed.Point26_6{fixed.Int26_6(x * 64), fixed.Int26_6(y * 64)}
|
|
|
|
d := &font.Drawer{
|
|
Dst: img,
|
|
Src: image.NewUniform(col),
|
|
Face: basicfont.Face7x13,
|
|
Dot: point,
|
|
}
|
|
d.DrawString(text)
|
|
}
|