Class: HighLine::ColorScheme
Overview
ColorScheme objects encapsulate a named set of colors to be used in the #color method call. For example, by applying a ColorScheme that has a :warning
color then the following could be used:
color("This is a warning", :warning)
A ColorScheme contains named sets of HighLine color constants.
Direct Known Subclasses
Instance Method Summary collapse
-
#[](color_tag) ⇒ Style
Allow the scheme to be accessed like a Hash.
-
#[]=(color_tag, constants) ⇒ Object
Allow the scheme to be set like a Hash.
-
#definition(color_tag) ⇒ Object
Retrieve the original form of the scheme.
-
#include?(color_tag) ⇒ Boolean
Does this color scheme include the given tag name?.
-
#initialize(h = nil) {|_self| ... } ⇒ ColorScheme
constructor
Create an instance of HighLine::ColorScheme.
-
#keys ⇒ Array
Retrieve the keys in the scheme.
-
#load_from_hash(h) ⇒ Object
Load multiple colors from key/value pairs.
-
#to_hash ⇒ Hash
Retrieve the color scheme hash (in original definition format).
Constructor Details
#initialize(h = nil) {|_self| ... } ⇒ ColorScheme
Create an instance of HighLine::ColorScheme. The customization can happen as a passed in Hash or via the yielded block. Keys are converted to :symbols
and values are converted to HighLine constants.
52 53 54 55 56 |
# File 'lib/highline/color_scheme.rb', line 52 def initialize(h = nil) @scheme = {} load_from_hash(h) if h yield self if block_given? end |
Instance Method Details
#[](color_tag) ⇒ Style
Allow the scheme to be accessed like a Hash.
76 77 78 |
# File 'lib/highline/color_scheme.rb', line 76 def [](color_tag) @scheme[to_symbol(color_tag)] end |
#[]=(color_tag, constants) ⇒ Object
Allow the scheme to be set like a Hash.
96 97 98 99 100 101 |
# File 'lib/highline/color_scheme.rb', line 96 def []=(color_tag, constants) @scheme[to_symbol(color_tag)] = HighLine::Style.new(name: color_tag.to_s.downcase.to_sym, list: constants, no_index: true) end |
#definition(color_tag) ⇒ Object
Retrieve the original form of the scheme
82 83 84 85 |
# File 'lib/highline/color_scheme.rb', line 82 def definition(color_tag) style = @scheme[to_symbol(color_tag)] style && style.list end |
#include?(color_tag) ⇒ Boolean
Does this color scheme include the given tag name?
69 70 71 |
# File 'lib/highline/color_scheme.rb', line 69 def include?(color_tag) @scheme.keys.include?(to_symbol(color_tag)) end |
#keys ⇒ Array
Retrieve the keys in the scheme
89 90 91 |
# File 'lib/highline/color_scheme.rb', line 89 def keys @scheme.keys end |
#load_from_hash(h) ⇒ Object
Load multiple colors from key/value pairs.
60 61 62 63 64 |
# File 'lib/highline/color_scheme.rb', line 60 def load_from_hash(h) h.each_pair do |color_tag, constants| self[color_tag] = constants end end |
#to_hash ⇒ Hash
Retrieve the color scheme hash (in original definition format)
105 106 107 108 109 110 |
# File 'lib/highline/color_scheme.rb', line 105 def to_hash @scheme.each_with_object({}) do |pair, hsh| key, value = pair hsh[key] = value.list end end |