Object
Colorize String class extension.
Colors Hash
Modes Hash
Display color matrix with color names
# File lib/colorize.rb, line 152 def color_matrix(txt = '[X]') size = String.colors.length String.colors.each do |color| String.colors.each do |back| print txt.colorize(:color => color, :background => back) end puts " < #{color}" end String.colors.reverse.each_with_index do |back, index| puts "#{"|".rjust(txt.length)*(size-index)} < #{back}" end '' end
Change color of string
Examples:
puts "This is blue".colorize(:blue) puts "This is light blue".colorize(:light_blue) puts "This is also blue".colorize(:color => :blue) puts "This is light blue with red background".colorize(:color => :light_blue, :background => :red) puts "This is light blue with red background".colorize(:light_blue ).colorize( :background => :red) puts "This is blue text on red".blue.on_red puts "This is red on blue".colorize(:red).on_blue puts "This is red on blue and underline".colorize(:red).on_blue.underline puts "This is blue text on red".blue.on_red.blink puts "This is uncolorized".blue.on_red.uncolorize
# File lib/colorize.rb, line 64 def colorize(params) begin require 'Win32/Console/ANSI' if RUBY_PLATFORM =~ /win32/ rescue LoadError raise 'You must gem install win32console to use colorize on Windows' end self.scan(REGEXP_PATTERN).inject("") do |str, match| match[0] ||= MODES[:default] match[1] ||= COLORS[:default] + COLOR_OFFSET match[2] ||= COLORS[:default] + BACKGROUND_OFFSET match[3] ||= match[4] if (params.instance_of?(Hash)) match[0] = MODES[params[:mode]] if params[:mode] && MODES[params[:mode]] match[1] = COLORS[params[:color]] + COLOR_OFFSET if params[:color] && COLORS[params[:color]] match[2] = COLORS[params[:background]] + BACKGROUND_OFFSET if params[:background] && COLORS[params[:background]] elsif (params.instance_of?(Symbol)) match[1] = COLORS[params] + COLOR_OFFSET if params && COLORS[params] end str << "\0033[#{match[0]};#{match[1]};#{match[2]}m#{match[3]}\0033[0m" end end
Generated with the Darkfish Rdoc Generator 2.