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

[Color 4] Support none alpha values in color.change() #2233

Merged
merged 1 commit into from
May 6, 2024
Merged
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
50 changes: 26 additions & 24 deletions lib/src/functions/color.dart
Original file line number Diff line number Diff line change
Expand Up @@ -711,7 +711,7 @@ SassColor _updateComponents(List<Value> arguments,
var spaceKeyword = keywords.remove("space")?.assertString("space")
?..assertUnquoted("space");

var alphaArg = keywords.remove('alpha')?.assertNumber('alpha');
var alphaArg = keywords.remove('alpha');

// For backwards-compatibility, we allow legacy colors to modify channels in
// any legacy color space.
Expand Down Expand Up @@ -743,9 +743,10 @@ SassColor _updateComponents(List<Value> arguments,
for (var i = 0; i < channelInfo.length; i++)
channelArgs[i]?.assertNumber(channelInfo[i].name)
];
var alphaNumber = alphaArg?.assertNumber("alpha");
result = scale
? _scaleColor(color, channelNumbers, alphaArg)
: _adjustColor(color, channelNumbers, alphaArg);
? _scaleColor(color, channelNumbers, alphaNumber)
: _adjustColor(color, channelNumbers, alphaNumber);
}

return result.toSpace(originalColor.space);
Expand All @@ -754,32 +755,33 @@ SassColor _updateComponents(List<Value> arguments,
/// Returns a copy of [color] with its channel values replaced by those in
/// [channelArgs] and [alphaArg], if specified.
SassColor _changeColor(
SassColor color, List<Value?> channelArgs, SassNumber? alphaArg) =>
SassColor color, List<Value?> channelArgs, Value? alphaArg) =>
_colorFromChannels(
color.space,
_channelForChange(channelArgs[0], color, 0),
_channelForChange(channelArgs[1], color, 1),
_channelForChange(channelArgs[2], color, 2),
alphaArg.andThen((alphaArg) {
if (!alphaArg.hasUnits) {
return alphaArg.valueInRange(0, 1, "alpha");
} else if (alphaArg.hasUnit('%')) {
return alphaArg.valueInRangeWithUnit(0, 100, "alpha", "%") /
100;
} else {
warnForDeprecation(
"\$alpha: Passing a unit other than % ($alphaArg) is "
"deprecated.\n"
"\n"
"To preserve current behavior: "
"${alphaArg.unitSuggestion('alpha')}\n"
"\n"
"See https://sass-lang.com/d/function-units",
Deprecation.functionUnits);
return alphaArg.valueInRange(0, 1, "alpha");
}
}) ??
color.alpha,
switch (alphaArg) {
null => color.alpha,
_ when _isNone(alphaArg) => null,
SassNumber(hasUnits: false) => alphaArg.valueInRange(0, 1, "alpha"),
SassNumber() when alphaArg.hasUnit('%') =>
alphaArg.valueInRangeWithUnit(0, 100, "alpha", "%") / 100,
SassNumber() => () {
warnForDeprecation(
"\$alpha: Passing a unit other than % ($alphaArg) is "
"deprecated.\n"
"\n"
"To preserve current behavior: "
"${alphaArg.unitSuggestion('alpha')}\n"
"\n"
"See https://sass-lang.com/d/function-units",
Deprecation.functionUnits);
return alphaArg.valueInRange(0, 1, "alpha");
}(),
_ => throw SassScriptException(
'$alphaArg is not a number or unquoted "none".', 'alpha')
},
clamp: false);

/// Returns the value for a single channel in `color.change()`.
Expand Down
Loading