Module: RBText::Colors

Defined in:
lib/rbtext/colors.rb

Class Method Summary collapse

Class Method Details

.bg_color_codesObject



25
26
27
28
29
30
31
32
33
# File 'lib/rbtext/colors.rb', line 25

def self.bg_color_codes
  bg_color_codes = {}

  for c in self.fg_color_codes.keys do
    bg_color_codes[c] = (self.fg_color_codes[c].to_i + 10).to_s
  end

  return bg_color_codes
end

.color(color, type: :fg, mode: :str) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rbtext/colors.rb', line 35

def self.color(color, type: :fg, mode: :str)
  return "\033[39m\033[49m" if type == :all && color == :reset

  if type == :fg
    color_code = self.fg_color_codes[color.to_sym]
  elsif type == :bg
    color_code = self.bg_color_codes[color.to_sym]
  else
    return nil
  end

  return "\033[#{color_code}m" if mode == :str
  print "\033[#{color_code}m" if mode == :set
end

.fg_color_codesObject



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/rbtext/colors.rb', line 3

def self.fg_color_codes
  {
    black: "30",
    red: "31",
    green: "32",
    yellow: "33",
    blue: "34",
    magenta: "35",
    cyan: "36",
    light_gray: "37",
    gray: "90",
    light_red: "91",
    light_green: "92",
    light_yellow: "93",
    light_blue: "94",
    light_magenta: "95",
    light_cyan: "96",
    white: "97",
    reset: "39"
  }
end

.num_color(num, type: :fg, mode: :str) ⇒ Object



50
51
52
53
# File 'lib/rbtext/colors.rb', line 50

def self.num_color(num, type: :fg, mode: :str)
  return "\033[#{type == :fg ? "38" : "48"};5;#{num}m" if mode == :str
  print "\033[#{type == :fg ? "38" : "48"};5;#{num}m" if mode == :set
end