Class: Git::Heatmap::Templates::Series

Inherits:
Object
  • Object
show all
Defined in:
lib/git/heatmap/templates/series.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(commits, title:, template: self.class.template) ⇒ Series

Returns a new instance of Series.



33
34
35
36
37
38
# File 'lib/git/heatmap/templates/series.rb', line 33

def initialize(commits, title:, template: self.class.template)
  @commits = commits
  @title = title
  
  @template = template
end

Instance Attribute Details

#commitsObject (readonly)

Returns the value of attribute commits.



40
41
42
# File 'lib/git/heatmap/templates/series.rb', line 40

def commits
  @commits
end

#titleObject (readonly)

Returns the value of attribute title.



41
42
43
# File 'lib/git/heatmap/templates/series.rb', line 41

def title
  @title
end

Class Method Details

.templateObject



29
30
31
# File 'lib/git/heatmap/templates/series.rb', line 29

def self.template
  Trenni::Template.load_file(File.expand_path("series/template.trenni", __dir__))
end

Instance Method Details

#background_color(size) ⇒ Object



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# File 'lib/git/heatmap/templates/series.rb', line 78

def background_color(size)
  temp = temperature(ramp(size.to_f), [
    [0xFF, 0xFF, 0xFF],
    #[0x04, 0x05, 0x57],
    
    #[0x17, 0xD3, 0xFF],
    [0x4D, 0x85, 0xFF],
    
    # [0x02, 0xB1, 0x50], # Green
    
    #[0xF2, 0xFD, 0xA0], # Yellow
    [0xF4, 0xFB, 0x13],
    
    [255, 0x05, 0x05],  # Red
    # [255, 200, 200]
  ])
  
  return hexcolor(temp)
end

#callObject



98
99
100
# File 'lib/git/heatmap/templates/series.rb', line 98

def call
  @template.to_string(self)
end

#hexcolor(c) ⇒ Object



74
75
76
# File 'lib/git/heatmap/templates/series.rb', line 74

def hexcolor(c)
  '#' + c.collect{|k| k.to_i.to_s(16).rjust(2, '0')}.join
end

#interpolate(t, x, y) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/git/heatmap/templates/series.rb', line 51

def interpolate(t, x, y)
  r = x.dup
  
  r.size.times{|i| r[i] = (x[i].to_f * (1.0 - t)) + (y[i].to_f * t)}
  
  return r
end

#ramp(x, max = commits.maximum) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/git/heatmap/templates/series.rb', line 43

def ramp(x, max = commits.maximum)
  i = x.to_f / max
  # j = Math::sqrt(i)
  # k = i ** 2
  # return interpolate(i, [j], [k])[0]
  return i
end

#temperature(t, colors) ⇒ Object

t between 0…1



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/git/heatmap/templates/series.rb', line 60

def temperature(t, colors)
  p = (t.to_f * colors.size)
  
  if (p <= 0)
    return colors.first
  end
  
  if (p+1 >= colors.size)
    return colors.last
  end
  
  return interpolate(p - p.floor, colors[p.floor], colors[p.floor+1])
end