Skip to content

Commit

Permalink
Fix alpha unit behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
nex3 committed Aug 2, 2023
1 parent 0f5b99e commit 942699c
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 9 deletions.
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
## 1.65.0

* **Breaking change**: Passing a number with unit `%` to the `$alpha` parameter
of `color.change()`, `color.adjust()`, `change-color()`, and `adjust-color()`
is now interpreted as a percentage, instead of ignoring the unit. For example,
`color.change(red, $alpha: 50%)` now returns `rgb(255 0 0 / 0.5)`.

* Add support for CSS Color Level 4 [color spaces]. Each color value now tracks
its color space along with the values of each channel in that color space.
There are two general principles to keep in mind when dealing with new color
spaces:

1. With the exception of legacy color spaces (`rgb`, `hsl`, and `hwb`), colors
will always be emitted in the color space they were defined in unless
they're explicitly converted.
Expand Down
20 changes: 18 additions & 2 deletions lib/src/functions/color.dart
Original file line number Diff line number Diff line change
Expand Up @@ -728,8 +728,24 @@ SassColor _changeColor(
channelArgs[0] ?? SassNumber(color.channel0),
channelArgs[1] ?? SassNumber(color.channel1, latterUnits),
channelArgs[2] ?? SassNumber(color.channel2, latterUnits),
alphaArg.andThen(
(alphaArg) => _percentageOrUnitless(alphaArg, 1, 'alpha')) ??
alphaArg.andThen((alphaArg) {
if (!alphaArg.hasUnits) {
return alphaArg.value;
} else if (alphaArg.hasUnit('%')) {
return alphaArg.value / 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.value;
}
}) ??
color.alpha);
}

Expand Down
6 changes: 0 additions & 6 deletions test/deprecations_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,6 @@ void main() {
_expectDeprecation("a {b: hsl(10deg, 0%, 0)}", Deprecation.functionUnits);
});

test("an alpha value with a percent unit", () {
_expectDeprecation(
r"@use 'sass:color'; a {b: color.change(red, $alpha: 1%)}",
Deprecation.functionUnits);
});

test("an alpha value with a non-percent unit", () {
_expectDeprecation(
r"@use 'sass:color'; a {b: color.change(red, $alpha: 1px)}",
Expand Down

0 comments on commit 942699c

Please sign in to comment.