Parses graphs and other data from camelcamelcamel.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.

47 lines
1.3 KiB

package graph
import (
"image"
"image/color"
"time"
)
func getDaysBetween(start, end time.Time, extraRoom int) (startIndex int, dates []time.Time, endIndex int) {
var arr []time.Time
for t := start.AddDate(0, 0, -extraRoom); t.Before(end.AddDate(0, 0, extraRoom)); t = t.AddDate(0, 0, 1) {
arr = append(arr, t)
}
return extraRoom, arr, len(arr) - extraRoom
}
func pctColor(img image.Image, fn colorMatcherFunc, p int, isColumn bool) float64 {
var n int
var t int
if isColumn {
t = img.Bounds().Max.Y
} else {
t = img.Bounds().Max.X
}
for op := 0; op < t; op++ {
if (isColumn && fn(img.At(p, op))) || (!isColumn && fn(img.At(op, p))) {
n++
}
}
return float64(n) / float64(t)
}
// colorMatcherFunc returns a bool given a color.
type colorMatcherFunc func(color.Color) bool
// colorMatcher returns a function checking if an color.Color matches a rgb color.
func colorMatcher(r, g, b uint8) colorMatcherFunc {
return func(c color.Color) bool {
cr, cg, cb, _ := c.RGBA()
return uint8(cr>>8) == r && uint8(cg>>8) == g && uint8(cb>>8) == b
}
}
// extrapolate estimates the value of point p on a line given two known points and values.
func extrapolate(p1, p2 int, v1, v2 float64, p int) float64 {
return float64(p-p1)/float64(p2-p1)*float64(v2-v1) + v1
}