Commit 863516d3 authored by santiaago's avatar santiaago
Browse files

you can now app hexa code colors to urls.

This is now supported for the following urls:
*/grid/random?bg=ff4008&fg=04d6f2
*/grid/random/symetric?bg=ff4008&fg=04d6f2
*/grid/random/symetric/x?bg=ff4008&fg=04d6f2
*/grid/random/symetric/y?bg=ff4008&fg=04d6f2

Right now only bg and fg are supported, if needed we might change this to support more than 2 colors.
Fixes #16
parent 5daa204c
Loading
Loading
Loading
Loading
+10 −2
Original line number Diff line number Diff line
@@ -14,9 +14,17 @@ import (
// generates a black and white grid random image.
func Random(w http.ResponseWriter, r *http.Request) {
	size := size(r)
	m := image.NewRGBA(image.Rect(0, 0, size, size))
	colorMap := colors.MapOfColorPatterns()
	draw.RandomGrid6X6(m, colorMap[0][0], colorMap[0][1])
	bg, err1 := background(r)
	if err1 != nil {
		bg = colorMap[0][0]
	}
	fg, err2 := foreground(r)
	if err2 != nil {
		fg = colorMap[0][1]
	}
	m := image.NewRGBA(image.Rect(0, 0, size, size))
	draw.RandomGrid6X6(m, bg, fg)
	var img image.Image = m
	write.Image(w, &img)
}
+49 −4
Original line number Diff line number Diff line
@@ -3,11 +3,12 @@ package grid
import (
	"crypto/md5"
	"fmt"
	"github.com/taironas/tinygraphs/colors"
	tgColors "github.com/taironas/tinygraphs/colors"
	"github.com/taironas/tinygraphs/draw"
	"github.com/taironas/tinygraphs/misc"
	"github.com/taironas/tinygraphs/write"
	"image"
	"image/color"
	"io"
	"log"
	"net/http"
@@ -21,13 +22,21 @@ func Square(w http.ResponseWriter, r *http.Request) {
	if err != nil {
		log.Printf("error when extracting permalink id: %v", err)
	} else {
		colorMap := tgColors.MapOfColorPatterns()
		bg, err1 := background(r)
		if err1 != nil {
			bg = colorMap[0][0]
		}
		fg, err2 := foreground(r)
		if err2 != nil {
			fg = colorMap[0][1]
		}
		size := size(r)
		m := image.NewRGBA(image.Rect(0, 0, size, size))
		colorMap := colors.MapOfColorPatterns()
		h := md5.New()
		io.WriteString(h, id)
		key := fmt.Sprintf("%x", h.Sum(nil)[:])
		draw.Square(m, key, colorMap[0][0], colorMap[0][1])
		draw.Square(m, key, bg, fg)
		var img image.Image = m
		write.Image(w, &img)
	}
@@ -43,7 +52,7 @@ func SquareColor(w http.ResponseWriter, r *http.Request) {
		if id, err1 := misc.PermalinkString(r, 4); err1 == nil {
			size := size(r)
			m := image.NewRGBA(image.Rect(0, 0, size, size))
			colorMap := colors.MapOfColorPatterns()
			colorMap := tgColors.MapOfColorPatterns()
			h := md5.New()
			io.WriteString(h, id)
			key := fmt.Sprintf("%x", h.Sum(nil)[:])
@@ -56,6 +65,42 @@ func SquareColor(w http.ResponseWriter, r *http.Request) {
	}
}

func background(req *http.Request) (color.RGBA, error) {
	bg := req.FormValue("bg")
	if len(bg) == 0 {
		return color.RGBA{}, fmt.Errorf("background: wrong input")
	}
	r, g, b, err := hexToRGB(bg)
	return color.RGBA{uint8(r), uint8(g), uint8(b), uint8(255)}, err
}

func foreground(req *http.Request) (color.RGBA, error) {
	fg := req.FormValue("fg")
	if len(fg) == 0 {
		return color.RGBA{}, fmt.Errorf("background: wrong input")
	}
	r, g, b, err := hexToRGB(fg)
	return color.RGBA{uint8(r), uint8(g), uint8(b), uint8(255)}, err
}

// HexToRGB converts an Hex string to a RGB triple.
func hexToRGB(h string) (uint8, uint8, uint8, error) {
	if len(h) > 0 && h[0] == '#' {
		h = h[1:]
	}
	if len(h) == 3 {
		h = h[:1] + h[:1] + h[1:2] + h[1:2] + h[2:] + h[2:]
	}
	if len(h) == 6 {
		if rgb, err := strconv.ParseUint(string(h), 16, 32); err == nil {
			return uint8(rgb >> 16), uint8((rgb >> 8) & 0xFF), uint8(rgb & 0xFF), nil
		} else {
			return 0, 0, 0, err
		}
	}
	return 0, 0, 0, nil
}

func size(r *http.Request) int {
	strSize := r.FormValue("size")
	if len(strSize) > 0 {
+20 −4
Original line number Diff line number Diff line
@@ -15,9 +15,17 @@ import (
// generates a black and white grid random image.
func RandomSymetricX(w http.ResponseWriter, r *http.Request) {
	size := size(r)
	m := image.NewRGBA(image.Rect(0, 0, size, size))
	colorMap := colors.MapOfColorPatterns()
	draw.RandomSymetricInXGrid6X6(m, colorMap[0][0], colorMap[0][1])
	bg, err1 := background(r)
	if err1 != nil {
		bg = colorMap[0][0]
	}
	fg, err2 := foreground(r)
	if err2 != nil {
		fg = colorMap[0][1]
	}
	m := image.NewRGBA(image.Rect(0, 0, size, size))
	draw.RandomSymetricInXGrid6X6(m, bg, fg)
	var img image.Image = m
	write.Image(w, &img)
}
@@ -26,9 +34,17 @@ func RandomSymetricX(w http.ResponseWriter, r *http.Request) {
// generates a black and white grid random image.
func RandomSymetricY(w http.ResponseWriter, r *http.Request) {
	size := size(r)
	m := image.NewRGBA(image.Rect(0, 0, size, size))
	colorMap := colors.MapOfColorPatterns()
	draw.RandomSymetricInYGrid6X6(m, colorMap[0][0], colorMap[0][1])
	bg, err1 := background(r)
	if err1 != nil {
		bg = colorMap[0][0]
	}
	fg, err2 := foreground(r)
	if err2 != nil {
		fg = colorMap[0][1]
	}
	m := image.NewRGBA(image.Rect(0, 0, size, size))
	draw.RandomSymetricInYGrid6X6(m, bg, fg)
	var img image.Image = m
	write.Image(w, &img)
}