dart-sass/lib/src/functions.dart

125 lines
4.7 KiB
Dart
Raw Normal View History

2016-08-27 11:06:15 +02:00
// Copyright 2016 Google Inc. Use of this source code is governed by an
// MIT-style license that can be found in the LICENSE file or at
// https://opensource.org/licenses/MIT.
import 'callable.dart';
import 'environment.dart';
2016-09-20 23:59:53 +02:00
import 'exception.dart';
2016-08-27 11:06:15 +02:00
import 'value.dart';
void defineCoreFunctions(Environment environment) {
2016-09-21 18:30:41 +02:00
// ## RGB
2016-09-21 17:57:53 +02:00
2016-09-21 18:30:41 +02:00
environment.setFunction(
new BuiltInCallable("rgb", r"$red, $green, $blue", (arguments) {
2016-09-20 23:59:53 +02:00
var red = arguments[0].assertNumber("red");
var green = arguments[1].assertNumber("green");
var blue = arguments[2].assertNumber("blue");
return new SassColor.rgb(
_percentageOrUnitless(red, 255, "red").round(),
_percentageOrUnitless(green, 255, "green").round(),
_percentageOrUnitless(blue, 255, "blue").round());
2016-09-20 23:59:53 +02:00
}));
environment.setFunction(new BuiltInCallable.overloaded("rgba", [
2016-09-21 18:30:41 +02:00
r"$red, $green, $blue, $alpha",
r"$color, $alpha",
], [
(arguments) {
// TODO: support calc strings
var red = arguments[0].assertNumber("red");
var green = arguments[1].assertNumber("green");
var blue = arguments[2].assertNumber("blue");
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(),
_percentageOrUnitless(alpha, 1, "alpha"));
},
(arguments) {
var color = arguments[0].assertColor("color");
var alpha = arguments[0].assertNumber("alpha");
return color.change(alpha: _percentageOrUnitless(alpha, 1, "alpha"));
}
]));
2016-09-21 18:30:41 +02:00
environment.setFunction(new BuiltInCallable("red", r"$color", (arguments) {
return new SassNumber(arguments.first.assertColor("color").red);
}));
2016-09-21 17:58:02 +02:00
2016-09-21 18:30:41 +02:00
environment.setFunction(new BuiltInCallable("green", r"$color", (arguments) {
return new SassNumber(arguments.first.assertColor("color").green);
}));
2016-09-21 17:58:02 +02:00
2016-09-21 18:30:41 +02:00
environment.setFunction(new BuiltInCallable("blue", r"$color", (arguments) {
return new SassNumber(arguments.first.assertColor("color").blue);
}));
2016-09-21 17:58:02 +02:00
2016-09-21 17:57:53 +02:00
environment.setFunction(new BuiltInCallable(
2016-09-21 18:30:41 +02:00
"mix", r"$color1, $color2, $weight: 50%", (arguments) {
2016-09-21 17:57:53 +02:00
var color1 = arguments[0].assertColor("color1");
var color2 = arguments[1].assertColor("color2");
var weight = arguments[2].assertNumber("weight");
// This algorithm factors in both the user-provided weight (w) and the
// difference between the alpha values of the two colors (a) to decide how
// 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 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 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 color1 is renormalized to be within [0, 1] and the
// weight of color2 is given by 1 minus the weight of color1.
var weightScale = weight.valueInRange(0, 100, "weight") / 100;
var normalizedWeight = weightScale * 2 - 1;
var alphaDistance = color1.alpha - color2.alpha;
var combinedWeight1 = normalizedWeight * alphaDistance == -1
? normalizedWeight
: (normalizedWeight + alphaDistance) /
(1 + normalizedWeight * alphaDistance);
var weight1 = (combinedWeight1 + 1) / 2;
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(),
color1.alpha * weightScale + color2.alpha * (1 - weightScale));
}));
2016-09-21 18:30:41 +02:00
// ## Introspection
2016-09-21 17:57:53 +02:00
2016-09-21 18:30:41 +02:00
environment.setFunction(new BuiltInCallable("inspect", r"$value",
2016-09-21 17:57:53 +02:00
(arguments) => new SassString(arguments.first.toString())));
2016-08-27 11:06:15 +02:00
}
2016-09-20 23:59:53 +02:00
num _percentageOrUnitless(SassNumber number, num max, String name) {
2016-09-20 23:59:53 +02:00
num value;
if (!number.hasUnits) {
value = number.value;
} else if (number.hasUnit("%")) {
value = max * number.value / 100;
} else {
throw new InternalException(
'\$$name: Expected $number to have no units or "%".');
}
return value.clamp(0, max);
2016-09-20 23:59:53 +02:00
}