Module: Sai::Decorator::HexColors Abstract Private

Included in:
Sai::Decorator
Defined in:
lib/sai/decorator/hex_colors.rb

Overview

This module is part of a private API. You should avoid using this module if possible, as it may be removed or be changed in the future.

This module is abstract.

This module is meant to be included in the Sai::Decorator class to provide hexadecimal color methods

Hexadecimal color methods for the Sai::Decorator class

Author:

Since:

  • 0.3.1

Instance Method Summary collapse

Instance Method Details

#hex(code) ⇒ Decorator

Apply a hexadecimal color to the foreground

Examples:

decorator.hex("#EB4133").decorate('Hello, world!').to_s #=> "\e[38;2;235;65;51mHello, world!\e[0m"

Parameters:

  • code (String) —

    the hex color code

Returns:

  • (Decorator) —

    a new instance of Decorator with the hex color applied

Raises:

  • (ArgumentError) —

    if the hex code is invalid

Author:

Since:

  • 0.1.0



28
29
30
31
32
# File 'lib/sai/decorator/hex_colors.rb', line 28

def hex(code)
  raise ArgumentError, "Invalid hex color code: #{code}" unless /^#?([A-Fa-f0-9]{6})$/.match?(code)

  dup.tap { |duped| duped.instance_variable_set(:@foreground, code) } #: Decorator
end

#on_hex(code) ⇒ Decorator

Apply a hexadecimal color to the background

Examples:

decorator.on_hex("#EB4133").decorate('Hello, world!').to_s #=> "\e[48;2;235;65;51mHello, world!\e[0m"

Parameters:

  • code (String) —

    the hex color code

Returns:

  • (Decorator) —

    a new instance of Decorator with the hex color applied

Raises:

  • (ArgumentError) —

    if the hex code is invalid

Author:

Since:

  • 0.1.0



49
50
51
52
53
# File 'lib/sai/decorator/hex_colors.rb', line 49

def on_hex(code)
  raise ArgumentError, "Invalid hex color code: #{code}" unless /^#?([A-Fa-f0-9]{6})$/.match?(code)

  dup.tap { |duped| duped.instance_variable_set(:@background, code) } #: Decorator
end