class Pigment::Palette

Represents a collection of named colors, for convenience

Attributes

colors[R]

@return [{Object: Pigment::Color}]

Public Class Methods

new(**colors) click to toggle source

@param [{Object => Pigment::Color}] colors

# File lib/pigment/palette.rb, line 12
def initialize(**colors)
  raise ArgumentError, "Pallete only accept colors" unless colors.values.all? {|color| color.is_a?(Pigment::Color)}
  @colors = colors
end

Public Instance Methods

+(other) click to toggle source

Adds a color or a Hash containing Colors as values @param [Pigment::Palette, Hash{Object => Pigment::Color}] other @return [Pigment::Palette]

# File lib/pigment/palette.rb, line 49
def +(other)
  case other
  when Pigment::Palette
    @colors.merge!(other.to_h)
  when Hash
    @colors.merge!(other) if other.values.all?(Pigment::Color)
  else raise ArgumentError, "Invalid operand \"#{other.inspect}:#{other.class}\" for +"
  end
  self
end
-(other) click to toggle source

Removes a color via its key or value @param [Object, Pigment::Color] other @return [Pigment::Palette, Pigment::Color, nil]

# File lib/pigment/palette.rb, line 63
def -(other)
  return @colors.delete(other) unless other.is_a? Pigment::Color
  @colors.delete_if {|_, color| other == color}
  self
end
==(other) click to toggle source

@param [Object] other @return [Boolean]

# File lib/pigment/palette.rb, line 71
def ==(other)
  self.class == other.class && @colors == other.to_h
end
[](*names) click to toggle source

Fetches the colors by names @param [Array] names @return [Pigment::Color] if only one result is found @return [Array<Pigment::Color>]

# File lib/pigment/palette.rb, line 30
def [](*names)
  colors = @colors.fetch_values(*names)
  colors.size == 1 ? colors.first : colors
end
[]=(*names, color) click to toggle source

Adds colors to the Palette with possible multiple names @param [Array] names @param [Pigment::Color] color @return [Pigment::Color] @raise ArgumentError

# File lib/pigment/palette.rb, line 40
def []=(*names, color)
  raise ArgumentError, "Expects Pigment::Color but got #{color.inspect}" unless color.is_a? Pigment::Color
  names.each { |name| @colors[name] = color }
  color
end
each(&block) click to toggle source

@param [Proc] block @return [Pigment::Palette] if block is passed @return [Enumerable] otherwise

# File lib/pigment/palette.rb, line 20
def each(&block)
  return to_enum(__method__) unless block_given?
  @colors.each(&block)
  self
end
names() click to toggle source

@return [Array<Object>] An array including all the defined color names

# File lib/pigment/palette.rb, line 81
def names
  @colors.keys
end
to_h() click to toggle source

@return [{Object => Pigment::Color}] An Hash representation of the Palette

# File lib/pigment/palette.rb, line 76
def to_h
  @colors.dup
end

Private Instance Methods

method_missing(method, *args) click to toggle source

Will retrieve the color from the Palette as a method call @param [Symbol] method the color name @param [Array] args will be ignored @return [Pigment::Color] The requested color

Calls superclass method
# File lib/pigment/palette.rb, line 95
def method_missing(method, *args)
  return super unless args.empty?
  @colors[method] || @colors[method.to_s] || super
end