Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Further improve rounding.

Closes #1732
commit 2c537b14a290215c6e869cf4a8a86f5f67102da8 1 parent 2e0f33b
@nex3 nex3 authored
View
4 doc-src/SASS_CHANGELOG.md
@@ -3,6 +3,10 @@
* Table of contents
{:toc}
+## 3.4.15 (Unreleased)
+
+* Further improve rounding for various numeric operations.
+
## 3.4.14 (22 May 2015)
* Further avoid race conditions when caching.
View
4 lib/sass/script/functions.rb
@@ -1057,7 +1057,7 @@ def adjust_hue(color, degrees)
# @raise [ArgumentError] if `$color` isn't a color
def ie_hex_str(color)
assert_type color, :Color, :color
- alpha = (color.alpha * 255).round.to_s(16).rjust(2, '0')
+ alpha = Sass::Util.round(color.alpha * 255).to_s(16).rjust(2, '0')
identifier("##{alpha}#{color.send(:hex_str)[1..-1]}".upcase)
end
declare :ie_hex_str, [:color]
@@ -1716,7 +1716,7 @@ def percentage(number)
# @return [Sass::Script::Value::Number]
# @raise [ArgumentError] if `$number` isn't a number
def round(number)
- numeric_transformation(number) {|n| n.round}
+ numeric_transformation(number) {|n| Sass::Util.round(n)}
end
declare :round, [:number]
View
6 lib/sass/script/value/color.rb
@@ -232,7 +232,7 @@ def initialize(attrs, representation = nil, allow_both_rgb_and_hsl = false)
raise ArgumentError.new("Color.new(array) expects a three- or four-element array")
end
- red, green, blue = attrs[0...3].map {|c| c.round}
+ 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
@@ -258,7 +258,7 @@ def initialize(attrs, representation = nil, allow_both_rgb_and_hsl = false)
[:red, :green, :blue].each do |k|
next if @attrs[k].nil?
- @attrs[k] = Sass::Util.restrict(@attrs[k].round, 0..255)
+ @attrs[k] = Sass::Util.restrict(Sass::Util.round(@attrs[k]), 0..255)
end
[:saturation, :lightness].each do |k|
@@ -632,7 +632,7 @@ def hsl_to_rgb!
hue_to_rgb(m1, m2, h + 1.0 / 3),
hue_to_rgb(m1, m2, h),
hue_to_rgb(m1, m2, h - 1.0 / 3)
- ].map {|c| (c * 0xff).round}
+ ].map {|c| Sass::Util.round(c * 0xff)}
end
def hue_to_rgb(m1, m2, h)
View
11 lib/sass/util.rb
@@ -138,6 +138,17 @@ def restrict(value, range)
[[value, range.first].max, range.last].min
end
+ # Like [Fixnum.round], but leaves rooms for slight floating-point
+ # differences.
+ #
+ # @param value [Numeric]
+ # @return [Numeric]
+ def round(value)
+ # If the number is within epsilon of X.5, round up.
+ return value.ceil if (value % 1) - 0.5 > -0.00001
+ value.round
+ end
+
# Concatenates all strings that are adjacent in an array,
# while leaving other elements as they are.
#
Please sign in to comment.
Something went wrong with that request. Please try again.