Module: ConsoleGlitter::ANSI
Instance Method Summary collapse
-
#bg_hex_color(color) ⇒ Object
Public: Generate an escape sequence to set the bg color to an approximation of a 4 or 8 bit per channel hex RGB color a la CSS.
-
#escape(sequence) ⇒ Object
Public: Return an appropriate escape sequence depending upon the current platform.
-
#hex_color(color) ⇒ Object
Public: Generate an escape sequence to set the foreground color to an approximation of a 4 or 8 bit per channel hex RGB color a la CSS.
Instance Method Details
#bg_hex_color(color) ⇒ Object
Public: Generate an escape sequence to set the bg color to an approximation of a 4 or 8 bit per channel hex RGB color a la CSS.
color - String containing hex digits describing the color to convert.
May be 4 or 8 bits per channel (3 or 6 characters long,
respectively).
Examples
ConsoleGlitter::ANSI.bg_hex_color("00FFFF")
# => "\033[38;5;51m"
ConsoleGlitter::ANSI.bg_hex_color("F0F")
# => "\033[38;5;201m"
Returns the appropriate escape code as a Fixnum.
53 54 55 |
# File 'lib/console-glitter/ansi.rb', line 53 def bg_hex_color(color) escape [48, 5, closest(color)].join(';') end |
#escape(sequence) ⇒ Object
Public: Return an appropriate escape sequence depending upon the current platform.
sequence - Control code(s) to be escaped. Multiple codes may be chained,
separated by ';'.
Examples
ConsoleGlitter::ANSI.escape 0
# => "\033[0m"
Returns a String.
15 16 17 |
# File 'lib/console-glitter/ansi.rb', line 15 def escape(sequence) RUBY_PLATFORM =~ /win|mingw/i ? '' : "\033[#{sequence}m" end |
#hex_color(color) ⇒ Object
Public: Generate an escape sequence to set the foreground color to an approximation of a 4 or 8 bit per channel hex RGB color a la CSS.
color - String containing hex digits describing the color to convert.
May be 4 or 8 bits per channel (3 or 6 characters long,
respectively).
Examples
ConsoleGlitter::ANSI.hex_color("00FFFF")
# => "\033[38;5;51m"
ConsoleGlitter::ANSI.hex_color("F0F")
# => "\033[38;5;201m"
Returns the appropriate escape code as a Fixnum.
34 35 36 |
# File 'lib/console-glitter/ansi.rb', line 34 def hex_color(color) escape [38, 5, closest(color)].join(';') end |