Skip to content

Commit 0132208

Browse files
committed
Fix alpha unit behavior
1 parent 0f5b99e commit 0132208

File tree

3 files changed

+24
-9
lines changed

3 files changed

+24
-9
lines changed

CHANGELOG.md

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,15 @@
11
## 1.65.0
22

3+
* **Breaking change**: Passing a number with unit `%` to the `$alpha` parameter
4+
of `color.change()`, `color.adjust()`, `change-color()`, and `adjust-color()`
5+
is now interpreted as a percentage, instead of ignoring the unit. For example,
6+
`color.change(red, $alpha: 50%)` now returns `rgb(255 0 0 / 0.5)`.
7+
38
* Add support for CSS Color Level 4 [color spaces]. Each color value now tracks
49
its color space along with the values of each channel in that color space.
510
There are two general principles to keep in mind when dealing with new color
611
spaces:
7-
12+
813
1. With the exception of legacy color spaces (`rgb`, `hsl`, and `hwb`), colors
914
will always be emitted in the color space they were defined in unless
1015
they're explicitly converted.

lib/src/functions/color.dart

+18-2
Original file line numberDiff line numberDiff line change
@@ -728,8 +728,24 @@ SassColor _changeColor(
728728
channelArgs[0] ?? SassNumber(color.channel0),
729729
channelArgs[1] ?? SassNumber(color.channel1, latterUnits),
730730
channelArgs[2] ?? SassNumber(color.channel2, latterUnits),
731-
alphaArg.andThen(
732-
(alphaArg) => _percentageOrUnitless(alphaArg, 1, 'alpha')) ??
731+
alphaArg.andThen((alphaArg) {
732+
if (!alphaArg.hasUnits) {
733+
return alphaArg.value;
734+
} else if (alphaArg.hasUnit('%')) {
735+
return alphaArg.value / 100;
736+
} else {
737+
warnForDeprecation(
738+
"\$alpha: Passing a unit other than % ($alphaArg) is "
739+
"deprecated.\n"
740+
"\n"
741+
"To preserve current behavior: "
742+
"${alphaArg.unitSuggestion('alpha')}\n"
743+
"\n"
744+
"See https://sass-lang.com/d/function-units",
745+
Deprecation.functionUnits);
746+
return alphaArg.value;
747+
}
748+
}) ??
733749
color.alpha);
734750
}
735751

test/deprecations_test.dart

-6
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,6 @@ void main() {
9898
_expectDeprecation("a {b: hsl(10deg, 0%, 0)}", Deprecation.functionUnits);
9999
});
100100

101-
test("an alpha value with a percent unit", () {
102-
_expectDeprecation(
103-
r"@use 'sass:color'; a {b: color.change(red, $alpha: 1%)}",
104-
Deprecation.functionUnits);
105-
});
106-
107101
test("an alpha value with a non-percent unit", () {
108102
_expectDeprecation(
109103
r"@use 'sass:color'; a {b: color.change(red, $alpha: 1px)}",

0 commit comments

Comments
 (0)