diff --git a/primitive/model.go b/primitive/model.go index 80f49ae..a85f3b4 100644 --- a/primitive/model.go +++ b/primitive/model.go @@ -9,17 +9,17 @@ import ( ) type Model struct { - Sw, Sh int - Scale float64 - Background Color - Target *image.RGBA - Current *image.RGBA - Context *gg.Context - Score float64 - Shapes []Shape - Colors []Color - Scores []float64 - Workers []*Worker + Sw, Sh, Vw, Vh int + Scale float64 + Background Color + Target *image.RGBA + Current *image.RGBA + Context *gg.Context + Score float64 + Shapes []Shape + Colors []Color + Scores []float64 + Workers []*Worker } func NewModel(target image.Image, background Color, size, numWorkers int) *Model { @@ -41,6 +41,8 @@ func NewModel(target image.Image, background Color, size, numWorkers int) *Model model := &Model{} model.Sw = sw model.Sh = sh + model.Vw = sw / int(scale) + model.Vh = sh / int(scale) model.Scale = scale model.Background = background model.Target = imageToRGBA(target) @@ -85,15 +87,23 @@ func (model *Model) Frames(scoreDelta float64) []image.Image { func (model *Model) SVG() string { bg := model.Background + fillA := model.Colors[0].A var lines []string - lines = append(lines, fmt.Sprintf("", model.Sw, model.Sh)) - lines = append(lines, fmt.Sprintf("", model.Sw, model.Sh, bg.R, bg.G, bg.B)) - lines = append(lines, fmt.Sprintf("", model.Scale)) + lines = append(lines, fmt.Sprintf("", model.Vw, model.Vh)) + lines = append(lines, fmt.Sprintf("", bg.R, bg.G, bg.B)) + group := "" + lines = append(lines, fmt.Sprintf(group, float64(fillA)/255)) for i, shape := range model.Shapes { + var attrs []string c := model.Colors[i] - attrs := "fill=\"#%02x%02x%02x\" fill-opacity=\"%f\"" - attrs = fmt.Sprintf(attrs, c.R, c.G, c.B, float64(c.A)/255) - lines = append(lines, shape.SVG(attrs)) + fill := "fill=\"#%02x%02x%02x\"" + fill = fmt.Sprintf(fill, c.R, c.G, c.B) + attrs = append(attrs, fill) + if c.A != fillA { + opacity := "fill-opacity=\"%f\"" + attrs = append(attrs, fmt.Sprintf(opacity, float64(c.A)/255)) + } + lines = append(lines, shape.SVG(strings.Join(attrs, " "))) } lines = append(lines, "") lines = append(lines, "")