Add a color.hwb() function (#1092)

See sass/sass#2834
See #1089
This commit is contained in:
Natalie Weizenbaum 2020-09-23 11:45:31 -07:00 committed by GitHub
parent 1918674295
commit d3691d44f0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -265,6 +265,22 @@ final module = BuiltInModule("color", functions: [
return color.changeHsl(saturation: 0);
}),
// ### HWB
BuiltInCallable.overloadedFunction("hwb", {
r"$hue, $whiteness, $blackness, $alpha: 1": (arguments) => _hwb(arguments),
r"$channels": (arguments) {
var parsed = _parseChannels(
"hwb", [r"$hue", r"$whiteness", r"$blackness"], arguments.first);
// `hwb()` doesn't (currently) support special number or variable strings.
if (parsed is SassString) {
throw SassScriptException('Expected numeric channels, got "$parsed".');
} else {
return _hwb(parsed as List<Value>);
}
}
}),
// ### Opacity
_removedColorFunction("opacify", "alpha"),
_removedColorFunction("fade-in", "alpha"),
@ -636,6 +652,25 @@ Value _hsl(String name, List<Value> arguments) {
: _percentageOrUnitless(alpha.assertNumber("alpha"), 1, "alpha"));
}
/// Create an HWB color from the given [arguments].
Value _hwb(List<Value> arguments) {
var alpha = arguments.length > 3 ? arguments[3] : null;
var hue = arguments[0].assertNumber("hue");
var whiteness = arguments[1].assertNumber("whiteness");
var blackness = arguments[2].assertNumber("blackness");
whiteness.assertUnit("%", "whiteness");
blackness.assertUnit("%", "whiteness");
return SassColor.hwb(
hue.value,
whiteness.valueInRange(0, 100, "whiteness"),
blackness.valueInRange(0, 100, "whiteness"),
alpha == null
? null
: _percentageOrUnitless(alpha.assertNumber("alpha"), 1, "alpha"));
}
Object /* SassString | List<Value> */ _parseChannels(
String name, List<String> argumentNames, Value channels) {
if (channels.isVar) return _functionString(name, [channels]);