class Chromate::EffectSet

A wrapper over the `Set` class, useful for maintaining and emitting collections of unrelated effects. Only effects may be added to an `EffectSet`, but some other types may be coerced into an effect. The coercion rules are as follows:

1. If the value is an `Effect`, it will remain as-is.
2. If the value is an `Array`, it is unambiguously in reference to a color,
   specifically an RGB array, and will be parsed as such.
3. If the value is a `Symbol`, it is in reference to a name of some sort.
   Names of effects will be checked before names of colors. If no effect
   or color exists with this name, an `ArgumentError` will be thrown.
4. If the value is a `String`, it may either be a name of some sort or a
   hex color string. If the value begins with a '#', it will be parsed as
   a hex string. Otherwise, it will be parsed according to rule 3.
5. If the value is a `Numeric`, it may either be a raw effect or a color
   code. If the value is negative or greater than 255, an error will be
   thrown. Otherwise, if the value is not a valid effect code, the value
   is unambiguously a color code and will be parsed as such. Otherwise,
   there is insufficient information available to determine course of
   action. The target class must be explicitly specified, or it will
   default to `Effect`. (Target class must be a subclass of `Effect`, or
   `Effect` itself.) As a special case, 0 is considered an invalid effect
   code, as it would make little sense to include an 'off' sequence in an
   unordered collection of effects. As such, adding 0 to an EffectSet will
   add the color black, not the off sequence.

Public Class Methods

[](*codes) click to toggle source

Construct a new `EffectSet` from an array of effects. If non-effects are encountered, they will be parsed as per the rules of `EffectSet`. @param codes [<Chromate::Effect,Integer,<Integer>,Symbol,String>]

@return [EffectSet]

# File lib/chromate/effect_set.rb, line 51
def self.[](*codes)
  new(codes)
end
new(enum = []) click to toggle source

Construct a new `EffectSet` from an existing enumerable object containing effects. If non-effects are encountered, they will be parsed as per the rules of `EffectSet`. @param enum [Enumerable] an enumerable object containing colors

@return [EffectSet]

Calls superclass method
# File lib/chromate/effect_set.rb, line 41
def initialize(enum = [])
  super(enum) { |what| parse_member(what) }
end

Public Instance Methods

+(other) click to toggle source
# File lib/chromate/effect_set.rb, line 79
def +(other)
  case other
  when EffectSet
    other.add(self)
  when Effect
    add(other)
  when String
    escape + other
  else
    raise TypeError, "cannot add #{other.class} to #{self.class}"
  end
end
add(what, klass = Effect) click to toggle source

Add an element to the set. If it is a non-effect, it will be parsed as per the rules of `EffectSet`. An optional second parameter indicates the class of `Effect` to use if passing in an ambiguous effect code. @param what [Chromate::Effect,Integer,<Integer>,Symbol,String] @param klass [Class]

@return [EffectSet] the same effect set with `what` added in to it

Calls superclass method
# File lib/chromate/effect_set.rb, line 75
def add(what, klass = Effect)
  super(parse_member(what, klass))
end
escape() click to toggle source

Generate a single escape sequence that is an agglomeration of the escape sequences of the individual effects within the set.

@return [String]

# File lib/chromate/effect_set.rb, line 60
def escape
  "\e[" + map do |effect|
    /\e\[(.*)m/.match(effect.escape)[1]
  end.join(';') + 'm'
end
Also aliased as: to_s
inspect() click to toggle source

@return [String]

# File lib/chromate/effect_set.rb, line 93
def inspect
  sprintf('#<EffectSet: {%s}>', to_a.inspect[1..-2])
end
to_s()
Alias for: escape

Private Instance Methods

parse_member(what, klass = Effect) click to toggle source
# File lib/chromate/effect_set.rb, line 99
def parse_member(what, klass = Effect)
  unless klass <= Effect
    raise TypeError, "#{klass.name} is not a sublcass of Effect"
  end

  case what
  when Effect
    what
  when Numeric
    return COLORS[0] if what == 0
    if what < 0 or what > 255
      raise ArgumentError, "invalid effect or color code: #{what}"
    elsif VALID_EFFECTS.include?(what)
      klass.new(what.to_i)
    else
      Color.new(what.to_i)
    end
  when Array
    Color.from_rgb(*what)
  when Symbol
    if EFFECTS.include?(what)
      EFFECTS[what]
    elsif COLOR_NAMES.include?(what)
      COLORS[COLOR_NAMES[what]]
    else
      raise ArgumentError, "no such effect or color: #{what}"
    end
  when String
    if what[0] == '#'
      Color.from_hex(what)
    else
      parse_member(what.intern)
    end
  else
    raise TypeError, "cannot create Chromate::Effect from #{what.class}"
  end
end