Module: Inform::Color

Included in:
Inform
Defined in:
lib/runtime/color.rb

Overview

The Color module

Constant Summary collapse

StyleCodes =
{
  plain: [
    "\e[0m", "\e[0m"],
  bold: [
    "\e[1m", "\e[22m"],
  italic: [
    "\e[3m", "\e[23m"],
  underline: [
    "\e[4m", "\e[24m"],
  blink: [
    "\e[5m", "\e[25m"],
  flash: [
    "\e[6m", "\e[26m"],
  inverse: [
    "\e[7m", "\e[27m"],
  hidden: [
    "\e[8m", "\e[28m"],
  strikethrough: [
    "\e[9m", "\e[29m"]
}.freeze
ColorCodes =
{
  default: [
    "\e[0m", "\e[0m"],
  black: [
    "\e[30m", "\e[39m"],
  black_on_white: [
    "\e[30m", "\e[39m"],
  red: [
    "\e[31m", "\e[39m"],
  green: [
    "\e[32m", "\e[39m"],
  yellow: [
    "\e[33m", "\e[39m"],
  blue: [
    "\e[34m", "\e[39m"],
  purple: [
    "\e[35m", "\e[39m"],
  magenta: [
    "\e[35m", "\e[39m"],
  cyan: [
    "\e[36m", "\e[39m"],
  white: [
    "\e[37m", "\e[39m"]
}.freeze
BackgroundColorCodes =
{
  default: [
    "\e[0m", "\e[0m"],
  black: [
    "\e[40m", "\e[49m"],
  red: [
    "\e[41m", "\e[49m"],
  green: [
    "\e[42m", "\e[49m"],
  yellow: [
    "\e[43m", "\e[49m"],
  blue: [
    "\e[44m", "\e[49m"],
  purple: [
    "\e[45m", "\e[49m"],
  magenta: [
    "\e[45m", "\e[49m"],
  cyan: [
    "\e[46m", "\e[49m"],
  white: [
    "\e[47m", "\e[49m"]
}.freeze
NearestColor =
{
  red: %i[
    crimson brick salmon coral],
  yellow: %i[
    golden orange],
  green: %i[
    seagreen teal forestgreen],
  blue: %i[
    aqua cyan skyblue turqoise],
  magenta: %i[
    lavender fuchsia indigo orchid plum purple violet]
}.freeze

Instance Method Summary collapse

Instance Method Details

#background_color(color, text = nil) ⇒ Object Also known as: bgcolor



113
114
115
116
117
# File 'lib/runtime/color.rb', line 113

def background_color(color, text = nil)
  code = (BackgroundColorCodes[color] || BackgroundColorCodes[:default]).first
  return code + text + (BackgroundColorCodes[color] || BackgroundColorCodes[:default]).last unless text.nil?
  print code
end

#color(color, text = nil) ⇒ Object



104
105
106
107
108
# File 'lib/runtime/color.rb', line 104

def color(color, text = nil)
  code = (ColorCodes[color] || ColorCodes[:default]).first
  return code + text + (ColorCodes[color] || ColorCodes[:default]).last unless text.nil?
  print code
end

#nearest_color(color) ⇒ Object



137
138
139
140
141
# File 'lib/runtime/color.rb', line 137

def nearest_color(color)
  colors = NearestColor.select { |_, v| v.include?(color) }
  return colors.first.first unless colors.empty?
  :white
end

#style(style, text = nil) ⇒ Object

Text style and color for terminals



95
96
97
98
99
# File 'lib/runtime/color.rb', line 95

def style(style, text = nil)
  code = (StyleCodes[style] || StyleCodes[:plain]).first
  return code + text + (StyleCodes[style] || StyleCodes[:plain]).last unless text.nil?
  print code
end

#un_background_color(color) ⇒ Object Also known as: unbgcolor



119
120
121
# File 'lib/runtime/color.rb', line 119

def un_background_color(color)
  print((BackgroundColorCodes[color] || BackgroundColorCodes[:default]).last)
end

#un_color(color) ⇒ Object Also known as: uncolor



109
110
111
# File 'lib/runtime/color.rb', line 109

def un_color(color)
  print((ColorCodes[color] || ColorCodes[:default]).last)
end

#un_style(style) ⇒ Object Also known as: unstyle



100
101
102
# File 'lib/runtime/color.rb', line 100

def un_style(style)
  print((StyleCodes[style] || StyleCodes[:plain]).last)
end