Commit 865368ca authored by santiaago's avatar santiaago
Browse files

making room for svg, we might move it to its own pattern at some point ...

parent 757bc3fb
Loading
Loading
Loading
Loading
+2 −2
Original line number Diff line number Diff line
@@ -24,7 +24,7 @@ func Color(w http.ResponseWriter, r *http.Request) {
		colorMap := colors.MapOfColorPatterns()
		draw.Grid6X6(m, colorMap[int(intID)][0], colorMap[int(intID)][1])
		var img image.Image = m
		write.Image(w, &img)
		write.ImageJPEG(w, &img)
	}
}

@@ -37,7 +37,7 @@ func Checkerboard(w http.ResponseWriter, r *http.Request) {
	color2 := color.RGBA{uint8(0), uint8(0), 0, 255}
	draw.Grid6X6(m, color1, color2)
	var img image.Image = m
	write.Image(w, &img)
	write.ImageJPEG(w, &img)
}

// extract size from HTTP request and return it.
+2 −2
Original line number Diff line number Diff line
@@ -26,7 +26,7 @@ func Random(w http.ResponseWriter, r *http.Request) {
	m := image.NewRGBA(image.Rect(0, 0, size, size))
	draw.RandomGrid6X6(m, bg, fg)
	var img image.Image = m
	write.Image(w, &img)
	write.ImageJPEG(w, &img)
}

// handler for "/grid/random/[0-9]"
@@ -41,6 +41,6 @@ func RandomColor(w http.ResponseWriter, r *http.Request) {
		colorMap := colors.MapOfColorPatterns()
		draw.RandomGrid6X6(m, colorMap[int(intID)][0], colorMap[int(intID)][1])
		var img image.Image = m
		write.Image(w, &img)
		write.ImageJPEG(w, &img)
	}
}
+4 −5
Original line number Diff line number Diff line
@@ -53,12 +53,11 @@ func Square(w http.ResponseWriter, r *http.Request) {
			m := image.NewRGBA(image.Rect(0, 0, size, size))
			draw.Squares(m, key, bg, fg)
			var img image.Image = m
			write.Image(w, &img)
			write.ImageJPEG(w, &img)
		} else if format == SVG {
			canvas := svg.New(w)
			canvas.Start(size, size)
			canvas.Rect(0, 0, size, size, "fill:rgb(0,0,255);")
			canvas.End()
			draw.SquaresSVG(canvas, key, bg, fg, size)
			write.ImageSVG(w, canvas)
		}
	}
}
@@ -94,7 +93,7 @@ func Color(w http.ResponseWriter, r *http.Request) {

			draw.Squares(m, key, colorMap[int(colorId)][0], colorMap[int(colorId)][1])
			var img image.Image = m
			write.Image(w, &img)
			write.ImageJPEG(w, &img)
		} else {
			log.Printf("error when extracting permalink string: %v", err)
		}
+34 −15
Original line number Diff line number Diff line
@@ -2,6 +2,7 @@ package draw

import (
	"encoding/hex"
	"github.com/ajstarks/svgo"
	"image"
	"image/color"
	"log"
@@ -28,21 +29,6 @@ func Grid6X6(m *image.RGBA, color1, color2 color.RGBA) {
	}
}

// drawGradient builds an image with gradient colors.
func Gradient(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 RandomGrid6X6(m *image.RGBA, color1, color2 color.RGBA) {
	size := m.Bounds().Size()
@@ -160,6 +146,39 @@ func Squares(m *image.RGBA, key string, color1, color2 color.RGBA) {
	}
}

func SquaresSVG(canvas *svg.SVG, key string, color1, color2 color.RGBA, size int) {
	canvas.Start(size, size)
	canvas.Rect(0, 0, size, size, "fill:rgb(0,0,255);")

	// size := m.Bounds().Size()
	// squares := 6
	// quad := size.X / squares
	// middle := math.Ceil(float64(squares) / float64(2))
	// colorMap := make(map[int]color.RGBA)
	// var currentYQuadrand = 0
	// for y := 0; y < size.Y; y++ {
	// 	yQuadrant := y / quad
	// 	if yQuadrant != currentYQuadrand {
	// 		// when y quadrant changes, clear map
	// 		colorMap = make(map[int]color.RGBA)
	// 		currentYQuadrand = yQuadrant
	// 	}
	// 	for x := 0; x < size.X; x++ {
	// 		xQuadrant := x / quad
	// 		if _, ok := colorMap[xQuadrant]; !ok {
	// 			if float64(xQuadrant) < middle {
	// 				colorMap[xQuadrant] = colorFromKey(key, color1, color2, xQuadrant+3*yQuadrant)
	// 			} else if xQuadrant < squares {
	// 				colorMap[xQuadrant] = colorMap[squares-xQuadrant-1]
	// 			} else {
	// 				colorMap[xQuadrant] = colorMap[0]
	// 			}
	// 		}
	// 		m.Set(x, y, colorMap[xQuadrant])
	// 	}
	// }
}

func colorFromKey(key string, color1, color2 color.RGBA, index int) color.RGBA {
	s := hex.EncodeToString([]byte{key[index]})
	if r, err := strconv.ParseInt(s, 16, 0); err == nil {
+9 −1
Original line number Diff line number Diff line
@@ -3,6 +3,7 @@ package write
import (
	"bytes"
	"encoding/base64"
	"github.com/ajstarks/svgo"
	"html/template"
	"image"
	"image/jpeg"
@@ -35,7 +36,7 @@ func ImageWithTemplate(w http.ResponseWriter, img *image.Image) {
}

// writeImage encodes an image 'img' in jpeg format and writes it into ResponseWriter.
func Image(w http.ResponseWriter, img *image.Image) {
func ImageJPEG(w http.ResponseWriter, img *image.Image) {

	buffer := new(bytes.Buffer)
	if err := jpeg.Encode(buffer, *img, nil); err != nil {
@@ -48,3 +49,10 @@ func Image(w http.ResponseWriter, img *image.Image) {
		log.Println("unable to write image.")
	}
}

// writeImage encodes an image 'img' in jpeg format and writes it into ResponseWriter.
func ImageSVG(w http.ResponseWriter, img *svg.SVG) {

	w.Header().Set("Content-Type", "image/svg+xml")
	img.End()
}