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

  1. package graph
  2. import (
  3. "image"
  4. "image/color"
  5. "time"
  6. )
  7. func getDaysBetween(start, end time.Time, extraRoom int) (startIndex int, dates []time.Time, endIndex int) {
  8. var arr []time.Time
  9. for t := start.AddDate(0, 0, -extraRoom); t.Before(end.AddDate(0, 0, extraRoom)); t = t.AddDate(0, 0, 1) {
  10. arr = append(arr, t)
  11. }
  12. return extraRoom, arr, len(arr) - extraRoom
  13. }
  14. func pctColor(img image.Image, fn colorMatcherFunc, p int, isColumn bool) float64 {
  15. var n int
  16. var t int
  17. if isColumn {
  18. t = img.Bounds().Max.Y
  19. } else {
  20. t = img.Bounds().Max.X
  21. }
  22. for op := 0; op < t; op++ {
  23. if (isColumn && fn(img.At(p, op))) || (!isColumn && fn(img.At(op, p))) {
  24. n++
  25. }
  26. }
  27. return float64(n) / float64(t)
  28. }
  29. // colorMatcherFunc returns a bool given a color.
  30. type colorMatcherFunc func(color.Color) bool
  31. // colorMatcher returns a function checking if an color.Color matches a rgb color.
  32. func colorMatcher(r, g, b uint8) colorMatcherFunc {
  33. return func(c color.Color) bool {
  34. cr, cg, cb, _ := c.RGBA()
  35. return uint8(cr>>8) == r && uint8(cg>>8) == g && uint8(cb>>8) == b
  36. }
  37. }
  38. // extrapolate estimates the value of point p on a line given two known points and values.
  39. func extrapolate(p1, p2 int, v1, v2 float64, p int) float64 {
  40. return float64(p-p1)/float64(p2-p1)*float64(v2-v1) + v1
  41. }