class Sass::Script::Value::Color

def initialize(attrs, representation = nil, allow_both_rgb_and_hsl = false)

Raises:
  • (ArgumentError) - if not enough attributes are specified
  • (ArgumentError) - if not enough attributes are specified,
  • (Sass::SyntaxError) - if any color value isn't in the specified range

Parameters:
  • representation (String) -- The original representation of the color
  • rgba (Array) -- A three- or four-element array
  • attrs ({Symbol => Numeric}) -- A hash of color attributes to values

Overloads:
  • initialize(rgba, [representation])
  • initialize(attrs)
def initialize(attrs, representation = nil, allow_both_rgb_and_hsl = false)
  super(nil)
  if attrs.is_a?(Array)
    unless (3..4).include?(attrs.size)
      raise ArgumentError.new("Color.new(array) expects a three- or four-element array")
    end
    red, green, blue = attrs[0...3].map {|c| Sass::Util.round(c)}
    @attrs = {:red => red, :green => green, :blue => blue}
    @attrs[:alpha] = attrs[3] ? attrs[3].to_f : 1
    @representation = representation
  else
    attrs = attrs.reject {|_k, v| v.nil?}
    hsl = [:hue, :saturation, :lightness] & attrs.keys
    rgb = [:red, :green, :blue] & attrs.keys
    if !allow_both_rgb_and_hsl && !hsl.empty? && !rgb.empty?
      raise ArgumentError.new("Color.new(hash) may not have both HSL and RGB keys specified")
    elsif hsl.empty? && rgb.empty?
      raise ArgumentError.new("Color.new(hash) must have either HSL or RGB keys specified")
    elsif !hsl.empty? && hsl.size != 3
      raise ArgumentError.new("Color.new(hash) must have all three HSL values specified")
    elsif !rgb.empty? && rgb.size != 3
      raise ArgumentError.new("Color.new(hash) must have all three RGB values specified")
    end
    @attrs = attrs
    @attrs[:hue] %= 360 if @attrs[:hue]
    @attrs[:alpha] ||= 1
    @representation = @attrs.delete(:representation)
  end
  [:red, :green, :blue].each do |k|
    next if @attrs[k].nil?
    @attrs[k] = Sass::Util.restrict(Sass::Util.round(@attrs[k]), 0..255)
  end
  [:saturation, :lightness].each do |k|
    next if @attrs[k].nil?
    @attrs[k] = Sass::Util.restrict(@attrs[k], 0..100)
  end
  @attrs[:alpha] = Sass::Util.restrict(@attrs[:alpha], 0..1)
end