35 lines
679 B
Go
35 lines
679 B
Go
|
package main
|
||
|
|
||
|
import (
|
||
|
"image"
|
||
|
"image/color"
|
||
|
|
||
|
"opslag.de/schobers/tins2020/img"
|
||
|
)
|
||
|
|
||
|
func colorImage(path string, col color.Color) error {
|
||
|
src, err := img.DecodeImage(path)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
colNRGBA := color.NRGBAModel.Convert(col).(color.NRGBA)
|
||
|
bounds := src.Bounds()
|
||
|
dst := image.NewRGBA(bounds)
|
||
|
for y := bounds.Min.Y; y < bounds.Max.Y; y++ {
|
||
|
for x := bounds.Min.X; x < bounds.Max.X; x++ {
|
||
|
c := src.At(x, y)
|
||
|
srcCol := color.NRGBAModel.Convert(c).(color.NRGBA)
|
||
|
if srcCol.A > 0 {
|
||
|
dstCol := colNRGBA
|
||
|
dstCol.A = srcCol.A
|
||
|
dst.Set(x, y, dstCol)
|
||
|
} else {
|
||
|
dst.Set(x, y, c)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
return img.EncodePNG(path, dst)
|
||
|
return nil
|
||
|
}
|