Commit 75b0a9bb authored by santiaago's avatar santiaago
Browse files

add randomGradient svg

parent d1472d31
Loading
Loading
Loading
Loading
+2 −1
Original line number Diff line number Diff line
@@ -26,7 +26,6 @@ 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/gradient", squares.Gradient)
	r.HandleFunc("/squares/:key", squares.Square) //cached

	r.HandleFunc("/isogrids/banner/random", isogrids.BannerRandom)
@@ -38,6 +37,8 @@ func main() {
	r.HandleFunc("/themes/:theme", themes.Theme)
	r.HandleFunc("/labs/checkerboard", checkerboard.Checkerboard)
	r.HandleFunc("/labs/squares/random", squares.Random)
	r.HandleFunc("/labs/squares/gradient/:key", squares.Gradient)
	r.HandleFunc("/labs/squares/banner/gradient", squares.BannerGradient)
	r.HandleFunc("/labs/isogrids/hexa", isogrids.Hexa)
	r.HandleFunc("/labs/isogrids/hexa/:key", isogrids.Hexa)
	r.HandleFunc("/labs/isogrids/skeleton", isogrids.Skeleton)
+34 −2
Original line number Diff line number Diff line
@@ -15,7 +15,7 @@ import (
	"github.com/taironas/tinygraphs/write"
)

// Gradient handler for "/squares/gradient/:key"
// Gradient handler for "/labs/squares/gradient/:key"
// generates a color gradient random grid image.
func Gradient(w http.ResponseWriter, r *http.Request) {
	var err error
@@ -49,5 +49,37 @@ func Gradient(w http.ResponseWriter, r *http.Request) {

	size := extract.Size(r)
	write.ImageSVG(w)
	squares.GradientSVG(w, key, colors, size)
	squares.GradientSVG(w, key, colors, size, size, 6)
}

// Gradient handler for "labs/squares/banner/gradient"
// generates a color gradient random grid image.
func BannerGradient(w http.ResponseWriter, r *http.Request) {
	width := extract.Width(r)
	height := extract.Height(r)
	xsquares := extract.XSquares(r)

	numColors := extract.NumColors(r)

	colorMap := colors.MapOfColorThemes()

	bg, fg := extract.ExtraColors(r, colorMap)

	var colors, gColors []color.RGBA
	theme := extract.Theme(r)
	if theme != "base" {
		if _, ok := colorMap[theme]; ok {
			colors = append(colors, colorMap[theme][0:numColors]...)
			gColors = colorMap[theme][1:3]
		} else {
			colors = append(colors, colorMap["base"]...)
			gColors = colorMap[theme]
		}
	} else {
		colors = append(colors, bg, fg)
		gColors = []color.RGBA{bg, fg}
	}

	write.ImageSVG(w)
	squares.RandomGradientSVG(w, colors, gColors, width, height, xsquares)
}
+62 −6
Original line number Diff line number Diff line
@@ -11,7 +11,7 @@ import (
)

// GradientSVG builds an image with 6 by 6 quadrants of alternate colors.
func GradientSVG(w http.ResponseWriter, key string, colors []color.RGBA, size int) {
func GradientSVG(w http.ResponseWriter, key string, colors []color.RGBA, width, height, xsquares int) {

	var gradientColors []svg.Offcolor
	gradientColors = make([]svg.Offcolor, len(colors))
@@ -24,14 +24,14 @@ func GradientSVG(w http.ResponseWriter, key string, colors []color.RGBA, size in
	}

	canvas := svg.New(w)
	canvas.Start(size, size)
	canvas.Start(width, height)
	canvas.Def()
	canvas.LinearGradient("gradientColors", 0, 0, uint8(size), 0, gradientColors)
	canvas.LinearGradient("gradientColors", 0, 0, uint8(width), 0, gradientColors)
	canvas.DefEnd()
	canvas.Rect(0, 0, size, size, "fill:url(#gradientColors)")
	canvas.Rect(0, 0, width, height, "fill:url(#gradientColors)")

	squares := 6
	quadrantSize := size / squares
	squares := xsquares
	quadrantSize := width / squares
	middle := math.Ceil(float64(squares) / float64(2))
	colorMap := make(map[int]color.RGBA)
	colorIndex := make(map[int]int)
@@ -66,6 +66,62 @@ func GradientSVG(w http.ResponseWriter, key string, colors []color.RGBA, size in
	canvas.End()
}

// RandomGradientSVG builds an image.
func RandomGradientSVG(w http.ResponseWriter, colors, gColors []color.RGBA, width, height, xsquares int) {

	var gradientColors []svg.Offcolor
	gradientColors = make([]svg.Offcolor, len(gColors))
	percentage := uint8(100 / len(gColors))

	step := uint8(100 / len(gColors))
	for i, c := range gColors {
		gradientColors[i] = svg.Offcolor{percentage, RGBToHex(c.R, c.G, c.B), 1}
		percentage += step
	}

	canvas := svg.New(w)
	canvas.Start(width, height)
	canvas.Def()
	canvas.LinearGradient("gradientColors", 0, 0, uint8(width), 0, gradientColors)
	canvas.DefEnd()
	canvas.Rect(0, 0, width, height, "fill:url(#gradientColors)")

	squares := xsquares
	quadrantSize := width / squares
	middle := math.Ceil(float64(squares) / float64(2))
	colorMap := make(map[int]color.RGBA)
	colorIndex := make(map[int]int)
	for yQ := 0; yQ < squares; yQ++ {
		y := yQ * quadrantSize
		colorMap = make(map[int]color.RGBA)
		colorIndex = make(map[int]int)
		for xQ := 0; xQ < squares; xQ++ {
			x := xQ * quadrantSize
			fill := ""
			if _, ok := colorMap[xQ]; !ok {
				if float64(xQ) < middle {
					colorIndex[xQ] = draw.RandomIndexFromArray(colors)
					colorMap[xQ] = colors[colorIndex[xQ]]
				} else if xQ < squares {
					colorIndex[xQ] = colorIndex[squares-xQ-1]
					colorMap[xQ] = colorMap[squares-xQ-1]
				} else {
					colorIndex[xQ] = colorIndex[0]
					colorMap[xQ] = colorMap[0]
				}
			}
			if colorIndex[xQ] != 0 {
				fill = "fill:none"
			} else {
				fill = draw.FillFromRGBA(colorMap[xQ])

			}
			canvas.Rect(x, y, quadrantSize, quadrantSize, fill)
		}
	}
	canvas.End()
}

// RGBToHex converts an RGB triple to an Hex string.
func RGBToHex(r, g, b uint8) string {
	return fmt.Sprintf("#%02X%02X%02X", r, g, b)