-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.rb
82 lines (71 loc) · 2.44 KB
/
helpers.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
require './matrix_2d_extensions'
class StrokeDash
def initialize(offset, *steps)
@offset = offset
@steps = steps
end
def css(shrink_by=0, precision=3)
css_properties(@steps.map.with_index{ |a, i| a + (i.odd? ? 1 : -1)*shrink_by }, @offset - shrink_by/2, precision)
end
private
def css_properties(array, offset, precision)
result = "stroke-dasharray: #{array.map{ |a| '%g' % a.round(precision) }.join(',')};"
result += " stroke-dashoffset: #{'%g' % offset.round(precision)};" unless offset == 0
result
end
end
def symmetry(n)
(0...n).map{|i| i/n.to_f*Math::PI*2}
end
def avg(xs)
xs.reduce(&:+) / xs.length.to_f
end
def page_intro(title, theme=:white, date='May 2016', path=nil)
gh = path ? "/blob/gh-pages/#{path}" : ''
puts <<-HTML.gsub(/^ {4}/, '')
<title>#{title}</title>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">
<style>
* { padding: 0; margin: 0; }
body { background: ##{:white.eql?(theme) ? "fff" : "000"}; width: 100%; height: 100%; font-family: "Avenir Next", "Helvetica Neue", Arial, sans-serif; text-align: center; }
body, h1 { font-weight: 100; }
a { opacity: #{:minimal.eql?(theme) ? "1" : "0.7"}; }
#content { position: absolute; bottom: 10px; right: 10px; color: ##{:minimal.eql?(theme) ? "555" : "999"}; text-align: left; }
svg { max-height: 100%; max-width: 100%; }
</style>
<div id="content">
<h1>#{title}</h1>
<p>By <a href="http://bernhardhaeussner.de">Bernhard Häussner</a>, #{date}.<br/>Code on <a href="https://github.com/bxt/Nazareth-Knot#{gh}">GitHub</a>.</p>
</div>
HTML
end
def darken_color(hex_color, amount)
transform_color(hex_color) do |rgb|
rgb.map { |c| c*amount }
end
end
def lighten_color(hex_color, amount)
transform_color(hex_color) do |rgb|
rgb.map { |c| 255 - (255 - c)*amount }
end
end
def mix_color(hex_color_a, hex_color_b, amount)
transform_color(hex_color_a) do |rgb_a|
transform_color(hex_color_b) do |rgb_b|
break rgb_a.zip(rgb_b).map do |a, b|
a*(amount) + b*(1 - amount)
end
end
end
end
def transform_color(hex_color)
rgb_in = if m = hex_color.match(/#?(\h\h)(\h\h)(\h\h)/)
m.to_a.drop(1).map(&:hex)
elsif m = hex_color.match(/#?(\h\h\h)/)
m[1].chars.map{ |c| (c + c).hex }
end
rgb_out = yield(rgb_in)
"#%02x%02x%02x" % rgb_out.map(&:round)
end