Skip to content
This repository has been archived by the owner on Apr 4, 2019. It is now read-only.

Commit

Permalink
Change mix()'s arguments from 'color_1','color_2' to 'color1','color2'
Browse files Browse the repository at this point in the history
  * Update tests to reflect changes
  • Loading branch information
JonathanTR committed Dec 14, 2013
1 parent 5e75094 commit 603cbf4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 15 deletions.
24 changes: 12 additions & 12 deletions lib/sass/script/functions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1196,9 +1196,9 @@ def change_color(color, kwargs)
# @return [Sass::Script::Value::Color]
# @raise [ArgumentError] if `$weight` is out of bounds or any parameter is
# the wrong type
def mix(color_1, color_2, weight = number(50))
assert_type color_1, :Color, :color_1
assert_type color_2, :Color, :color_2
def mix(color1, color2, weight = number(50))
assert_type color1, :Color, :color1
assert_type color2, :Color, :color2
assert_type weight, :Number, :weight

Sass::Util.check_range("Weight", 0..100, weight, '%')
Expand All @@ -1208,33 +1208,33 @@ def mix(color_1, color_2, weight = number(50))
# to perform the weighted average of the two RGB values.
#
# It works by first normalizing both parameters to be within [-1, 1],
# where 1 indicates "only use color_1", -1 indicates "only use color_2", and
# where 1 indicates "only use color1", -1 indicates "only use color2", and
# all values in between indicated a proportionately weighted average.
#
# Once we have the normalized variables w and a, we apply the formula
# (w + a)/(1 + w*a) to get the combined weight (in [-1, 1]) of color_1.
# (w + a)/(1 + w*a) to get the combined weight (in [-1, 1]) of color1.
# This formula has two especially nice properties:
#
# * When either w or a are -1 or 1, the combined weight is also that number
# (cases where w * a == -1 are undefined, and handled as a special case).
#
# * When a is 0, the combined weight is w, and vice versa.
#
# Finally, the weight of color_1 is renormalized to be within [0, 1]
# and the weight of color_2 is given by 1 minus the weight of color_1.
# Finally, the weight of color1 is renormalized to be within [0, 1]
# and the weight of color2 is given by 1 minus the weight of color1.
p = (weight.value / 100.0).to_f
w = p * 2 - 1
a = color_1.alpha - color_2.alpha
a = color1.alpha - color2.alpha

w1 = ((w * a == -1 ? w : (w + a) / (1 + w * a)) + 1) / 2.0
w2 = 1 - w1

rgba = color_1.rgb.zip(color_2.rgb).map {|v1, v2| v1 * w1 + v2 * w2}
rgba << color_1.alpha * p + color_2.alpha * (1 - p)
rgba = color1.rgb.zip(color2.rgb).map {|v1, v2| v1 * w1 + v2 * w2}
rgba << color1.alpha * p + color2.alpha * (1 - p)
rgb_color(*rgba)
end
declare :mix, [:color_1, :color_2]
declare :mix, [:color_1, :color_2, :weight]
declare :mix, [:color1, :color2]
declare :mix, [:color1, :color2, :weight]

# Converts a color to grayscale. This is identical to `desaturate(color,
# 100%)`.
Expand Down
6 changes: 3 additions & 3 deletions test/sass/functions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -799,12 +799,12 @@ def test_mix
assert_equal("blue", evaluate("mix(transparentize(#f00, 1), #00f, 0%)"))
assert_equal("rgba(0, 0, 255, 0)", evaluate("mix(#f00, transparentize(#00f, 1), 0%)"))
assert_equal("rgba(255, 0, 0, 0)", evaluate("mix(transparentize(#f00, 1), #00f, 100%)"))
assert_equal("rgba(255, 0, 0, 0)", evaluate("mix($color-1: transparentize(#f00, 1), $color-2: #00f, $weight: 100%)"))
assert_equal("rgba(255, 0, 0, 0)", evaluate("mix($color1: transparentize(#f00, 1), $color2: #00f, $weight: 100%)"))
end

def test_mix_tests_types
assert_error_message("$color-1: \"foo\" is not a color for `mix'", "mix(\"foo\", #f00, 10%)")
assert_error_message("$color-2: \"foo\" is not a color for `mix'", "mix(#f00, \"foo\", 10%)")
assert_error_message("$color1: \"foo\" is not a color for `mix'", "mix(\"foo\", #f00, 10%)")
assert_error_message("$color2: \"foo\" is not a color for `mix'", "mix(#f00, \"foo\", 10%)")
assert_error_message("$weight: \"foo\" is not a number for `mix'", "mix(#f00, #baf, \"foo\")")
end

Expand Down

0 comments on commit 603cbf4

Please sign in to comment.