Commit 1d5c97e4 authored by santiaago's avatar santiaago
Browse files

refactor color extraction

Fixes #77
parent 97e3b0ad
Loading
Loading
Loading
Loading
+2 −51
Original line number Diff line number Diff line
package isogrids

import (
	"image/color"
	"net/http"

	tgColors "github.com/taironas/tinygraphs/colors"
	"github.com/taironas/tinygraphs/draw/isogrids"
	"github.com/taironas/tinygraphs/extract"
	"github.com/taironas/tinygraphs/write"
@@ -16,7 +14,7 @@ func BannerRandom(w http.ResponseWriter, r *http.Request) {
	width := extract.Width(r)
	height := extract.Height(r)

	colors := GetColors(r)
	colors := extract.Colors(r)
	prob := extract.Probability(r, 1/float64(len(colors)))

	xt := extract.XTriangles(r)
@@ -24,60 +22,13 @@ func BannerRandom(w http.ResponseWriter, r *http.Request) {
	isogrids.Random(w, "", colors, width, height, xt, prob)
}

func GetColors(r *http.Request) []color.RGBA {

	if newColors, err := extract.Colors(r); err == nil {
		return newColors
	}

	var colors []color.RGBA
	th := extract.Theme(r)

	if th == "base" {
		bg, fg := extract.ExtraColors(r)
		colors = append(colors, bg, fg)
	} else {
		m := tgColors.MapOfColorThemes()
		if _, ok := m[th]; ok {
			n := extract.NumColors(r)
			colors = append(colors, m[th][0:n]...)
		} else {
			colors = append(colors, m["base"]...)
		}
	}

	return colors
}

// BannerRandomGradinet handler for /isogrids/banner/random/gradient.
// Generates a random gradient banner isogrid image.
func BannerRandomGradient(w http.ResponseWriter, r *http.Request) {
	width := extract.Width(r)
	height := extract.Height(r)

	colorMap := tgColors.MapOfColorThemes()
	bg, fg := extract.ExtraColors(r)
	theme := extract.Theme(r)
	if val, ok := colorMap[theme]; ok {
		bg = val[0]
		fg = val[1]
	}

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

	if newColors, err := extract.Colors(r); err == nil {
		colors = newColors
	}
	colors := extract.Colors(r)

	xt := extract.XTriangles(r)
	write.ImageSVG(w)
+2 −30
Original line number Diff line number Diff line
package isogrids

import (
	"image/color"
	"net/http"

	"github.com/taironas/tinygraphs/colors"
	"github.com/taironas/tinygraphs/draw/isogrids"
	"github.com/taironas/tinygraphs/extract"
	"github.com/taironas/tinygraphs/write"
@@ -17,36 +15,10 @@ func BannerGradient(w http.ResponseWriter, r *http.Request) {
	width := extract.Width(r)
	height := extract.Height(r)
	xtriangles := extract.XTriangles(r)
	numColors := extract.NumColors(r)
	gv := extract.GradientVector(r, uint8(0), uint8(0), uint8(width), uint8(0))

	colorMap := colors.MapOfColorThemes()

	bg, fg := extract.ExtraColors(r)

	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}
	}

	if newColors, err := extract.Colors(r); err == nil {
		colors = newColors
		if len(colors) > 2 {
			gColors = colors[1:3]
		} else {
			gColors = colors
		}
	}
	colors := extract.Colors(r)
	gColors := extract.GColors(r)

	prob := extract.Probability(r, 1/float64(len(colors)))

+1 −26
Original line number Diff line number Diff line
@@ -3,12 +3,10 @@ package isogrids
import (
	"crypto/md5"
	"fmt"
	"image/color"
	"io"
	"net/http"

	"github.com/taironas/route"
	"github.com/taironas/tinygraphs/colors"
	"github.com/taironas/tinygraphs/draw/isogrids"
	"github.com/taironas/tinygraphs/extract"
	"github.com/taironas/tinygraphs/write"
@@ -18,7 +16,6 @@ import (
// builds an hexagon with alternate colors.
func Hexa(w http.ResponseWriter, r *http.Request) {
	var err error
	colorMap := colors.MapOfColorThemes()
	size := extract.Size(r)

	var key string
@@ -29,30 +26,8 @@ func Hexa(w http.ResponseWriter, r *http.Request) {
	io.WriteString(h, key)
	key = fmt.Sprintf("%x", h.Sum(nil)[:])

	numColors := extract.NumColors(r)
	lines := int(extract.Hexalines(r))
	bg, fg := extract.ExtraColors(r)

	theme := extract.Theme(r)
	if val, ok := colorMap[theme]; ok {
		bg = val[0]
		fg = val[1]
	}

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

	if newColors, err := extract.Colors(r); err == nil {
		colors = newColors
	}
	colors := extract.Colors(r)

	write.ImageSVG(w)
	isogrids.Hexa(w, key, colors, size, lines)
+1 −26
Original line number Diff line number Diff line
@@ -3,13 +3,11 @@ package isogrids
import (
	"crypto/md5"
	"fmt"
	"image/color"
	"io"
	"log"
	"net/http"

	"github.com/taironas/route"
	"github.com/taironas/tinygraphs/colors"
	"github.com/taironas/tinygraphs/draw/isogrids"
	"github.com/taironas/tinygraphs/extract"
	"github.com/taironas/tinygraphs/write"
@@ -29,30 +27,7 @@ func Isogrids(w http.ResponseWriter, r *http.Request) {
	io.WriteString(h, key)
	key = fmt.Sprintf("%x", h.Sum(nil)[:])

	colorMap := colors.MapOfColorThemes()
	bg, fg := extract.ExtraColors(r)
	theme := extract.Theme(r)
	if val, ok := colorMap[theme]; ok {
		bg = val[0]
		fg = val[1]
	}

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

	if newColors, err := extract.Colors(r); err == nil {
		colors = newColors
	}

	colors := extract.Colors(r)
	size := extract.Size(r)
	lines := extract.Lines(r)

+1 −26
Original line number Diff line number Diff line
package isogrids

import (
	"image/color"
	"net/http"

	"github.com/taironas/tinygraphs/colors"
	"github.com/taironas/tinygraphs/draw/isogrids"
	"github.com/taironas/tinygraphs/extract"
	"github.com/taironas/tinygraphs/write"
@@ -14,30 +12,7 @@ import (
// Generates a random isogrid image.
func Random(w http.ResponseWriter, r *http.Request) {

	colorMap := colors.MapOfColorThemes()
	bg, fg := extract.ExtraColors(r)
	theme := extract.Theme(r)
	if val, ok := colorMap[theme]; ok {
		bg = val[0]
		fg = val[1]
	}

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

	if newColors, err := extract.Colors(r); err == nil {
		colors = newColors
	}

	colors := extract.Colors(r)
	prob := extract.Probability(r, 1/float64(len(colors)))

	size := extract.Size(r)
Loading