From e26d84a9dd04a068ad7c682524bef1ec8e121551 Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Sun, 19 Jun 2022 12:21:08 -0400 Subject: [PATCH] separate out into packages to avoid unnecessary dependencies --- bin/main.go | 8 ++--- common/common.go | 56 +++++++++++++++++++++++++++++++++++ graph/graph.go => svg/svg.go | 57 ++++-------------------------------- 3 files changed, 66 insertions(+), 55 deletions(-) create mode 100644 common/common.go rename graph/graph.go => svg/svg.go (67%) diff --git a/bin/main.go b/bin/main.go index 3f8b474..8144dfc 100644 --- a/bin/main.go +++ b/bin/main.go @@ -6,7 +6,7 @@ import ( "time" "github.com/gorilla/mux" - "github.com/taigrr/gitgraph/graph" + "github.com/taigrr/gitgraph/svg" ) type DayCount [366]int @@ -22,7 +22,7 @@ func main() { freq = append(freq, rand.Int()) } w.Header().Add("Content-Type", "text/html") - svg := graph.GetWeekSVG(freq) + svg := svg.GetWeekSVG(freq) svg.WriteTo(w) }) @@ -31,11 +31,11 @@ func main() { for i := 0; i < 365; i++ { freq = append(freq, rand.Int()) } - svg := graph.GetYearSVG(freq) + svg := svg.GetYearSVG(freq) w.Header().Add("Content-Type", "text/html") svg.WriteTo(w) }) - http.ListenAndServe("0.0.0.0:5578", r) + http.ListenAndServe(":8080", r) } diff --git a/common/common.go b/common/common.go new file mode 100644 index 0000000..d0b0634 --- /dev/null +++ b/common/common.go @@ -0,0 +1,56 @@ +package common + +import ( + "bytes" + "image/color" + "sync" + + sc "github.com/taigrr/go-colorpallettes/simplecolor" +) + +var colorsLoaded sync.Once +var colorScheme []sc.SimpleColor + +func CreateGraph() bytes.Buffer { + var x bytes.Buffer + return x +} + +func init() { + colors := []string{"#767960", "#a7297f", "#e8ca89", "#f5efd6", "#858966"} + // colors = []string{"#000000", "#0e4429", "#006d32", "#26a641", "#39d353"} + for _, c := range colors { + color := sc.FromHexString(c) + colorScheme = append(colorScheme, color) + } +} + +func SetColorScheme(c []color.Color) { + for _, c := range c { + colorScheme = append(colorScheme, sc.FromRGBA(c.RGBA())) + } +} + +func ColorForFrequency(freq, min, max int) sc.SimpleColor { + spread := max - min + interval := float64(spread) / float64(len(colorScheme)) + colorIndex := 0 + for i := float64(min); i < float64(freq); i += float64(interval) { + colorIndex++ + } + if colorIndex > len(colorScheme)-1 { + colorIndex = len(colorScheme) - 1 + } + return colorScheme[colorIndex] +} +func MinMax(f []int) (int, int) { + min, max := 0, 0 + for _, x := range f { + if x < min { + min = x + } else if x > max { + max = x + } + } + return min, max +} diff --git a/graph/graph.go b/svg/svg.go similarity index 67% rename from graph/graph.go rename to svg/svg.go index 090c54c..76822cd 100644 --- a/graph/graph.go +++ b/svg/svg.go @@ -1,11 +1,10 @@ -package graph +package svg import ( "bufio" "bytes" "fmt" "image" - "image/color" "image/png" "os" "sync" @@ -13,49 +12,18 @@ import ( svg "github.com/ajstarks/svgo" "github.com/srwiley/oksvg" "github.com/srwiley/rasterx" + "github.com/taigrr/gitgraph/common" sc "github.com/taigrr/go-colorpallettes/simplecolor" ) var colorsLoaded sync.Once var colorScheme []sc.SimpleColor -func CreateGraph() bytes.Buffer { - var x bytes.Buffer - return x -} - -func init() { - colors := []string{"#767960", "#a7297f", "#e8ca89", "#f5efd6", "#858966"} - // colors = []string{"#000000", "#0e4429", "#006d32", "#26a641", "#39d353"} - for _, c := range colors { - color := sc.FromHexString(c) - colorScheme = append(colorScheme, color) - } -} - -func SetColorScheme(c []color.Color) { - for _, c := range c { - colorScheme = append(colorScheme, sc.FromRGBA(c.RGBA())) - } -} - -func ColorForFrequency(freq, min, max int) sc.SimpleColor { - spread := max - min - interval := float64(spread) / float64(len(colorScheme)) - colorIndex := 0 - for i := float64(min); i < float64(freq); i += float64(interval) { - colorIndex++ - } - if colorIndex > len(colorScheme)-1 { - colorIndex = len(colorScheme) - 1 - } - return colorScheme[colorIndex] -} func GetWeekSVG(frequencies []int) bytes.Buffer { squareColors := []sc.SimpleColor{} - min, max := minmax(frequencies) + min, max := common.MinMax(frequencies) for _, f := range frequencies { - squareColors = append(squareColors, ColorForFrequency(f, min, max)) + squareColors = append(squareColors, common.ColorForFrequency(f, min, max)) } return drawWeekImage(squareColors) } @@ -78,9 +46,9 @@ func drawWeekImage(c []sc.SimpleColor) bytes.Buffer { } func GetYearSVG(frequencies []int) bytes.Buffer { squareColors := []sc.SimpleColor{} - min, max := minmax(frequencies) + min, max := common.MinMax(frequencies) for _, f := range frequencies { - squareColors = append(squareColors, ColorForFrequency(f, min, max)) + squareColors = append(squareColors, common.ColorForFrequency(f, min, max)) } return drawYearImage(squareColors) } @@ -104,19 +72,6 @@ func drawYearImage(c []sc.SimpleColor) bytes.Buffer { sbw.Flush() return sb } - -func minmax(f []int) (int, int) { - min, max := 0, 0 - for _, x := range f { - if x < min { - min = x - } else if x > max { - max = x - } - } - return min, max -} - func svgToPng() { w, h := 512, 512