module RCompile::Colorize

Constants

COLORS

Colors Hash

MODES

Modes Hash

Public Class Methods

color_matrix(txt = '[X]') click to toggle source

Display color matrix with color names

# File lib/rcompile/colorize.rb, line 167
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
colors() click to toggle source

Return array of available colors used by colorize method

# File lib/rcompile/colorize.rb, line 160
def colors
  COLORS.keys
end
modes() click to toggle source

Return array of available modes used by colorize method

# File lib/rcompile/colorize.rb, line 153
def modes
  MODES.keys
end

Public Instance Methods

colorize(params) click to toggle source

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/rcompile/colorize.rb, line 77
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

  color_parameters = {}

  if (params.instance_of?(Hash))
    color_parameters[:color]      = COLORS[params[:color]]
    color_parameters[:background] = COLORS[params[:background]]
    color_parameters[:mode]       = MODES[params[:mode]]
  elsif (params.instance_of?(Symbol))
    color_parameters[:color] = COLORS[params]
  end

  color_parameters[:color] ||= @color ||= COLORS[:default]
  color_parameters[:background] ||= @background ||= COLORS[:default]
  color_parameters[:mode] ||= @mode ||= MODES[:default]

  color_parameters[:uncolorized] ||= @uncolorized ||= self.dup

  # Calculate bright mode
  color_parameters[:color] += 50 if color_parameters[:color] > 10

  color_parameters[:background] += 50 if color_parameters[:background] > 10

  "\033[#{color_parameters[:mode]};#{color_parameters[:color]+30};#{color_parameters[:background]+40}m#{color_parameters[:uncolorized]}\033[0m".set_color_parameters(color_parameters)
end
colorized?() click to toggle source

Return true if string is colorized

# File lib/rcompile/colorize.rb, line 118
def colorized?
  !!@uncolorized
end
set_color_parameters(params) click to toggle source

Set color values in new string instance

# File lib/rcompile/colorize.rb, line 49
def set_color_parameters(params)
  if (params.instance_of?(Hash))
    @color       = params[:color]
    @background  = params[:background]
    @mode        = params[:mode]
    @uncolorized = params[:uncolorized]
    self
  end
end
uncolorize() click to toggle source

Return uncolorized string

# File lib/rcompile/colorize.rb, line 111
def uncolorize
  @uncolorized || self
end