class Sass::Script::Color

def initialize(attrs, 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:
  • rgba (Array) -- A three- or four-element array
  • attrs ({Symbol => Numeric}) -- A hash of color attributes to values

Overloads:
  • initialize(rgba)
  • initialize(attrs)
def initialize(attrs, 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| c.to_i}
    @attrs = {:red => red, :green => green, :blue => blue}
    @attrs[:alpha] = attrs[3] ? attrs[3].to_f : 1
  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
  end
  [:red, :green, :blue].each do |k|
    next if @attrs[k].nil?
    @attrs[k] = @attrs[k].to_i
    next if (0..255).include?(@attrs[k])
    raise Sass::SyntaxError.new("#{k.to_s.capitalize} value must be between 0 and 255")
  end
  [:saturation, :lightness].each do |k|
    next if @attrs[k].nil?
    @attrs[k] = 0 if @attrs[k] < 0.00001 && @attrs[k] > -0.00001
    @attrs[k] = 100 if @attrs[k] - 100 < 0.00001 && @attrs[k] - 100 > -0.00001
    next if (0..100).include?(@attrs[k])
    raise Sass::SyntaxError.new("#{k.to_s.capitalize} must be between 0 and 100")
  end
  unless (0..1).include?(@attrs[:alpha])
    raise Sass::SyntaxError.new("Alpha channel must between 0 and 1")
  end
end