Created
April 26, 2014 19:43
-
-
Save mscoutermarsh/11329131 to your computer and use it in GitHub Desktop.
Rainbow Text β€οΈ π π π
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Allows us to print rainbow text to console | |
# Use puts Rainbow.new.colorize("Magical Text") | |
class Rainbow | |
PI_3 = Math::PI / 3 | |
def initialize | |
# colors calculation stolen from Minitest's Pride Plugin | |
# https://github.com/seattlerb/minitest | |
@colors = (0...(6 * 7)).map { |n| | |
n *= 1.0 / 6 | |
r = (3 * Math.sin(n ) + 3).to_i | |
g = (3 * Math.sin(n + 2 * PI_3) + 3).to_i | |
b = (3 * Math.sin(n + 4 * PI_3) + 3).to_i | |
36 * r + 6 * g + b + 16 | |
} | |
@color_index = 0 | |
end | |
def rainbow | |
@color_index == (@colors.size - 1) ? @color_index = 0 : @color_index += 1 | |
@colors[@color_index] | |
end | |
def colorize(string) | |
rainbow_string = "" | |
string.each_char do |char| | |
rainbow_string << "\e[38;5;#{rainbow}m#{char}\e[0m" | |
end | |
rainbow_string | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I'm using this now in a gem i'm writing. Gem is a CLI... prints some stuff in rainbow. Just... for fun.