@@ -7,7 +7,10 @@ import 'dart:math' as math;
77import 'package:meta/meta.dart' ;
88import 'package:source_span/source_span.dart' ;
99
10+ import '../deprecation.dart' ;
11+ import '../evaluation_context.dart' ;
1012import '../exception.dart' ;
13+ import '../io.dart' ;
1114import '../util/number.dart' ;
1215import '../value.dart' ;
1316import '../visitor/interface/value.dart' ;
@@ -98,52 +101,63 @@ class SassColor extends Value {
98101
99102 /// Creates an RGB color.
100103 ///
104+ /// Passing `null` to [alpha] is deprecated, and will change behavior in
105+ /// future versions of Dart Sass to represent a [missing component] instead of
106+ /// being equivalent to `1` . Callers who want to create opaque colors should
107+ /// explicitly pass `1` or not pass [alpha] at all.
108+ ///
109+ /// [missing component] : https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#missing_color_components
110+ ///
101111 /// Throws a [RangeError] if [red] , [green] , and [blue] aren't between `0` and
102112 /// `255` , or if [alpha] isn't between `0` and `1` .
103- SassColor .rgb (int red, int green, int blue, [num ? alpha])
104- : this .rgbInternal (red, green, blue, alpha);
113+ SassColor .rgb (int red, int green, int blue, [num ? alpha = 1 ])
114+ : this .rgbInternal (red, green, blue, _handleNullAlpha ( alpha) );
105115
106116 /// Like [SassColor.rgb] , but also takes a [format] parameter.
107117 ///
108118 /// @nodoc
109119 @internal
110120 SassColor .rgbInternal (this ._red, this ._green, this ._blue,
111- [num ? alpha, this .format])
112- : _alpha = alpha == null
113- ? 1
114- : fuzzyAssertRange (alpha.toDouble (), 0 , 1 , "alpha" ) {
121+ [num alpha = 1 , this .format])
122+ : _alpha = fuzzyAssertRange (alpha.toDouble (), 0 , 1 , "alpha" ) {
115123 RangeError .checkValueInInterval (red, 0 , 255 , "red" );
116124 RangeError .checkValueInInterval (green, 0 , 255 , "green" );
117125 RangeError .checkValueInInterval (blue, 0 , 255 , "blue" );
118126 }
119127
120128 /// Creates an HSL color.
121129 ///
130+ /// Passing `null` to [alpha] is deprecated, and will change behavior in
131+ /// future versions of Dart Sass to represent a [missing component] instead of
132+ /// being equivalent to `1` . Callers who want to create opaque colors should
133+ /// explicitly pass `1` or not pass [alpha] at all.
134+ ///
135+ /// [missing component] : https://developer.mozilla.org/en-US/docs/Web/CSS/color_value#missing_color_components
136+ ///
122137 /// Throws a [RangeError] if [saturation] or [lightness] aren't between `0`
123138 /// and `100` , or if [alpha] isn't between `0` and `1` .
124- SassColor .hsl (num hue, num saturation, num lightness, [num ? alpha])
125- : this .hslInternal (hue, saturation, lightness, alpha);
139+ SassColor .hsl (num hue, num saturation, num lightness, [num ? alpha = 1 ])
140+ : this .hslInternal (hue, saturation, lightness, _handleNullAlpha ( alpha) );
126141
127142 /// Like [SassColor.hsl] , but also takes a [format] parameter.
128143 ///
129144 /// @nodoc
130145 @internal
131146 SassColor .hslInternal (num hue, num saturation, num lightness,
132- [num ? alpha, this .format])
147+ [num alpha = 1 , this .format])
133148 : _hue = hue % 360 ,
134149 _saturation =
135150 fuzzyAssertRange (saturation.toDouble (), 0 , 100 , "saturation" ),
136151 _lightness =
137152 fuzzyAssertRange (lightness.toDouble (), 0 , 100 , "lightness" ),
138- _alpha = alpha == null
139- ? 1
140- : fuzzyAssertRange (alpha.toDouble (), 0 , 1 , "alpha" );
153+ _alpha = fuzzyAssertRange (alpha.toDouble (), 0 , 1 , "alpha" );
141154
142155 /// Creates an HWB color.
143156 ///
144157 /// Throws a [RangeError] if [whiteness] or [blackness] aren't between `0` and
145158 /// `100` , or if [alpha] isn't between `0` and `1` .
146- factory SassColor .hwb (num hue, num whiteness, num blackness, [num ? alpha]) {
159+ factory SassColor .hwb (num hue, num whiteness, num blackness,
160+ [num ? alpha = 1 ]) {
147161 // From https://www.w3.org/TR/css-color-4/#hwb-to-rgb
148162 var scaledHue = hue % 360 / 360 ;
149163 var scaledWhiteness =
@@ -171,6 +185,21 @@ class SassColor extends Value {
171185 toRgb (scaledHue - 1 / 3 ), alpha);
172186 }
173187
188+ /// Prints a deprecation warning if [alpha] is explicitly `null` .
189+ static num _handleNullAlpha (num ? alpha) {
190+ if (alpha != null ) return alpha;
191+
192+ warnForDeprecation (
193+ 'Passing null for alpha in the ${isJS ? 'JS' : 'Dart' } API is '
194+ 'deprecated.\n '
195+ 'To preserve current behavior, pass 1${isJS ? ' or undefined' : '' } '
196+ 'instead.'
197+ '\n '
198+ 'More info: https://sass-lang.com/d/null-alpha' ,
199+ Deprecation .nullAlpha);
200+ return 1 ;
201+ }
202+
174203 SassColor ._(this ._red, this ._green, this ._blue, this ._hue, this ._saturation,
175204 this ._lightness, this ._alpha)
176205 : format = null ;
0 commit comments