package main
import (
"image/color"
"net/url"
"strconv"
)
type Options struct {
// image
Width, Height int
Pattern string
Colors []color.RGBA
// web
Desc string
}
// to be used only with dopt.Copy(), otherwise the default colors can be changed.
var dopt = &Options{
// image
Width: 200,
Height: 200,
Pattern: "plain",
Colors: []color.RGBA{
color.RGBA{0x24, 0x29, 0x33, 0xff},
color.RGBA{0xec, 0xef, 0xf4, 0xff},
},
// web
Desc: "Dummy image generator",
}
// FromForm gets values from the form (which includes the url encoded query)
// and applies them to our Options. To ne used with r.Form after r.ParseForm().
func (opt *Options) FromForm(form url.Values) error {
// image
if w, ok := form["width"]; ok {
i, err := strconv.Atoi(w[0])
if err != nil {
return err
}
opt.Width = i
}
if h, ok := form["height"]; ok {
i, err := strconv.Atoi(h[0])
if err != nil {
return err
}
opt.Height = i
}
if p, ok := form["pattern"]; ok {
opt.Pattern = p[0]
}
if colors, ok := form["color"]; ok {
for i, h := range colors {
c, err := Hex(h)
if err != nil {
return err
}
opt.Colors[i] = c
}
}
// web
if d, ok := form["desc"]; ok {
opt.Desc = d[0]
}
return nil
}
// Copy so that the original options don't change (essentially a deep copy).
func (opt Options) Copy() (copt Options) {
// copy all the fields
copt = opt
// deep copy for slices
copt.Colors = make([]color.RGBA, len(opt.Colors))
copy(copt.Colors, opt.Colors)
return copt
}
// Normalize our Options, implement limits and workarounds.
func (opt *Options) Normalize() {
// limit width to 600 for now
if opt.Width > 600 {
opt.Width = 600
}
}