Loading app-backend/main.go +1 −0 Original line number Diff line number Diff line Loading @@ -24,6 +24,7 @@ func main() { r.HandleFunc("/squares", squares.Random) r.HandleFunc("/squares/banner/random", squares.BannerRandom) r.HandleFunc("/squares/banner/random/gradient", squares.BannerRandomGradient) r.HandleFunc("/squares/:key", squares.Square) //cached r.HandleFunc("/isogrids/:key", isogrids.Isogrids) r.HandleFunc("/spaceinvaders/:key", spaceinvaders.SpaceInvaders) Loading colors/colors.go +2 −5 Original line number Diff line number Diff line Loading @@ -8,10 +8,9 @@ import ( func MapOfColorThemes() map[string][]color.RGBA { return map[string][]color.RGBA{ "base": []color.RGBA{ color.RGBA{uint8(255), uint8(255), uint8(255), uint8(255)}, color.RGBA{uint8(0), uint8(0), uint8(0), uint8(255)}, color.RGBA{uint8(0), uint8(0), uint8(0), uint8(255)}, // white color.RGBA{uint8(255), uint8(255), uint8(255), uint8(255)}, // back }, "sugarsweets": []color.RGBA{ color.RGBA{232, 70, 134, 255}, // main color.RGBA{255, 245, 249, 255}, //background Loading Loading @@ -53,14 +52,12 @@ func MapOfColorThemes() map[string][]color.RGBA { color.RGBA{244, 234, 252, 255}, //background color.RGBA{70, 86, 212, 255}, // 2dary color.RGBA{201, 64, 206, 255}, // 2dary }, "frogideas": []color.RGBA{ color.RGBA{93, 214, 75, 255}, // main color.RGBA{226, 255, 222, 255}, //background color.RGBA{67, 191, 134, 255}, // 2dary color.RGBA{148, 232, 56, 255}, // 2dary }, "berrypie": []color.RGBA{ color.RGBA{248, 0, 6, 255}, // main Loading controllers/squares/banner.go +35 −0 Original line number Diff line number Diff line Loading @@ -46,3 +46,38 @@ func BannerRandom(w http.ResponseWriter, r *http.Request) { squares.RandomGridSVG(w, colors, width, height, 50) } } // BannerRandom handler for "/squares/banner/random/gradient" // generates a random banner grid image with gradient colors from brighter to darker color. func BannerRandomGradient(w http.ResponseWriter, r *http.Request) { width := extract.Width(r) height := extract.Height(r) numColors := extract.NumColors(r) colorMap := colors.MapOfColorThemes() bg, fg := extract.ExtraColors(r, colorMap) var colors []color.RGBA theme := extract.Theme(r) 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, width, height)) squares.RandomGradientGrid(m, colors, 50) var img image.Image = m write.ImageJPEG(w, &img) } else if f == format.SVG { write.ImageSVG(w) squares.RandomGradientGridSVG(w, colors, width, height, 50) } } draw/squares/random.go 0 → 100644 +100 −0 Original line number Diff line number Diff line package squares import ( "image" "image/color" "net/http" "github.com/ajstarks/svgo" "github.com/taironas/tinygraphs/draw" ) // RandomGrid builds a grid image with with x colors selected at random for each quadrant. func RandomGrid(m *image.RGBA, colors []color.RGBA, xSquares int) { size := m.Bounds().Size() quad := size.X / xSquares colorMap := make(map[int]color.RGBA) var currentQuadrand = 0 for x := 0; x < size.X; x++ { if x/quad != currentQuadrand { colorMap = make(map[int]color.RGBA) currentQuadrand = x / quad } for y := 0; y < size.Y; y++ { yQuadrant := y / quad if _, ok := colorMap[yQuadrant]; !ok { colorMap[yQuadrant] = draw.RandomColorFromArray(colors) } m.Set(x, y, colorMap[yQuadrant]) } } } // RandomGradientGrid builds a grid image with with x colors selected at random for each quadrant going from brighter to dracker color. func RandomGradientGrid(m *image.RGBA, colors []color.RGBA, xSquares int) { size := m.Bounds().Size() quad := size.X / xSquares colorMap := make(map[int]color.RGBA) var currentQuadrand = 0 for x := 0; x < size.X; x++ { if x/quad != currentQuadrand { colorMap = make(map[int]color.RGBA) currentQuadrand = x / quad } percentage := int(float64(x) / float64(size.X) * 100) for y := 0; y < size.Y; y++ { yQuadrant := y / quad if _, ok := colorMap[yQuadrant]; !ok { colorMap[yQuadrant] = draw.ColorByPercentage(colors, percentage) } m.Set(x, y, colorMap[yQuadrant]) } } } // RandomGridSVG builds a grid image with with x colors selected at random for each quadrant. func RandomGridSVG(w http.ResponseWriter, colors []color.RGBA, width, height, xSquares int) { canvas := svg.New(w) canvas.Start(width, height) squares := xSquares quadrantSize := width / squares colorMap := make(map[int]color.RGBA) for yQ := 0; yQ < squares; yQ++ { y := yQ * quadrantSize colorMap = make(map[int]color.RGBA) for xQ := 0; xQ < squares; xQ++ { x := xQ * quadrantSize if _, ok := colorMap[xQ]; !ok { colorMap[xQ] = draw.RandomColorFromArray(colors) } canvas.Rect(x, y, quadrantSize, quadrantSize, draw.FillFromRGBA(colorMap[xQ])) } } canvas.End() } // RandomGradientGridSVG builds a grid image with with x colors selected at random for each quadrant. func RandomGradientGridSVG(w http.ResponseWriter, colors []color.RGBA, width, height, xSquares int) { canvas := svg.New(w) canvas.Start(width, height) squares := xSquares quadrantSize := width / squares colorMap := make(map[int]color.RGBA) for yQ := 0; yQ < squares; yQ++ { y := yQ * quadrantSize colorMap = make(map[int]color.RGBA) for xQ := 0; xQ < squares; xQ++ { x := xQ * quadrantSize if _, ok := colorMap[xQ]; !ok { percentage := int(float64(xQ) / float64(squares) * 100) colorMap[xQ] = draw.ColorByPercentage(colors, percentage) } canvas.Rect(x, y, quadrantSize, quadrantSize, draw.FillFromRGBA(colorMap[xQ])) } } canvas.End() } draw/squares/squares.go +2 −44 Original line number Diff line number Diff line Loading @@ -54,50 +54,7 @@ func GridSVG(w http.ResponseWriter, color1, color2 color.RGBA, size int) { canvas.End() } // RandomGrid builds a 6 by 6 grid image with with 2 colors selected at random for each quadrant. func RandomGrid(m *image.RGBA, colors []color.RGBA, xSquares int) { size := m.Bounds().Size() quad := size.X / xSquares colorMap := make(map[int]color.RGBA) var currentQuadrand = 0 for x := 0; x < size.X; x++ { if x/quad != currentQuadrand { // quadrant changed, clear map colorMap = make(map[int]color.RGBA) currentQuadrand = x / quad } for y := 0; y < size.Y; y++ { yQuadrant := y / quad if _, ok := colorMap[yQuadrant]; !ok { colorMap[yQuadrant] = draw.RandomColorFromArray(colors) } m.Set(x, y, colorMap[yQuadrant]) } } } // RandomGrid6X6SVG builds a grid image with with 2 colors selected at random for each quadrant. func RandomGridSVG(w http.ResponseWriter, colors []color.RGBA, width, height, xSquares int) { canvas := svg.New(w) canvas.Start(width, height) squares := xSquares quadrantSize := width / squares colorMap := make(map[int]color.RGBA) for yQ := 0; yQ < squares; yQ++ { y := yQ * quadrantSize colorMap = make(map[int]color.RGBA) for xQ := 0; xQ < squares; xQ++ { x := xQ * quadrantSize if _, ok := colorMap[xQ]; !ok { colorMap[xQ] = draw.RandomColorFromArray(colors) } canvas.Rect(x, y, quadrantSize, quadrantSize, draw.FillFromRGBA(colorMap[xQ])) } } canvas.End() } // Squares builds an image with 6 by 6 quadrants of alternate colors. func Squares(m *image.RGBA, key string, colors []color.RGBA) { size := m.Bounds().Size() squares := 6 Loading Loading @@ -128,6 +85,7 @@ func Squares(m *image.RGBA, key string, colors []color.RGBA) { } } // SquaresSVG builds an image with 6 by 6 quadrants of alternate colors. func SquaresSVG(w http.ResponseWriter, key string, colors []color.RGBA, size int) { canvas := svg.New(w) canvas.Start(size, size) Loading Loading
app-backend/main.go +1 −0 Original line number Diff line number Diff line Loading @@ -24,6 +24,7 @@ func main() { r.HandleFunc("/squares", squares.Random) r.HandleFunc("/squares/banner/random", squares.BannerRandom) r.HandleFunc("/squares/banner/random/gradient", squares.BannerRandomGradient) r.HandleFunc("/squares/:key", squares.Square) //cached r.HandleFunc("/isogrids/:key", isogrids.Isogrids) r.HandleFunc("/spaceinvaders/:key", spaceinvaders.SpaceInvaders) Loading
colors/colors.go +2 −5 Original line number Diff line number Diff line Loading @@ -8,10 +8,9 @@ import ( func MapOfColorThemes() map[string][]color.RGBA { return map[string][]color.RGBA{ "base": []color.RGBA{ color.RGBA{uint8(255), uint8(255), uint8(255), uint8(255)}, color.RGBA{uint8(0), uint8(0), uint8(0), uint8(255)}, color.RGBA{uint8(0), uint8(0), uint8(0), uint8(255)}, // white color.RGBA{uint8(255), uint8(255), uint8(255), uint8(255)}, // back }, "sugarsweets": []color.RGBA{ color.RGBA{232, 70, 134, 255}, // main color.RGBA{255, 245, 249, 255}, //background Loading Loading @@ -53,14 +52,12 @@ func MapOfColorThemes() map[string][]color.RGBA { color.RGBA{244, 234, 252, 255}, //background color.RGBA{70, 86, 212, 255}, // 2dary color.RGBA{201, 64, 206, 255}, // 2dary }, "frogideas": []color.RGBA{ color.RGBA{93, 214, 75, 255}, // main color.RGBA{226, 255, 222, 255}, //background color.RGBA{67, 191, 134, 255}, // 2dary color.RGBA{148, 232, 56, 255}, // 2dary }, "berrypie": []color.RGBA{ color.RGBA{248, 0, 6, 255}, // main Loading
controllers/squares/banner.go +35 −0 Original line number Diff line number Diff line Loading @@ -46,3 +46,38 @@ func BannerRandom(w http.ResponseWriter, r *http.Request) { squares.RandomGridSVG(w, colors, width, height, 50) } } // BannerRandom handler for "/squares/banner/random/gradient" // generates a random banner grid image with gradient colors from brighter to darker color. func BannerRandomGradient(w http.ResponseWriter, r *http.Request) { width := extract.Width(r) height := extract.Height(r) numColors := extract.NumColors(r) colorMap := colors.MapOfColorThemes() bg, fg := extract.ExtraColors(r, colorMap) var colors []color.RGBA theme := extract.Theme(r) 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, width, height)) squares.RandomGradientGrid(m, colors, 50) var img image.Image = m write.ImageJPEG(w, &img) } else if f == format.SVG { write.ImageSVG(w) squares.RandomGradientGridSVG(w, colors, width, height, 50) } }
draw/squares/random.go 0 → 100644 +100 −0 Original line number Diff line number Diff line package squares import ( "image" "image/color" "net/http" "github.com/ajstarks/svgo" "github.com/taironas/tinygraphs/draw" ) // RandomGrid builds a grid image with with x colors selected at random for each quadrant. func RandomGrid(m *image.RGBA, colors []color.RGBA, xSquares int) { size := m.Bounds().Size() quad := size.X / xSquares colorMap := make(map[int]color.RGBA) var currentQuadrand = 0 for x := 0; x < size.X; x++ { if x/quad != currentQuadrand { colorMap = make(map[int]color.RGBA) currentQuadrand = x / quad } for y := 0; y < size.Y; y++ { yQuadrant := y / quad if _, ok := colorMap[yQuadrant]; !ok { colorMap[yQuadrant] = draw.RandomColorFromArray(colors) } m.Set(x, y, colorMap[yQuadrant]) } } } // RandomGradientGrid builds a grid image with with x colors selected at random for each quadrant going from brighter to dracker color. func RandomGradientGrid(m *image.RGBA, colors []color.RGBA, xSquares int) { size := m.Bounds().Size() quad := size.X / xSquares colorMap := make(map[int]color.RGBA) var currentQuadrand = 0 for x := 0; x < size.X; x++ { if x/quad != currentQuadrand { colorMap = make(map[int]color.RGBA) currentQuadrand = x / quad } percentage := int(float64(x) / float64(size.X) * 100) for y := 0; y < size.Y; y++ { yQuadrant := y / quad if _, ok := colorMap[yQuadrant]; !ok { colorMap[yQuadrant] = draw.ColorByPercentage(colors, percentage) } m.Set(x, y, colorMap[yQuadrant]) } } } // RandomGridSVG builds a grid image with with x colors selected at random for each quadrant. func RandomGridSVG(w http.ResponseWriter, colors []color.RGBA, width, height, xSquares int) { canvas := svg.New(w) canvas.Start(width, height) squares := xSquares quadrantSize := width / squares colorMap := make(map[int]color.RGBA) for yQ := 0; yQ < squares; yQ++ { y := yQ * quadrantSize colorMap = make(map[int]color.RGBA) for xQ := 0; xQ < squares; xQ++ { x := xQ * quadrantSize if _, ok := colorMap[xQ]; !ok { colorMap[xQ] = draw.RandomColorFromArray(colors) } canvas.Rect(x, y, quadrantSize, quadrantSize, draw.FillFromRGBA(colorMap[xQ])) } } canvas.End() } // RandomGradientGridSVG builds a grid image with with x colors selected at random for each quadrant. func RandomGradientGridSVG(w http.ResponseWriter, colors []color.RGBA, width, height, xSquares int) { canvas := svg.New(w) canvas.Start(width, height) squares := xSquares quadrantSize := width / squares colorMap := make(map[int]color.RGBA) for yQ := 0; yQ < squares; yQ++ { y := yQ * quadrantSize colorMap = make(map[int]color.RGBA) for xQ := 0; xQ < squares; xQ++ { x := xQ * quadrantSize if _, ok := colorMap[xQ]; !ok { percentage := int(float64(xQ) / float64(squares) * 100) colorMap[xQ] = draw.ColorByPercentage(colors, percentage) } canvas.Rect(x, y, quadrantSize, quadrantSize, draw.FillFromRGBA(colorMap[xQ])) } } canvas.End() }
draw/squares/squares.go +2 −44 Original line number Diff line number Diff line Loading @@ -54,50 +54,7 @@ func GridSVG(w http.ResponseWriter, color1, color2 color.RGBA, size int) { canvas.End() } // RandomGrid builds a 6 by 6 grid image with with 2 colors selected at random for each quadrant. func RandomGrid(m *image.RGBA, colors []color.RGBA, xSquares int) { size := m.Bounds().Size() quad := size.X / xSquares colorMap := make(map[int]color.RGBA) var currentQuadrand = 0 for x := 0; x < size.X; x++ { if x/quad != currentQuadrand { // quadrant changed, clear map colorMap = make(map[int]color.RGBA) currentQuadrand = x / quad } for y := 0; y < size.Y; y++ { yQuadrant := y / quad if _, ok := colorMap[yQuadrant]; !ok { colorMap[yQuadrant] = draw.RandomColorFromArray(colors) } m.Set(x, y, colorMap[yQuadrant]) } } } // RandomGrid6X6SVG builds a grid image with with 2 colors selected at random for each quadrant. func RandomGridSVG(w http.ResponseWriter, colors []color.RGBA, width, height, xSquares int) { canvas := svg.New(w) canvas.Start(width, height) squares := xSquares quadrantSize := width / squares colorMap := make(map[int]color.RGBA) for yQ := 0; yQ < squares; yQ++ { y := yQ * quadrantSize colorMap = make(map[int]color.RGBA) for xQ := 0; xQ < squares; xQ++ { x := xQ * quadrantSize if _, ok := colorMap[xQ]; !ok { colorMap[xQ] = draw.RandomColorFromArray(colors) } canvas.Rect(x, y, quadrantSize, quadrantSize, draw.FillFromRGBA(colorMap[xQ])) } } canvas.End() } // Squares builds an image with 6 by 6 quadrants of alternate colors. func Squares(m *image.RGBA, key string, colors []color.RGBA) { size := m.Bounds().Size() squares := 6 Loading Loading @@ -128,6 +85,7 @@ func Squares(m *image.RGBA, key string, colors []color.RGBA) { } } // SquaresSVG builds an image with 6 by 6 quadrants of alternate colors. func SquaresSVG(w http.ResponseWriter, key string, colors []color.RGBA, size int) { canvas := svg.New(w) canvas.Start(size, size) Loading