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

Track missing alpha channels in colors #2051

Merged
merged 2 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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
Loading