class Pigment::Palette
Represents a collection of named colors, for convenience
Attributes
@return [{Object: Pigment::Color}]
Public Class Methods
@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
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
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
@param [Object] other @return [Boolean]
# File lib/pigment/palette.rb, line 71 def ==(other) self.class == other.class && @colors == other.to_h end
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
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
@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
@return [Array<Object>] An array including all the defined color names
# File lib/pigment/palette.rb, line 81 def names @colors.keys end
@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
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
# 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