separate out into packages to avoid unnecessary dependencies

This commit is contained in:
2022-06-19 12:21:08 -04:00
parent 4ec06a5994
commit e26d84a9dd
3 changed files with 66 additions and 55 deletions

View File

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

56
common/common.go Normal file
View File

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

View File

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