class Sass::Script::Color

A SassScript object representing a CSS color.

def div(other)

Raises:
  • (Sass::SyntaxError) - if `other` is a number with units

Returns:
  • (Color) - The resulting color

Parameters:
  • other (Literal) -- The right-hand side of the operator
def div(other)
  if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color)
    piecewise(other, :/)
  else
    super
  end
end

def initialize(rgb)

Raises:
  • (Sass::SyntaxError) - if any color value isn't between 0 and 255

Parameters:
  • rgb (Array) -- A three-element array of the red, green, and blue values (respectively)
def initialize(rgb)
  rgb = rgb.map {|c| c.to_i}
  raise Sass::SyntaxError.new("Color values must be between 0 and 255") if rgb.any? {|c| c < 0 || c > 255}
  super(rgb)
end

def minus(other)

Raises:
  • (Sass::SyntaxError) - if `other` is a number with units

Returns:
  • (Color) - The resulting color

Parameters:
  • other (Literal) -- The right-hand side of the operator
def minus(other)
  if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color)
    piecewise(other, :-)
  else
    super
  end
end

def mod(other)

Raises:
  • (Sass::SyntaxError) - if `other` is a number with units

Returns:
  • (Color) - The resulting color

Parameters:
  • other (Literal) -- The right-hand side of the operator
def mod(other)
  if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color)
    piecewise(other, :%)
  else
    raise NoMethodError.new(nil, :mod)
  end
end

def piecewise(other, operation)

def piecewise(other, operation)
  other_num = other.is_a? Number
  other_val = other.value
  if other_num && !other.unitless?
    raise Sass::SyntaxError.new("Cannot add a number with units (#{other}) to a color (#{self}).") 
  end
  rgb = []
  for i in (0...3)
    res = @value[i].send(operation, other_num ? other_val : other_val[i])
    rgb[i] = [ [res, 255].min, 0 ].max
  end
  Color.new(rgb)
end

def plus(other)

Raises:
  • (Sass::SyntaxError) - if `other` is a number with units

Returns:
  • (Color) - The resulting color

Parameters:
  • other (Literal) -- The right-hand side of the operator
def plus(other)
  if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color)
    piecewise(other, :+)
  else
    super
  end
end

def times(other)

Raises:
  • (Sass::SyntaxError) - if `other` is a number with units

Returns:
  • (Color) - The resulting color

Parameters:
  • other (Literal) -- The right-hand side of the operator
def times(other)
  if other.is_a?(Sass::Script::Number) || other.is_a?(Sass::Script::Color)
    piecewise(other, :*)
  else
    raise NoMethodError.new(nil, :times)
  end
end

def to_s

Returns:
  • (String) - The string representation
def to_s
  return HTML4_COLORS_REVERSE[@value] if HTML4_COLORS_REVERSE[@value]
  red, green, blue = @value.map { |num| num.to_s(16).rjust(2, '0') }
  "##{red}#{green}#{blue}"
end