Commit 622635eb authored by santiaago's avatar santiaago
Browse files

add color handler

parent 985de4fe
Loading
Loading
Loading
Loading
+1 −0
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ func main() {
	r.HandleFunc("/isogrids/skeleton/[a-zA-Z0-9]+/?", isogrids.Skeleton)
	r.HandleFunc("/isogrids/diagonals/[a-zA-Z0-9]+/?", isogrids.Diagonals)
	r.HandleFunc("/isogrids/halfdiagonals/[a-zA-Z0-9]+/?", isogrids.HalfDiagonals)
	r.HandleFunc("/isogrids/color/[a-zA-Z0-9]+/?", isogrids.Color)
	r.HandleFunc("/isogrids/[a-zA-Z0-9]+/?", isogrids.Isogrids)

	r.AddStaticResource(root)
+24 −0
Original line number Diff line number Diff line
@@ -113,3 +113,27 @@ func HalfDiagonals(w http.ResponseWriter, r *http.Request) {
		draw.HalfDiagonals(w, key, bg, fg, size)
	}
}

func Color(w http.ResponseWriter, r *http.Request) {

	if id, err := misc.PermalinkString(r, 3); err != nil {
		log.Printf("error when extracting permalink id: %v", err)
	} else {
		h := md5.New()
		io.WriteString(h, id)
		key := fmt.Sprintf("%x", h.Sum(nil)[:])

		colorMap := tgColors.MapOfColorPatterns()
		bg, err1 := extract.Background(r)
		if err1 != nil {
			bg = colorMap[0][0]
		}
		fg, err2 := extract.Foreground(r)
		if err2 != nil {
			fg = colorMap[0][1]
		}
		size := extract.Size(r)
		write.ImageSVG(w)
		draw.IsogridsColor(w, key, bg, fg, size)
	}
}
+47 −0
Original line number Diff line number Diff line
@@ -134,3 +134,50 @@ func IsogridsSkeleton(w http.ResponseWriter, key string, color1, color2 color.RG

	canvas.End()
}

// Isogrids builds an image with 10x10 grids of half diagonals
func IsogridsColor(w http.ResponseWriter, key string, color1, color2 color.RGBA, size int) {
	canvas := svg.New(w)
	size = 400
	canvas.Start(size, size)

	lines := 10
	fringeSize := size / lines

	// vertical lines
	for xL := 0; xL <= lines; xL++ {
		x := xL * fringeSize
		firstY := 0
		lastY := (lines) * fringeSize
		style := fmt.Sprintf("stroke:black;stroke-width:2; %s", fillFromRGBA(color2))
		if (xL % 2) != 0 {
			lastY = lastY - fringeSize/2
			firstY = fringeSize / 2
		}
		canvas.Line(x, firstY, x, lastY, style)
	}

	// y -- > x up right
	for xL := 0; xL <= 2*lines; xL++ {
		x := xL * fringeSize
		style := fmt.Sprintf("stroke:black;stroke-width:2; %s", fillFromRGBA(color2))
		xPrev := 0
		yPrev := (xL) * fringeSize
		if yPrev > 0 {
			canvas.Line(xPrev, yPrev, x*2, 0, style)
		}
	}
	// x --> y down right
	for xL := -2 * lines; xL <= 2*lines; xL++ {
		x := xL * fringeSize * 2
		style := fmt.Sprintf("stroke:black;stroke-width:2; %s", fillFromRGBA(color2))
		xPrev := lines * fringeSize
		yPrev := (lines - xL*2) * fringeSize
		if yPrev > 0 {
			canvas.Line(x, 0, xPrev, yPrev/2, style)
		}
	}

	// color here:
	canvas.End()
}