Class: Sai::Mei::Palette Abstract Private

Inherits:
Object
  • Object
show all
Defined in:
lib/sai/mei/palette.rb

Overview

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

This class is abstract.

Include in subclass and define a constant named COLORS

The base module for color palettes

Author:

Since:

  • unreleased

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(lookup) ⇒ Palette

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

Initialize a new instance of the Palette

Parameters:

  • lookup (Hash{Symbol => Array<Integer>}) —

    the candidates to be installed

Author:

Since:

  • unreleased



60
61
62
63
# File 'lib/sai/mei/palette.rb', line 60

def initialize(lookup)
  @candidates = lookup.dup
  @lookup = lookup.freeze
end

Instance Attribute Details

#candidates ⇒ Hash{Symbol => Array<Integer>} (readonly)

The candidates to be installed

Returns:

  • (Hash{Symbol => Array<Integer>})

Author:

Since:

  • unreleased



23
24
25
# File 'lib/sai/mei/palette.rb', line 23

def candidates
  @candidates
end

Class Method Details

.load(config_name) ⇒ Palette

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

Load the Palette from configuration

Parameters:

  • config_name (String, Symbol) —

    the name of the configuration to load

Returns:

Author:

Since:

  • unreleased



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sai/mei/palette.rb', line 36

def self.load(config_name)
  lookup = YAML.load_file(
    File.expand_path("../../../config/#{config_name.to_s.downcase}.yml", File.dirname(__FILE__)),
    symbolize_names: true
  )

  if Sai::Mei.const_defined?(config_name)
    Sai::Mei.const_get(config_name).new(lookup)
  else
    Sai::Mei.const_set(config_name, Class.new(Palette)).new(lookup)
  end
end

Instance Method Details

#color_names ⇒ Array<Symbol>

Get all the names of the color names available in the Palette

Examples:

Sai::Mei.xterm.all_names #=> [:black, :maroon, :green, ...]

Returns:

  • (Array<Symbol>) —

    the names of the colors in the palette

Author:

Since:

  • unreleased



76
77
78
# File 'lib/sai/mei/palette.rb', line 76

def color_names
  @lookup.keys
end

#excluding(*color_names) ⇒ Palette

Exclude colors from the palette

Examples:

Sai::Mei.xterm.except(:green, :dodger_blue_2, :spring_green_4).install

Parameters:

  • color_names (Array<String, Symbol>) —

    the names of the colors to exclude

Returns:

  • (Palette) —

    the palette with the specified colors excluded

Author:

Since:

  • unreleased



94
95
96
97
# File 'lib/sai/mei/palette.rb', line 94

def excluding(*color_names)
  @candidates = candidates.except(*color_names.map(&:to_sym))
  self
end

#install ⇒ void

This method returns an undefined value.

Install the candidates into Sai

Examples:

Sai::Mei.xterm.install

Author:

Since:

  • unreleased



111
112
113
# File 'lib/sai/mei/palette.rb', line 111

def install
  candidates.each_pair { |name, color| Sai.register(name, color) }
end

#only(*color_names) ⇒ self

Limit the color palette to specific named colors

Examples:

Sai::Mei.xterm.only(:green, :dodger_blue_2, :spring_green_4).install

Parameters:

  • color_names (Array<String, Symbol>) —

    the names of the colors to restrict the palette to

Returns:

  • (self) —

    the palette with the specified colors

Author:

Since:

  • unreleased



129
130
131
132
# File 'lib/sai/mei/palette.rb', line 129

def only(*color_names)
  @candidates = candidates.slice(*color_names.map(&:to_sym))
  self
end

#rename(**color_map) ⇒ self

Change the name specific colors in the palette

Examples:

Sai::Mei.xterm.rename(green: :xterm_green, dodger_blue_3: :xterm_dodger_blue).install

Parameters:

  • color_map (Hash{String, Symbol => String, Symbol}) —

    the mapping of color names to new names

Returns:

  • (self) —

    the palette with the specified colors renamed

Author:

Since:

  • unreleased



148
149
150
151
152
153
154
155
156
157
# File 'lib/sai/mei/palette.rb', line 148

def rename(**color_map)
  color_map = color_map.transform_keys(&:to_sym).transform_values(&:to_sym) #: Hash[Symbol, Symbol]

  color_map.each_pair do |old_name, new_name|
    next unless candidates.key?(old_name)

    candidates[new_name] = candidates.delete(old_name) # steep:ignore ArgumentTypeMismatch
  end
  self
end

#reset! ⇒ Palette

Reset the candidates to the original state

Examples:

palette = Sai::Mei.xterm.except(:green)
palette.candidates.keys.include?(:green) #=> false
palette.reset.candidates.keys.include?(:green) #=> true

Returns:

  • (Palette) —

    the Palette with the candidates reset

Author:

Since:

  • unreleased



173
174
175
# File 'lib/sai/mei/palette.rb', line 173

def reset!
  self.class.new(@lookup.dup)
end