Loading app-backend/draw.go 0 → 100644 +128 −0 Original line number Diff line number Diff line package main import ( "image" "image/color" "math" "math/rand" ) //drawGrid6X6 builds an image with 6X6 quadrants of alternate colors. func drawGrid6X6(m *image.RGBA, color1, color2 color.RGBA) { size := m.Bounds().Size() quad := size.X / 6 for x := 0; x < size.X; x++ { val := (x / quad) % 2 for y := 0; y < size.Y; y++ { val2 := (y / quad) % 2 q := (val + val2) % 2 if q == 0 { m.Set(x, y, color1) } else { m.Set(x, y, color2) } } } } // drawGradient builds an image with gradient colors. func drawGradient(m *image.RGBA) { size := m.Bounds().Size() for x := 0; x < size.X; x++ { for y := 0; y < size.Y; y++ { color := color.RGBA{ uint8(255 * x / size.X), uint8(255 * y / size.Y), 55, 255} m.Set(x, y, color) } } } // drawRandomGrid6X6 builds a grid image with with 2 colors selected at random for each quadrant. func drawRandomGrid6X6(m *image.RGBA, color1, color2 color.RGBA) { size := m.Bounds().Size() quad := size.X / 6 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] = getRandomColor(color1, color2) } m.Set(x, y, colorMap[yQuadrant]) } } } // getRandomColor returns a random color between c1 and c2 func getRandomColor(c1, c2 color.RGBA) color.RGBA { r := rand.Intn(2) if r == 1 { return c1 } return c2 } // drawRandomGrid6X6 builds a grid image with with 2 colors selected at random for each quadrant. func drawRandomSymetricInYGrid6X6(m *image.RGBA, color1, color2 color.RGBA) { size := m.Bounds().Size() squares := 6 quad := size.X / squares middle := math.Ceil(float64(squares) / float64(2)) colorMap := make(map[int]color.RGBA) var currentQuadrand = 0 for x := 0; x < size.X; x++ { if x/quad != currentQuadrand { // when x quadrant changes, 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 { if float64(yQuadrant) < middle { colorMap[yQuadrant] = getRandomColor(color1, color2) } else { colorMap[yQuadrant] = colorMap[squares-yQuadrant-1] //getRandomColor(color1, color2) } } m.Set(x, y, colorMap[yQuadrant]) } } } // drawRandomGrid6X6 builds a grid image with with 2 colors selected at random for each quadrant. func drawRandomSymetricInXGrid6X6(m *image.RGBA, color1, color2 color.RGBA) { size := m.Bounds().Size() squares := 6 quad := size.X / squares middle := math.Ceil(float64(squares) / float64(2)) colorMap := make(map[int]color.RGBA) var currentQuadrand = 0 for y := 0; y < size.Y; y++ { if y/quad != currentQuadrand { // when y quadrant changes, clear map colorMap = make(map[int]color.RGBA) currentQuadrand = y / quad } for x := 0; x < size.X; x++ { xQuadrant := x / quad if _, ok := colorMap[xQuadrant]; !ok { if float64(xQuadrant) < middle { colorMap[xQuadrant] = getRandomColor(color1, color2) } else { colorMap[xQuadrant] = colorMap[squares-xQuadrant-1] } } m.Set(x, y, colorMap[xQuadrant]) } } } app-backend/grid.go +0 −33 Original line number Diff line number Diff line Loading @@ -40,36 +40,3 @@ func gradientHandler(w http.ResponseWriter, r *http.Request) { var img image.Image = m writeImage(w, &img) } //drawGrid6X6 builds an image with 6X6 quadrants of alternate colors. func drawGrid6X6(m *image.RGBA, color1, color2 color.RGBA) { size := m.Bounds().Size() quad := size.X / 6 for x := 0; x < size.X; x++ { val := (x / quad) % 2 for y := 0; y < size.Y; y++ { val2 := (y / quad) % 2 q := (val + val2) % 2 if q == 0 { m.Set(x, y, color1) } else { m.Set(x, y, color2) } } } } // drawGradient builds an image with gradient colors. func drawGradient(m *image.RGBA) { size := m.Bounds().Size() for x := 0; x < size.X; x++ { for y := 0; y < size.Y; y++ { color := color.RGBA{ uint8(255 * x / size.X), uint8(255 * y / size.Y), 55, 255} m.Set(x, y, color) } } } app-backend/images.go +0 −44 Original line number Diff line number Diff line package main import ( "bytes" "encoding/base64" "html/template" "image" "image/color" "image/draw" "image/jpeg" "log" "net/http" "strconv" ) func blackHandler(w http.ResponseWriter, r *http.Request) { Loading Loading @@ -49,41 +43,3 @@ func redHandler(w http.ResponseWriter, r *http.Request) { var img image.Image = m writeImageWithTemplate(w, &img) } var ImageTemplate string = `<!DOCTYPE html> <html lang="en"><head><title>{{ .Title }}</title></head> <body><img src="data:image/jpg;base64,{{.Image}}"></body>` // writeImageWithTemplate encodes an image 'img' in jpeg format and writes it into ResponseWriter using a template. func writeImageWithTemplate(w http.ResponseWriter, img *image.Image) { buffer := new(bytes.Buffer) if err := jpeg.Encode(buffer, *img, nil); err != nil { log.Println("unable to encode image.") } str := base64.StdEncoding.EncodeToString(buffer.Bytes()) if tmpl, err := template.New("image").Parse(ImageTemplate); err != nil { log.Println("unable to parse image template.") } else { data := map[string]interface{}{"Image": str} if err = tmpl.Execute(w, data); err != nil { log.Println("unable to execute template.") } } } // writeImage encodes an image 'img' in jpeg format and writes it into ResponseWriter. func writeImage(w http.ResponseWriter, img *image.Image) { buffer := new(bytes.Buffer) if err := jpeg.Encode(buffer, *img, nil); err != nil { log.Fatalln("unable to encode image.") } w.Header().Set("Content-Type", "image/jpeg") w.Header().Set("Content-Length", strconv.Itoa(len(buffer.Bytes()))) if _, err := w.Write(buffer.Bytes()); err != nil { log.Println("unable to write image.") } } app-backend/main.go +1 −2 Original line number Diff line number Diff line Loading @@ -24,13 +24,12 @@ func main() { r.HandleFunc("/grid/random/?", gridRandomHandler) r.HandleFunc("/grid/random/[0-8]/?", gridRandomColorHandler) r.HandleFunc("/grid/random/symetric/?", gridRandomSymetricXHandler) r.HandleFunc("/grid/random/symetric/x/?", gridRandomSymetricXHandler) r.HandleFunc("/grid/random/symetric/x/[0-8]?", gridRandomSymetricXColorHandler) r.HandleFunc("/grid/random/symetric/y/?", gridRandomSymetricYHandler) r.HandleFunc("/grid/random/symetric/y/[0-8]?", gridRandomSymetricYColorHandler) r.HandleFunc("/grid/random/symetric/?", gridRandomSymetricXHandler) r.HandleFunc("/gradient/?", gradientHandler) r.AddStaticResource(root) Loading app-backend/randomgrid.go +0 −33 Original line number Diff line number Diff line Loading @@ -2,9 +2,7 @@ package main import ( "image" "image/color" "log" "math/rand" "net/http" ) Loading Loading @@ -32,34 +30,3 @@ func gridRandomColorHandler(w http.ResponseWriter, r *http.Request) { writeImage(w, &img) } } // drawRandomGrid6X6 builds a grid image with with 2 colors selected at random for each quadrant. func drawRandomGrid6X6(m *image.RGBA, color1, color2 color.RGBA) { size := m.Bounds().Size() quad := size.X / 6 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] = getRandomColor(color1, color2) } m.Set(x, y, colorMap[yQuadrant]) } } } // getRandomColor returns a random color between c1 and c2 func getRandomColor(c1, c2 color.RGBA) color.RGBA { r := rand.Intn(2) if r == 1 { return c1 } return c2 } Loading
app-backend/draw.go 0 → 100644 +128 −0 Original line number Diff line number Diff line package main import ( "image" "image/color" "math" "math/rand" ) //drawGrid6X6 builds an image with 6X6 quadrants of alternate colors. func drawGrid6X6(m *image.RGBA, color1, color2 color.RGBA) { size := m.Bounds().Size() quad := size.X / 6 for x := 0; x < size.X; x++ { val := (x / quad) % 2 for y := 0; y < size.Y; y++ { val2 := (y / quad) % 2 q := (val + val2) % 2 if q == 0 { m.Set(x, y, color1) } else { m.Set(x, y, color2) } } } } // drawGradient builds an image with gradient colors. func drawGradient(m *image.RGBA) { size := m.Bounds().Size() for x := 0; x < size.X; x++ { for y := 0; y < size.Y; y++ { color := color.RGBA{ uint8(255 * x / size.X), uint8(255 * y / size.Y), 55, 255} m.Set(x, y, color) } } } // drawRandomGrid6X6 builds a grid image with with 2 colors selected at random for each quadrant. func drawRandomGrid6X6(m *image.RGBA, color1, color2 color.RGBA) { size := m.Bounds().Size() quad := size.X / 6 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] = getRandomColor(color1, color2) } m.Set(x, y, colorMap[yQuadrant]) } } } // getRandomColor returns a random color between c1 and c2 func getRandomColor(c1, c2 color.RGBA) color.RGBA { r := rand.Intn(2) if r == 1 { return c1 } return c2 } // drawRandomGrid6X6 builds a grid image with with 2 colors selected at random for each quadrant. func drawRandomSymetricInYGrid6X6(m *image.RGBA, color1, color2 color.RGBA) { size := m.Bounds().Size() squares := 6 quad := size.X / squares middle := math.Ceil(float64(squares) / float64(2)) colorMap := make(map[int]color.RGBA) var currentQuadrand = 0 for x := 0; x < size.X; x++ { if x/quad != currentQuadrand { // when x quadrant changes, 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 { if float64(yQuadrant) < middle { colorMap[yQuadrant] = getRandomColor(color1, color2) } else { colorMap[yQuadrant] = colorMap[squares-yQuadrant-1] //getRandomColor(color1, color2) } } m.Set(x, y, colorMap[yQuadrant]) } } } // drawRandomGrid6X6 builds a grid image with with 2 colors selected at random for each quadrant. func drawRandomSymetricInXGrid6X6(m *image.RGBA, color1, color2 color.RGBA) { size := m.Bounds().Size() squares := 6 quad := size.X / squares middle := math.Ceil(float64(squares) / float64(2)) colorMap := make(map[int]color.RGBA) var currentQuadrand = 0 for y := 0; y < size.Y; y++ { if y/quad != currentQuadrand { // when y quadrant changes, clear map colorMap = make(map[int]color.RGBA) currentQuadrand = y / quad } for x := 0; x < size.X; x++ { xQuadrant := x / quad if _, ok := colorMap[xQuadrant]; !ok { if float64(xQuadrant) < middle { colorMap[xQuadrant] = getRandomColor(color1, color2) } else { colorMap[xQuadrant] = colorMap[squares-xQuadrant-1] } } m.Set(x, y, colorMap[xQuadrant]) } } }
app-backend/grid.go +0 −33 Original line number Diff line number Diff line Loading @@ -40,36 +40,3 @@ func gradientHandler(w http.ResponseWriter, r *http.Request) { var img image.Image = m writeImage(w, &img) } //drawGrid6X6 builds an image with 6X6 quadrants of alternate colors. func drawGrid6X6(m *image.RGBA, color1, color2 color.RGBA) { size := m.Bounds().Size() quad := size.X / 6 for x := 0; x < size.X; x++ { val := (x / quad) % 2 for y := 0; y < size.Y; y++ { val2 := (y / quad) % 2 q := (val + val2) % 2 if q == 0 { m.Set(x, y, color1) } else { m.Set(x, y, color2) } } } } // drawGradient builds an image with gradient colors. func drawGradient(m *image.RGBA) { size := m.Bounds().Size() for x := 0; x < size.X; x++ { for y := 0; y < size.Y; y++ { color := color.RGBA{ uint8(255 * x / size.X), uint8(255 * y / size.Y), 55, 255} m.Set(x, y, color) } } }
app-backend/images.go +0 −44 Original line number Diff line number Diff line package main import ( "bytes" "encoding/base64" "html/template" "image" "image/color" "image/draw" "image/jpeg" "log" "net/http" "strconv" ) func blackHandler(w http.ResponseWriter, r *http.Request) { Loading Loading @@ -49,41 +43,3 @@ func redHandler(w http.ResponseWriter, r *http.Request) { var img image.Image = m writeImageWithTemplate(w, &img) } var ImageTemplate string = `<!DOCTYPE html> <html lang="en"><head><title>{{ .Title }}</title></head> <body><img src="data:image/jpg;base64,{{.Image}}"></body>` // writeImageWithTemplate encodes an image 'img' in jpeg format and writes it into ResponseWriter using a template. func writeImageWithTemplate(w http.ResponseWriter, img *image.Image) { buffer := new(bytes.Buffer) if err := jpeg.Encode(buffer, *img, nil); err != nil { log.Println("unable to encode image.") } str := base64.StdEncoding.EncodeToString(buffer.Bytes()) if tmpl, err := template.New("image").Parse(ImageTemplate); err != nil { log.Println("unable to parse image template.") } else { data := map[string]interface{}{"Image": str} if err = tmpl.Execute(w, data); err != nil { log.Println("unable to execute template.") } } } // writeImage encodes an image 'img' in jpeg format and writes it into ResponseWriter. func writeImage(w http.ResponseWriter, img *image.Image) { buffer := new(bytes.Buffer) if err := jpeg.Encode(buffer, *img, nil); err != nil { log.Fatalln("unable to encode image.") } w.Header().Set("Content-Type", "image/jpeg") w.Header().Set("Content-Length", strconv.Itoa(len(buffer.Bytes()))) if _, err := w.Write(buffer.Bytes()); err != nil { log.Println("unable to write image.") } }
app-backend/main.go +1 −2 Original line number Diff line number Diff line Loading @@ -24,13 +24,12 @@ func main() { r.HandleFunc("/grid/random/?", gridRandomHandler) r.HandleFunc("/grid/random/[0-8]/?", gridRandomColorHandler) r.HandleFunc("/grid/random/symetric/?", gridRandomSymetricXHandler) r.HandleFunc("/grid/random/symetric/x/?", gridRandomSymetricXHandler) r.HandleFunc("/grid/random/symetric/x/[0-8]?", gridRandomSymetricXColorHandler) r.HandleFunc("/grid/random/symetric/y/?", gridRandomSymetricYHandler) r.HandleFunc("/grid/random/symetric/y/[0-8]?", gridRandomSymetricYColorHandler) r.HandleFunc("/grid/random/symetric/?", gridRandomSymetricXHandler) r.HandleFunc("/gradient/?", gradientHandler) r.AddStaticResource(root) Loading
app-backend/randomgrid.go +0 −33 Original line number Diff line number Diff line Loading @@ -2,9 +2,7 @@ package main import ( "image" "image/color" "log" "math/rand" "net/http" ) Loading Loading @@ -32,34 +30,3 @@ func gridRandomColorHandler(w http.ResponseWriter, r *http.Request) { writeImage(w, &img) } } // drawRandomGrid6X6 builds a grid image with with 2 colors selected at random for each quadrant. func drawRandomGrid6X6(m *image.RGBA, color1, color2 color.RGBA) { size := m.Bounds().Size() quad := size.X / 6 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] = getRandomColor(color1, color2) } m.Set(x, y, colorMap[yQuadrant]) } } } // getRandomColor returns a random color between c1 and c2 func getRandomColor(c1, c2 color.RGBA) color.RGBA { r := rand.Intn(2) if r == 1 { return c1 } return c2 }