Skip to content

Commit 38b2eae

Browse files
committed
Track missing alpha channels in colors
See sass/sass#2831
1 parent 216ceb7 commit 38b2eae

File tree

4 files changed

+179
-45
lines changed

4 files changed

+179
-45
lines changed

lib/src/functions/color.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ Value _invert(List<Value> arguments) {
566566
var rgb = color.toSpace(ColorSpace.rgb);
567567
return _mixLegacy(
568568
SassColor.rgb(255.0 - rgb.channel0, 255.0 - rgb.channel1,
569-
255.0 - rgb.channel2, color.alpha),
569+
255.0 - rgb.channel2, color.alphaOrNull),
570570
color,
571571
weightNumber);
572572
}

lib/src/node/value/color.dart

+13-5
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,20 @@ import 'package:js/js.dart';
66

77
import '../../value.dart';
88
import '../reflection.dart';
9+
import '../utils.dart';
910

1011
/// The JavaScript `SassColor` class.
1112
final JSClass colorClass = () {
1213
var jsClass = createJSClass('sass.SassColor', (Object self, _Channels color) {
1314
if (color.red != null) {
14-
return SassColor.rgb(color.red!, color.green!, color.blue!, color.alpha);
15+
return SassColor.rgb(color.red!, color.green!, color.blue!,
16+
_handleUndefinedAlpha(color.alpha));
1517
} else if (color.saturation != null) {
16-
return SassColor.hsl(
17-
color.hue!, color.saturation!, color.lightness!, color.alpha);
18+
return SassColor.hsl(color.hue!, color.saturation!, color.lightness!,
19+
_handleUndefinedAlpha(color.alpha));
1820
} else {
19-
return SassColor.hwb(
20-
color.hue!, color.whiteness!, color.blackness!, color.alpha);
21+
return SassColor.hwb(color.hue!, color.whiteness!, color.blackness!,
22+
_handleUndefinedAlpha(color.alpha));
2123
}
2224
});
2325

@@ -70,6 +72,12 @@ final JSClass colorClass = () {
7072
return jsClass;
7173
}();
7274

75+
/// Converts an undefined [alpha] to 1.
76+
///
77+
/// This ensures that an explicitly null alpha will be treated as a missing
78+
/// component.
79+
num? _handleUndefinedAlpha(num? alpha) => isUndefined(alpha) ? 1 : alpha;
80+
7381
@JS()
7482
@anonymous
7583
class _Channels {

lib/src/parse/stylesheet.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -2433,7 +2433,7 @@ abstract class StylesheetParser extends Parser {
24332433
red,
24342434
green,
24352435
blue,
2436-
alpha,
2436+
alpha ?? 1,
24372437
// Don't emit four- or eight-digit hex colors as hex, since that's not
24382438
// yet well-supported in browsers.
24392439
alpha == null ? SpanColorFormat(scanner.spanFrom(start)) : null);

0 commit comments

Comments
 (0)