module LsColors::ColorManipulation

Methods and data for manipulating terminal colors.

Constants

CODE_DESCRIPTIONS

Explanation of system-related file codes, for previewing and learning purposes.

COLOR_BG

The prefix for the background color (256 colors).

COLOR_FG

The prefix for the foreground color (256 colors).

COLOR_HEAD

The string that begins a terminal-compatible color definition.

COLOR_RESET

This string resets color and style to default.

COLOR_TAIL

The string that ends a terminal-compatible color definition.

DEFAULTS

The fallback color sheet.

STYLES

The style codes recognized by the terminal.

Public Instance Methods

color_card(color) click to toggle source

Returns a preview of a single color.

@param [Integer] color @return [String]

# File lib/lscolors.rb, line 118
def color_card(color)
  "#{colorizer ['foo', 0, color]}   #{format '%03d', color}#{COLOR_RESET}"
end
color_setting(array) click to toggle source

Take an array as per the {DEFAULTS} format and create a color definition string.

@param [Array] array @return [String]

# File lib/lscolors.rb, line 137
def color_setting(array)
  string = ''

  STYLES.each_pair do |style, code|
    next unless array.include?(style) || array.include?(style.to_s)
    string << "#{code};"
  end
  string << "#{COLOR_FG}#{array[1]}"
  string << ";#{COLOR_BG}#{array[2]}" if array[2].is_a? Integer

  string
end
colorizer(array) click to toggle source

Take an array as per the {DEFAULTS} format and create a terminal style escape string.

@param [Array] array @return [String]

# File lib/lscolors.rb, line 127
def colorizer(array)
  fail "No foreground color in #{array}." unless array[1].is_a? Integer
  "#{COLOR_HEAD}#{color_setting(array)}#{COLOR_TAIL}"
end
preview_extensions() click to toggle source

Return a colorized preview of the extension color settings.

@return [String]

# File lib/lscolors.rb, line 165
def preview_extensions
  string = ''

  @colors[:extensions].each do |c|
    c.first.split.sort.each do |ext|
      string << "#{colorizer(c)}*.#{ext}#{COLOR_RESET} "
    end
    string << "\n"
  end

  string
end
preview_system() click to toggle source

Return a colorized preview of the system file color settings.

@return [String]

# File lib/lscolors.rb, line 153
def preview_system
  array = @colors[:system].map do |c|
    code = CODE_DESCRIPTIONS.fetch(c.first.to_sym, c.first)
    "#{colorizer(c)}#{code}#{COLOR_RESET}"
  end

  array.join ' '
end
test_array(array) click to toggle source

Test an array of color numbers.

@param [Array] array the color numbers to test. @return [String]

# File lib/lscolors.rb, line 108
def test_array(array)
  output = array.map { |color| color_card(color) } << "\n"

  output.join
end
test_cube() click to toggle source

Test the 6x6x6 color cube.

@return [String]

# File lib/lscolors.rb, line 86
def test_cube
  output = (0..17).map do |row|
    column = 16 + (row * 6) + (36 * (row / 6))
    cube_1 = (column..column + 5).to_a
    cube_2 = (column + 36..column + 41).to_a
    test_array(cube_1 + cube_2)
  end

  output.join
end
test_grayscale() click to toggle source

Test the 24 grayscale colors.

@return [String]

# File lib/lscolors.rb, line 100
def test_grayscale
  [(232..243), (244..255)].map { |range| test_array range.to_a }.join
end
test_system() click to toggle source

Test the 16 system colors.

@return [String]

# File lib/lscolors.rb, line 79
def test_system
  [(0..7), (8..15)].map { |range| test_array range.to_a }.join
end