Loading controllers/squares/random.go +21 −13 Original line number Diff line number Diff line Loading @@ -12,35 +12,43 @@ import ( "github.com/taironas/tinygraphs/write" ) // handler for "/squares/random" // generates a black and white grid random image. // Random handler for "/squares/random" // generates a random 6 by 6 grid image. func Random(w http.ResponseWriter, r *http.Request) { size := extract.Size(r) theme := extract.Theme(r) numColors := extract.NumColors(r) colorMap := colors.MapOfColorThemes() var bg, fg color.RGBA var err error if val, ok := colorMap[theme]; ok { bg = val[0] fg = val[1] } else { if bg, err = extract.Background(r); err != nil { bg = colorMap["base"][0] } if fg, err = extract.Foreground(r); err != nil { fg = colorMap["base"][1] } var colors []color.RGBA if theme != "base" { if _, ok := colorMap[theme]; ok { colors = append(colors, colorMap[theme][0:numColors]...) } else { colors = append(colors, colorMap["base"]...) } } else { colors = append(colors, bg, fg) } if f := extract.Format(r); f == format.JPEG { m := image.NewRGBA(image.Rect(0, 0, size, size)) draw.RandomGrid6X6(m, bg, fg) draw.RandomGrid6X6(m, colors) var img image.Image = m write.ImageJPEG(w, &img) } else if f == format.SVG { write.ImageSVG(w) draw.RandomGrid6X6SVG(w, bg, fg, size) draw.RandomGrid6X6SVG(w, colors, size) } } controllers/squares/square.go +2 −2 Original line number Diff line number Diff line Loading @@ -18,8 +18,8 @@ import ( "github.com/taironas/tinygraphs/write" ) // Square is the handler for /squares/:key // builds a 6x6 grid with alternate colors based on the number passed in the url. // Square handler for /squares/:key // builds a 6 by 6 grid with alternate colors based the key passed in the url. func Square(w http.ResponseWriter, r *http.Request) { var err error var key string Loading draw/draw.go +4 −4 Original line number Diff line number Diff line Loading @@ -54,7 +54,7 @@ func Grid6X6SVG(w http.ResponseWriter, color1, color2 color.RGBA, size int) { } // RandomGrid6X6 builds a grid image with with 2 colors selected at random for each quadrant. func RandomGrid6X6(m *image.RGBA, color1, color2 color.RGBA) { func RandomGrid6X6(m *image.RGBA, colors []color.RGBA) { size := m.Bounds().Size() quad := size.X / 6 colorMap := make(map[int]color.RGBA) Loading @@ -68,7 +68,7 @@ func RandomGrid6X6(m *image.RGBA, color1, color2 color.RGBA) { for y := 0; y < size.Y; y++ { yQuadrant := y / quad if _, ok := colorMap[yQuadrant]; !ok { colorMap[yQuadrant] = randomColor(color1, color2) colorMap[yQuadrant] = randomColorFromArray(colors) } m.Set(x, y, colorMap[yQuadrant]) } Loading @@ -76,7 +76,7 @@ func RandomGrid6X6(m *image.RGBA, color1, color2 color.RGBA) { } // RandomGrid6X6SVG builds a grid image with with 2 colors selected at random for each quadrant. func RandomGrid6X6SVG(w http.ResponseWriter, color1, color2 color.RGBA, size int) { func RandomGrid6X6SVG(w http.ResponseWriter, colors []color.RGBA, size int) { canvas := svg.New(w) canvas.Start(size, size) squares := 6 Loading @@ -89,7 +89,7 @@ func RandomGrid6X6SVG(w http.ResponseWriter, color1, color2 color.RGBA, size int for xQ := 0; xQ < squares; xQ++ { x := xQ * quadrantSize if _, ok := colorMap[xQ]; !ok { colorMap[xQ] = randomColor(color1, color2) colorMap[xQ] = randomColorFromArray(colors) } canvas.Rect(x, y, quadrantSize, quadrantSize, fillFromRGBA(colorMap[xQ])) } Loading draw/tools.go +7 −1 Original line number Diff line number Diff line Loading @@ -9,7 +9,7 @@ import ( "strconv" ) // getRandomColor returns a random color between c1 and c2 // randomColor returns a random color between c1 and c2 func randomColor(c1, c2 color.RGBA) color.RGBA { r := rand.Intn(2) if r == 1 { Loading @@ -18,6 +18,12 @@ func randomColor(c1, c2 color.RGBA) color.RGBA { return c2 } // getRandomColor returns a random color between c1 and c2 func randomColorFromArray(colors []color.RGBA) color.RGBA { r := rand.Intn(len(colors)) return colors[r] } func fillFromRGBA(c color.RGBA) string { return fmt.Sprintf("fill:rgb(%d,%d,%d)", c.R, c.G, c.B) } Loading Loading
controllers/squares/random.go +21 −13 Original line number Diff line number Diff line Loading @@ -12,35 +12,43 @@ import ( "github.com/taironas/tinygraphs/write" ) // handler for "/squares/random" // generates a black and white grid random image. // Random handler for "/squares/random" // generates a random 6 by 6 grid image. func Random(w http.ResponseWriter, r *http.Request) { size := extract.Size(r) theme := extract.Theme(r) numColors := extract.NumColors(r) colorMap := colors.MapOfColorThemes() var bg, fg color.RGBA var err error if val, ok := colorMap[theme]; ok { bg = val[0] fg = val[1] } else { if bg, err = extract.Background(r); err != nil { bg = colorMap["base"][0] } if fg, err = extract.Foreground(r); err != nil { fg = colorMap["base"][1] } var colors []color.RGBA if theme != "base" { if _, ok := colorMap[theme]; ok { colors = append(colors, colorMap[theme][0:numColors]...) } else { colors = append(colors, colorMap["base"]...) } } else { colors = append(colors, bg, fg) } if f := extract.Format(r); f == format.JPEG { m := image.NewRGBA(image.Rect(0, 0, size, size)) draw.RandomGrid6X6(m, bg, fg) draw.RandomGrid6X6(m, colors) var img image.Image = m write.ImageJPEG(w, &img) } else if f == format.SVG { write.ImageSVG(w) draw.RandomGrid6X6SVG(w, bg, fg, size) draw.RandomGrid6X6SVG(w, colors, size) } }
controllers/squares/square.go +2 −2 Original line number Diff line number Diff line Loading @@ -18,8 +18,8 @@ import ( "github.com/taironas/tinygraphs/write" ) // Square is the handler for /squares/:key // builds a 6x6 grid with alternate colors based on the number passed in the url. // Square handler for /squares/:key // builds a 6 by 6 grid with alternate colors based the key passed in the url. func Square(w http.ResponseWriter, r *http.Request) { var err error var key string Loading
draw/draw.go +4 −4 Original line number Diff line number Diff line Loading @@ -54,7 +54,7 @@ func Grid6X6SVG(w http.ResponseWriter, color1, color2 color.RGBA, size int) { } // RandomGrid6X6 builds a grid image with with 2 colors selected at random for each quadrant. func RandomGrid6X6(m *image.RGBA, color1, color2 color.RGBA) { func RandomGrid6X6(m *image.RGBA, colors []color.RGBA) { size := m.Bounds().Size() quad := size.X / 6 colorMap := make(map[int]color.RGBA) Loading @@ -68,7 +68,7 @@ func RandomGrid6X6(m *image.RGBA, color1, color2 color.RGBA) { for y := 0; y < size.Y; y++ { yQuadrant := y / quad if _, ok := colorMap[yQuadrant]; !ok { colorMap[yQuadrant] = randomColor(color1, color2) colorMap[yQuadrant] = randomColorFromArray(colors) } m.Set(x, y, colorMap[yQuadrant]) } Loading @@ -76,7 +76,7 @@ func RandomGrid6X6(m *image.RGBA, color1, color2 color.RGBA) { } // RandomGrid6X6SVG builds a grid image with with 2 colors selected at random for each quadrant. func RandomGrid6X6SVG(w http.ResponseWriter, color1, color2 color.RGBA, size int) { func RandomGrid6X6SVG(w http.ResponseWriter, colors []color.RGBA, size int) { canvas := svg.New(w) canvas.Start(size, size) squares := 6 Loading @@ -89,7 +89,7 @@ func RandomGrid6X6SVG(w http.ResponseWriter, color1, color2 color.RGBA, size int for xQ := 0; xQ < squares; xQ++ { x := xQ * quadrantSize if _, ok := colorMap[xQ]; !ok { colorMap[xQ] = randomColor(color1, color2) colorMap[xQ] = randomColorFromArray(colors) } canvas.Rect(x, y, quadrantSize, quadrantSize, fillFromRGBA(colorMap[xQ])) } Loading
draw/tools.go +7 −1 Original line number Diff line number Diff line Loading @@ -9,7 +9,7 @@ import ( "strconv" ) // getRandomColor returns a random color between c1 and c2 // randomColor returns a random color between c1 and c2 func randomColor(c1, c2 color.RGBA) color.RGBA { r := rand.Intn(2) if r == 1 { Loading @@ -18,6 +18,12 @@ func randomColor(c1, c2 color.RGBA) color.RGBA { return c2 } // getRandomColor returns a random color between c1 and c2 func randomColorFromArray(colors []color.RGBA) color.RGBA { r := rand.Intn(len(colors)) return colors[r] } func fillFromRGBA(c color.RGBA) string { return fmt.Sprintf("fill:rgb(%d,%d,%d)", c.R, c.G, c.B) } Loading