Skip to content

Instantly share code, notes, and snippets.

@macrat
Created November 9, 2017 05:25
Show Gist options
  • Save macrat/80791bdf96bb1078056eebbaafb7755e to your computer and use it in GitHub Desktop.
Save macrat/80791bdf96bb1078056eebbaafb7755e to your computer and use it in GitHub Desktop.
golangでグラデーション作ったり、文字入れたり。
package main
import (
"image"
"image/color"
"image/png"
"os"
"golang.org/x/image/font"
"golang.org/x/image/font/basicfont"
"golang.org/x/image/math/fixed"
)
func main() {
img := image.NewRGBA(image.Rect(0, 0, 320, 240))
for y := img.Rect.Min.Y; y < img.Rect.Max.Y; y++ {
for x := img.Rect.Min.X; x < img.Rect.Max.X; x++ {
xrate := float32(x) / float32(img.Rect.Max.X)
yrate := float32(y) / float32(img.Rect.Max.Y)
img.Set(x, y, color.RGBA{uint8(xrate * 255 * yrate), 0, 0, uint8(yrate * 255)})
}
}
d := &font.Drawer{
Dst: img,
Src: image.NewUniform(color.RGBA{255, 255, 255, 255}),
Face: basicfont.Face7x13,
Dot: fixed.Point26_6{fixed.Int26_6(160 * 64), fixed.Int26_6(120 * 64)},
}
d.DrawString("hello world")
file, err := os.Create("tea.png")
if err != nil {
panic(err.Error())
}
defer file.Close()
if err := png.Encode(file, img); err != nil {
panic(err.Error())
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment