@@ -9,17 +9,17 @@ import (
9
9
)
10
10
11
11
type Model struct {
12
- Sw , Sh int
13
- Scale float64
14
- Background Color
15
- Target * image.RGBA
16
- Current * image.RGBA
17
- Context * gg.Context
18
- Score float64
19
- Shapes []Shape
20
- Colors []Color
21
- Scores []float64
22
- Workers []* Worker
12
+ Sw , Sh , Vw , Vh int
13
+ Scale float64
14
+ Background Color
15
+ Target * image.RGBA
16
+ Current * image.RGBA
17
+ Context * gg.Context
18
+ Score float64
19
+ Shapes []Shape
20
+ Colors []Color
21
+ Scores []float64
22
+ Workers []* Worker
23
23
}
24
24
25
25
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
41
41
model := & Model {}
42
42
model .Sw = sw
43
43
model .Sh = sh
44
+ model .Vw = sw / int (scale )
45
+ model .Vh = sh / int (scale )
44
46
model .Scale = scale
45
47
model .Background = background
46
48
model .Target = imageToRGBA (target )
@@ -85,15 +87,23 @@ func (model *Model) Frames(scoreDelta float64) []image.Image {
85
87
86
88
func (model * Model ) SVG () string {
87
89
bg := model .Background
90
+ fillA := model .Colors [0 ].A
88
91
var lines []string
89
- lines = append (lines , fmt .Sprintf ("<svg xmlns=\" http://www.w3.org/2000/svg\" version=\" 1.1\" width=\" %d\" height=\" %d\" >" , model .Sw , model .Sh ))
90
- lines = append (lines , fmt .Sprintf ("<rect x=\" 0\" y=\" 0\" width=\" %d\" height=\" %d\" fill=\" #%02x%02x%02x\" />" , model .Sw , model .Sh , bg .R , bg .G , bg .B ))
91
- lines = append (lines , fmt .Sprintf ("<g transform=\" scale(%f) translate(0.5 0.5)\" >" , model .Scale ))
92
+ lines = append (lines , fmt .Sprintf ("<svg xmlns=\" http://www.w3.org/2000/svg\" version=\" 1.1\" viewBox=\" 0 0 %d %d\" >" , model .Vw , model .Vh ))
93
+ lines = append (lines , fmt .Sprintf ("<rect width=\" 100%%\" height=\" 100%%\" fill=\" #%02x%02x%02x\" />" , bg .R , bg .G , bg .B ))
94
+ group := "<g fill-opacity=\" %f\" >"
95
+ lines = append (lines , fmt .Sprintf (group , float64 (fillA )/ 255 ))
92
96
for i , shape := range model .Shapes {
97
+ var attrs []string
93
98
c := model .Colors [i ]
94
- attrs := "fill=\" #%02x%02x%02x\" fill-opacity=\" %f\" "
95
- attrs = fmt .Sprintf (attrs , c .R , c .G , c .B , float64 (c .A )/ 255 )
96
- lines = append (lines , shape .SVG (attrs ))
99
+ fill := "fill=\" #%02x%02x%02x\" "
100
+ fill = fmt .Sprintf (fill , c .R , c .G , c .B )
101
+ attrs = append (attrs , fill )
102
+ if c .A != fillA {
103
+ opacity := "fill-opacity=\" %f\" "
104
+ attrs = append (attrs , fmt .Sprintf (opacity , float64 (c .A )/ 255 ))
105
+ }
106
+ lines = append (lines , shape .SVG (strings .Join (attrs , " " )))
97
107
}
98
108
lines = append (lines , "</g>" )
99
109
lines = append (lines , "</svg>" )
@@ -148,7 +158,7 @@ func (model *Model) Step(shapeType ShapeType, alpha, repeat int) int {
148
158
return counter
149
159
}
150
160
151
- func (model * Model ) runWorkers (t ShapeType , a , n , age , m int ) * State {
161
+ func (model * Model ) runWorkers (t ShapeType , alphaVal , n , age , m int ) * State {
152
162
wn := len (model .Workers )
153
163
ch := make (chan * State , wn )
154
164
wm := m / wn
@@ -158,7 +168,7 @@ func (model *Model) runWorkers(t ShapeType, a, n, age, m int) *State {
158
168
for i := 0 ; i < wn ; i ++ {
159
169
worker := model .Workers [i ]
160
170
worker .Init (model .Current , model .Score )
161
- go model .runWorker (worker , t , a , n , age , wm , ch )
171
+ go model .runWorker (worker , t , alphaVal , n , age , wm , ch )
162
172
}
163
173
var bestEnergy float64
164
174
var bestState * State
0 commit comments