Module: RBText::Colors

Included in:
C
Defined in:
lib/rbtext/colors.rb

Constant Summary collapse

@@methods =
[
  :fg_color_codes,
  :bg_color_codes,
  :color,
  :num_color
]

Instance Method Summary collapse

Instance Method Details

#bg_color_codesObject



32
33
34
35
36
37
38
39
40
# File 'lib/rbtext/colors.rb', line 32

def 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



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/rbtext/colors.rb', line 42

def 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



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/rbtext/colors.rb', line 10

def 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



57
58
59
60
# File 'lib/rbtext/colors.rb', line 57

def 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