2021-02-26 08:09:37 +00:00
|
|
|
package generativeart
|
|
|
|
|
|
|
|
import (
|
2021-03-11 09:35:03 +00:00
|
|
|
"github.com/jdxyw/generativeart/common"
|
2021-02-26 08:09:37 +00:00
|
|
|
"image"
|
|
|
|
"image/color"
|
|
|
|
"image/draw"
|
|
|
|
"image/jpeg"
|
|
|
|
"image/png"
|
|
|
|
"os"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Engine interface {
|
|
|
|
Generative(c *canva)
|
|
|
|
}
|
|
|
|
|
|
|
|
type canva struct {
|
|
|
|
height, width int
|
|
|
|
img *image.RGBA
|
2021-03-02 03:23:29 +00:00
|
|
|
opts Options
|
|
|
|
}
|
|
|
|
|
|
|
|
type Options struct {
|
2021-03-04 02:46:41 +00:00
|
|
|
background color.RGBA
|
|
|
|
foreground color.RGBA
|
|
|
|
lineColor color.RGBA
|
|
|
|
lineWidth float64
|
|
|
|
colorSchema []color.RGBA
|
|
|
|
nIters int
|
|
|
|
alpha int
|
2021-02-26 08:09:37 +00:00
|
|
|
}
|
|
|
|
|
2021-02-26 08:10:55 +00:00
|
|
|
// NewCanva returns a canva.
|
2021-03-04 02:46:41 +00:00
|
|
|
func NewCanva(h, w int) *canva {
|
2021-02-26 08:09:37 +00:00
|
|
|
return &canva{
|
|
|
|
height: h,
|
|
|
|
width: w,
|
|
|
|
img: image.NewRGBA(image.Rect(0, 0, h, w)),
|
2021-03-02 03:23:29 +00:00
|
|
|
// Set some defaults value
|
|
|
|
opts: Options{
|
2021-03-11 09:35:03 +00:00
|
|
|
background: common.Azure,
|
|
|
|
foreground: common.MistyRose,
|
|
|
|
lineColor: common.Tomato,
|
2021-03-04 02:46:41 +00:00
|
|
|
lineWidth: 3,
|
2021-03-11 09:35:03 +00:00
|
|
|
colorSchema: common.Plasma,
|
2021-03-04 02:46:41 +00:00
|
|
|
nIters: 20,
|
|
|
|
alpha: 255,
|
2021-03-02 03:23:29 +00:00
|
|
|
},
|
2021-02-26 08:09:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-02 03:23:29 +00:00
|
|
|
func (c *canva) SetOptions(opts Options) {
|
|
|
|
c.opts = opts
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *canva) SetBackground(rgba color.RGBA) {
|
|
|
|
c.opts.background = rgba
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *canva) SetForeground(rgba color.RGBA) {
|
|
|
|
c.opts.foreground = rgba
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *canva) SetColorSchema(rgbas []color.RGBA) {
|
|
|
|
c.opts.colorSchema = rgbas
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *canva) SetLineColor(rgba color.RGBA) {
|
|
|
|
c.opts.lineColor = rgba
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *canva) SetLineWidth(lw float64) {
|
|
|
|
c.opts.lineWidth = lw
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *canva) SetIterations(nIters int) {
|
|
|
|
c.opts.nIters = nIters
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *canva) SetAlpha(alpha int) {
|
|
|
|
c.opts.alpha = alpha
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *canva) Draw(e Engine) {
|
|
|
|
e.Generative(c)
|
|
|
|
}
|
|
|
|
|
|
|
|
// FillBackground fills the background of the canva.
|
|
|
|
func (c *canva) FillBackground() {
|
|
|
|
draw.Draw(c.img, c.img.Bounds(), &image.Uniform{c.opts.background}, image.ZP, draw.Src)
|
2021-02-26 08:09:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ToPng saves the image to local with PNG format.
|
2021-03-02 03:23:29 +00:00
|
|
|
func (c *canva) ToPNG(fpath string) error {
|
2021-02-26 08:09:37 +00:00
|
|
|
f, err := os.Create(fpath)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := png.Encode(f, c.img); err != nil {
|
|
|
|
f.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ToJpeg saves the image to local with Jpeg format.
|
2021-03-02 03:23:29 +00:00
|
|
|
func (c *canva) ToJPEG(path string) error {
|
2021-02-26 08:09:37 +00:00
|
|
|
f, err := os.Create(path)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if err := jpeg.Encode(f, c.img, nil); err != nil {
|
|
|
|
f.Close()
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := f.Close(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|