Module: GitRainbow::Painter

Extended by:
Painter
Included in:
Painter
Defined in:
lib/git_rainbow/painter.rb

Instance Method Summary collapse

Instance Method Details

#build_palette(size) ⇒ Object



15
16
17
18
19
20
21
22
23
24
# File 'lib/git_rainbow/painter.rb', line 15

def build_palette(size)
  size.times.map do |i|
    # we're basically generating a bunch of sine wave sequences
    # with a PI/2 phase shift between them to represent RGB colors
    red   = wave_value_at(i, size: size, phase:  Math::PI/2 - 1)
    green = wave_value_at(i, size: size, phase:  0          - 1)
    blue  = wave_value_at(i, size: size, phase: -Math::PI/2 - 1)
    "##{red}#{green}#{blue}"
  end
end

#colorize(char, color) ⇒ Object



11
12
13
# File 'lib/git_rainbow/painter.rb', line 11

def colorize(char, color)
  Tco.fg(color, char)
end

#paint(message) ⇒ Object



6
7
8
9
# File 'lib/git_rainbow/painter.rb', line 6

def paint(message)
  palette = build_palette(message.size)
  message.chars.map { |ch| colorize(ch, palette.shift) }.join("")
end

#wave_value_at(i, size: 1, phase: 0) ⇒ Object



26
27
28
29
30
# File 'lib/git_rainbow/painter.rb', line 26

def wave_value_at(i, size: 1, phase: 0)
  sin = Math.sin(Math::PI / size * 4 * i + phase) # pure sin -1..+1 value
  int = sin * 127 + 128 # 0..255 value
  "%02x" % int # 2 symbols hex
end