Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix rounding behavior. #147

Merged
merged 1 commit into from
May 30, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

* Properly parse unary operators immediately after commas.

* Match Ruby Sass's rounding behavior for all functions.

* Fix a number of `@extend` bugs:

* `selector-extend()` and `selector-replace()` now allow compound selector
Expand Down
33 changes: 18 additions & 15 deletions lib/src/functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ void defineCoreFunctions(Environment environment) {
var alpha = arguments[3].assertNumber("alpha");

return new SassColor.rgb(
_percentageOrUnitless(red, 255, "red").round(),
_percentageOrUnitless(green, 255, "green").round(),
_percentageOrUnitless(blue, 255, "blue").round(),
fuzzyRound(_percentageOrUnitless(red, 255, "red")),
fuzzyRound(_percentageOrUnitless(green, 255, "green")),
fuzzyRound(_percentageOrUnitless(blue, 255, "blue")),
_percentageOrUnitless(alpha, 1, "alpha"));
},
(arguments) {
Expand Down Expand Up @@ -308,9 +308,9 @@ void defineCoreFunctions(Environment environment) {
getInRange(String name, num min, num max) =>
keywords.remove(name)?.assertNumber(name)?.valueInRange(min, max, name);

var red = getInRange("red", -255, 255)?.round();
var green = getInRange("green", -255, 255)?.round();
var blue = getInRange("blue", -255, 255)?.round();
var red = _fuzzyRoundOrNull(getInRange("red", -255, 255));
var green = _fuzzyRoundOrNull(getInRange("green", -255, 255));
var blue = _fuzzyRoundOrNull(getInRange("blue", -255, 255));
var hue = keywords.remove("hue")?.assertNumber("hue")?.value;
var saturation = getInRange("saturation", -100, 100);
var lightness = getInRange("lightness", -100, 100);
Expand Down Expand Up @@ -393,9 +393,9 @@ void defineCoreFunctions(Environment environment) {
}

return color.changeRgb(
red: scaleValue(color.red, red, 255).round(),
green: scaleValue(color.green, green, 255).round(),
blue: scaleValue(color.blue, blue, 255).round(),
red: fuzzyRound(scaleValue(color.red, red, 255)),
green: fuzzyRound(scaleValue(color.green, green, 255)),
blue: fuzzyRound(scaleValue(color.blue, blue, 255)),
alpha: scaleValue(color.alpha, alpha, 1));
} else if (hasHsl) {
return color.changeHsl(
Expand Down Expand Up @@ -423,9 +423,9 @@ void defineCoreFunctions(Environment environment) {
getInRange(String name, num min, num max) =>
keywords.remove(name)?.assertNumber(name)?.valueInRange(min, max, name);

var red = getInRange("red", 0, 255)?.round();
var green = getInRange("green", 0, 255)?.round();
var blue = getInRange("blue", 0, 255)?.round();
var red = _fuzzyRoundOrNull(getInRange("red", 0, 255));
var green = _fuzzyRoundOrNull(getInRange("green", 0, 255));
var blue = _fuzzyRoundOrNull(getInRange("blue", 0, 255));
var hue = keywords.remove("hue")?.assertNumber("hue")?.value;
var saturation = getInRange("saturation", 0, 100);
var lightness = getInRange("lightness", 0, 100);
Expand Down Expand Up @@ -1008,9 +1008,9 @@ SassColor _mix(SassColor color1, SassColor color2, SassNumber weight) {
var weight2 = 1 - weight1;

return new SassColor.rgb(
(color1.red * weight1 + color2.red * weight2).round(),
(color1.green * weight1 + color2.green * weight2).round(),
(color1.blue * weight1 + color2.blue * weight2).round(),
fuzzyRound(color1.red * weight1 + color2.red * weight2),
fuzzyRound(color1.green * weight1 + color2.green * weight2),
fuzzyRound(color1.blue * weight1 + color2.blue * weight2),
color1.alpha * weightScale + color2.alpha * (1 - weightScale));
}

Expand Down Expand Up @@ -1077,3 +1077,6 @@ CompoundSelector _prependParent(CompoundSelector compound) {
<SimpleSelector>[new ParentSelector()]..addAll(compound.components));
}
}

/// Like [fuzzyRound], but returns `null` if [number] is `null`.
int _fuzzyRoundOrNull(num number) => number == null ? null : fuzzyRound(number);