Method: Sass::Script::Functions#scale_color

Defined in:
lib/sass/script/functions.rb

#scale_color($color, [$red], [$green], [$blue], [$saturation], [$lightness], [$alpha]) ⇒ Color

Fluidly scales one or more properties of a color. Unlike adjust-color, which changes a color's properties by fixed amounts, scale-color fluidly changes them based on how high or low they already are. That means that lightening an already-light color with scale-color won't change the lightness much, but lightening a dark color by the same amount will change it more dramatically. This has the benefit of making scale-color($color, ...) have a similar effect regardless of what $color is.

For example, the lightness of a color can be anywhere between 0% and 100%. If scale-color($color, $lightness: 40%) is called, the resulting color's lightness will be 40% of the way between its original lightness and 100. If scale-color($color, $lightness: -40%) is called instead, the lightness will be 40% of the way between the original and 0.

This can change the red, green, blue, saturation, value, and alpha properties. The properties are specified as keyword arguments. All arguments should be percentages between 0% and 100%.

All properties are optional. You can't specify both RGB properties ($red, $green, $blue) and HSL properties ($saturation, $value) at the same time.

Examples:

scale-color(hsl(120, 70%, 80%), $lightness: 50%) => hsl(120, 70%, 90%)
scale-color(rgb(200, 150%, 170%), $green: -40%, $blue: 70%) => rgb(200, 90, 229)
scale-color(hsl(200, 70%, 80%), $saturation: -90%, $alpha: -30%) => hsla(200, 7%, 80%, 0.7)

Parameters:

Returns:

Raises:

  • (ArgumentError)

    if any parameter is the wrong type or out-of bounds, or if RGB properties and HSL properties are adjusted at the same time



896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
# File 'lib/sass/script/functions.rb', line 896

def scale_color(color, kwargs)
  assert_type color, :Color, :color
  with = Sass::Util.map_hash({
      "red" => 255,
      "green" => 255,
      "blue" => 255,
      "saturation" => 100,
      "lightness" => 100,
      "alpha" => 1
    }) do |name, max|

    next unless val = kwargs.delete(name)
    assert_type val, :Number, name
    if !(val.numerator_units == ['%'] && val.denominator_units.empty?)
      raise ArgumentError.new("$#{name}: Amount #{val} must be a % (e.g. #{val.value}%)")
    else
      Sass::Util.check_range("$#{name}: Amount", -100..100, val, '%')
    end

    current = color.send(name)
    scale = val.value/100.0
    diff = scale > 0 ? max - current : current
    [name.to_sym, current + diff*scale]
  end

  unless kwargs.empty?
    name, val = kwargs.to_a.first
    raise ArgumentError.new("Unknown argument $#{name} (#{val})")
  end

  color.with(with)
end