Commit 4c97755b authored by santiaago's avatar santiaago
Browse files

refactor getColors #77

parent 8c506dd8
Loading
Loading
Loading
Loading
+15 −18
Original line number Diff line number Diff line
@@ -4,7 +4,7 @@ import (
	"image/color"
	"net/http"

	"github.com/taironas/tinygraphs/colors"
	tgColors "github.com/taironas/tinygraphs/colors"
	"github.com/taironas/tinygraphs/draw/isogrids"
	"github.com/taironas/tinygraphs/extract"
	"github.com/taironas/tinygraphs/write"
@@ -26,29 +26,26 @@ func BannerRandom(w http.ResponseWriter, r *http.Request) {

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

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

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

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

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

	return colors
}

@@ -58,7 +55,7 @@ func BannerRandomGradient(w http.ResponseWriter, r *http.Request) {
	width := extract.Width(r)
	height := extract.Height(r)

	colorMap := colors.MapOfColorThemes()
	colorMap := tgColors.MapOfColorThemes()
	bg, fg := extract.ExtraColors(r)
	theme := extract.Theme(r)
	if val, ok := colorMap[theme]; ok {
+14 −14
Original line number Diff line number Diff line
@@ -32,6 +32,20 @@ func Foreground(req *http.Request) (color.RGBA, error) {
	return color.RGBA{uint8(r), uint8(g), uint8(b), uint8(255)}, err
}

// ExtraColors returns a background and foreground color.RGBA is specified.
// returns black and white otherwise
func ExtraColors(req *http.Request) (color.RGBA, color.RGBA) {
	var err error
	var bg, fg color.RGBA
	if bg, err = Background(req); err != nil {
		fg = colors.White()
	}
	if fg, err = Foreground(req); err != nil {
		bg = colors.Black()
	}
	return bg, fg
}

// Colors extract an array of hexadecimal colors and returns an array of color.RGBA
func Colors(req *http.Request) ([]color.RGBA, error) {
	if err := req.ParseForm(); err != nil {
@@ -55,20 +69,6 @@ func Colors(req *http.Request) ([]color.RGBA, error) {
	return colors, nil
}

// ExtraColors returns a background and foreground color.RGBA is specified.
// returns black and white otherwise
func ExtraColors(req *http.Request) (color.RGBA, color.RGBA) {
	var err error
	var bg, fg color.RGBA
	if bg, err = Background(req); err != nil {
		bg = colors.Black()
	}
	if fg, err = Foreground(req); err != nil {
		fg = colors.White()
	}
	return bg, fg
}

// Size returns the value of size param from HTTP request.
// Default value: 240
func Size(r *http.Request) int {