Module: Mayu::Colors

Extended by:
T::Sig
Defined in:
lib/mayu/colors.rb

Class Method Summary collapse

Class Method Details

.rainbow(str, t = Time.now.to_f) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/mayu/colors.rb', line 9

def self.rainbow(str, t = Time.now.to_f)
  str
    .each_line
    .map do |line|
      line
        .chars
        .map
        .with_index do |ch, i|
          next ch if ch.strip.empty?

          r, g, b =
            3
              .times
              .map { _1 / 3.0 * Math::PI }
              .map { _1 + i / 10.0 }
              .map { Math.sin(_1 - t)**2 }
              .map { (_1 * 255).to_i }

          format("\e[38;2;%d;%d;%dm%s", r, g, b, ch)
        end
        .join
    end
    .join + "\e[0m"
end