Class: Lolize::Colorizer
- Inherits:
-
Object
- Object
- Lolize::Colorizer
- Defined in:
- lib/lolize/colorizer.rb
Constant Summary collapse
- RAW_WRITE_METHOD =
:write_without_lolize
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize ⇒ Colorizer
constructor
A new instance of Colorizer.
-
#rainbow ⇒ Object
The algorithm is from lolcat (github.com/busyloop/lolcat) lolcat is released with WTFPL.
- #raw_write(s) ⇒ Object
- #write(s) ⇒ Object
Constructor Details
#initialize ⇒ Colorizer
Returns a new instance of Colorizer.
12 13 14 15 16 17 18 19 |
# File 'lib/lolize/colorizer.rb', line 12 def initialize @freq = 0.3 @spread = 8.0 @line_color = 0 @color = 0 @state = :normal end |
Class Method Details
Instance Method Details
#rainbow ⇒ Object
The algorithm is from lolcat (github.com/busyloop/lolcat) lolcat is released with WTFPL
23 24 25 26 27 28 29 |
# File 'lib/lolize/colorizer.rb', line 23 def rainbow red = Math.sin(@freq*@color + 0) * 127 + 128 green = Math.sin(@freq*@color + 2*Math::PI/3) * 127 + 128 blue = Math.sin(@freq*@color + 4*Math::PI/3) * 127 + 128 @color += 1/@spread "#%02X%02X%02X" % [ red, green, blue ] end |
#raw_write(s) ⇒ Object
50 51 52 53 54 55 56 57 58 |
# File 'lib/lolize/colorizer.rb', line 50 def raw_write(s) if $stdout.respond_to?(RAW_WRITE_METHOD) # if $stdout is hook $stdout.send(RAW_WRITE_METHOD, s) else # $stdout is not hooked $stdout.write(s) end end |
#write(s) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/lolize/colorizer.rb', line 31 def write(s) s.each_char do |c| case @state when :normal if c=="\e" @state = :ansi elsif c=="\n" @line_color += 1 @color = @line_color raw_write(c) else raw_write(Paint[c, rainbow]) end when :ansi @state = :normal if c=='m' end end end |