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.

67 lines
1.6 KiB

  1. package main
  2. import (
  3. "image"
  4. "image/color"
  5. "image/png"
  6. "os"
  7. "time"
  8. )
  9. func getDaysBetween(start, end time.Time, extraRoom int) (startIndex int, dates []time.Time, endIndex int) {
  10. var arr []time.Time
  11. for t := start.AddDate(0, 0, -extraRoom); t.Before(end.AddDate(0, 0, extraRoom)); t = t.AddDate(0, 0, 1) {
  12. arr = append(arr, t)
  13. }
  14. return extraRoom, arr, len(arr) - extraRoom
  15. }
  16. func pctColor(img image.Image, fn colorMatcherFunc, p int, isColumn bool) float64 {
  17. var n int
  18. var t int
  19. if isColumn {
  20. t = img.Bounds().Max.Y
  21. } else {
  22. t = img.Bounds().Max.X
  23. }
  24. for op := 0; op < t; op++ {
  25. if (isColumn && fn(img.At(p, op))) || (!isColumn && fn(img.At(op, p))) {
  26. n++
  27. }
  28. }
  29. return float64(n) / float64(t)
  30. }
  31. // colorMatcherFunc returns a bool given a color.
  32. type colorMatcherFunc func(color.Color) bool
  33. // colorMatcher returns a function checking if an color.Color matches a rgb color.
  34. func colorMatcher(r, g, b uint8) colorMatcherFunc {
  35. return func(c color.Color) bool {
  36. cr, cg, cb, _ := c.RGBA()
  37. return uint8(cr>>8) == r && uint8(cg>>8) == g && uint8(cb>>8) == b
  38. }
  39. }
  40. // extrapolate estimates the value of point p on a line given two known points and values.
  41. func extrapolate(p1, p2 int, v1, v2 float64, p int) float64 {
  42. return float64(p-p1)/float64(p2-p1)*float64(v2-v1) + v1
  43. }
  44. // loadPNG loads a PNG image.
  45. func loadPNG(path string) (image.Image, error) {
  46. f, err := os.Open(path)
  47. if err != nil {
  48. return nil, err
  49. }
  50. defer f.Close()
  51. return png.Decode(f)
  52. }
  53. // must panics if err is not nil. This is only to simplify testing during development.
  54. func must(v interface{}, err error) interface{} {
  55. if err != nil {
  56. panic(err)
  57. }
  58. return v
  59. }