Skip to content

Commit

Permalink
Track missing alpha channels in colors
Browse files Browse the repository at this point in the history
  • Loading branch information
nex3 committed Aug 1, 2023
1 parent 216ceb7 commit 38b2eae
Show file tree
Hide file tree
Showing 4 changed files with 179 additions and 45 deletions.
2 changes: 1 addition & 1 deletion lib/src/functions/color.dart
Original file line number Diff line number Diff line change
Expand Up @@ -566,7 +566,7 @@ Value _invert(List<Value> arguments) {
var rgb = color.toSpace(ColorSpace.rgb);
return _mixLegacy(
SassColor.rgb(255.0 - rgb.channel0, 255.0 - rgb.channel1,
255.0 - rgb.channel2, color.alpha),
255.0 - rgb.channel2, color.alphaOrNull),
color,
weightNumber);
}
Expand Down
18 changes: 13 additions & 5 deletions lib/src/node/value/color.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,20 @@ import 'package:js/js.dart';

import '../../value.dart';
import '../reflection.dart';
import '../utils.dart';

/// The JavaScript `SassColor` class.
final JSClass colorClass = () {
var jsClass = createJSClass('sass.SassColor', (Object self, _Channels color) {
if (color.red != null) {
return SassColor.rgb(color.red!, color.green!, color.blue!, color.alpha);
return SassColor.rgb(color.red!, color.green!, color.blue!,
_handleUndefinedAlpha(color.alpha));
} else if (color.saturation != null) {
return SassColor.hsl(
color.hue!, color.saturation!, color.lightness!, color.alpha);
return SassColor.hsl(color.hue!, color.saturation!, color.lightness!,
_handleUndefinedAlpha(color.alpha));
} else {
return SassColor.hwb(
color.hue!, color.whiteness!, color.blackness!, color.alpha);
return SassColor.hwb(color.hue!, color.whiteness!, color.blackness!,
_handleUndefinedAlpha(color.alpha));
}
});

Expand Down Expand Up @@ -70,6 +72,12 @@ final JSClass colorClass = () {
return jsClass;
}();

/// Converts an undefined [alpha] to 1.
///
/// This ensures that an explicitly null alpha will be treated as a missing
/// component.
num? _handleUndefinedAlpha(num? alpha) => isUndefined(alpha) ? 1 : alpha;

@JS()
@anonymous
class _Channels {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/parse/stylesheet.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2433,7 +2433,7 @@ abstract class StylesheetParser extends Parser {
red,
green,
blue,
alpha,
alpha ?? 1,
// Don't emit four- or eight-digit hex colors as hex, since that's not
// yet well-supported in browsers.
alpha == null ? SpanColorFormat(scanner.spanFrom(start)) : null);
Expand Down
Loading

0 comments on commit 38b2eae

Please sign in to comment.