@@ -58,7 +58,7 @@ class ColorComputer {
58
58
if (memberName != null ) {
59
59
colorConst = _getMember (colorConst, memberName);
60
60
} else if (index != null ) {
61
- colorConst = _getSwatchValue (colorConst, index);
61
+ colorConst = _getSwatchColor (colorConst, index);
62
62
}
63
63
64
64
return _tryRecordColor (expression, colorConst);
@@ -77,37 +77,56 @@ class ColorComputer {
77
77
var classElement = staticElement? .enclosingElement3;
78
78
var className = classElement? .name;
79
79
var constructorName = constructor.name? .name;
80
- var constructorArgs = expression.argumentList.arguments
81
- .map ((e) => e is Literal ? e : null )
82
- .toList ();
80
+ var constructorArgs = expression.argumentList.arguments.toList ();
83
81
84
- int ? colorValue ;
82
+ ColorInformation ? color ;
85
83
if (_isDartUi (classElement) && className == 'Color' ) {
86
- colorValue = _getDartUiColorValue (constructorName, constructorArgs);
84
+ color = _getDartUiColor (constructorName, constructorArgs);
87
85
} else if (_isFlutterPainting (classElement) && className == 'ColorSwatch' ) {
88
- colorValue =
89
- _getFlutterSwatchColorValue (constructorName, constructorArgs);
86
+ color = _getFlutterSwatchColor (constructorName, constructorArgs);
90
87
} else if (_isFlutterMaterial (classElement) &&
91
88
className == 'MaterialAccentColor' ) {
92
- colorValue =
93
- _getFlutterMaterialAccentColorValue (constructorName, constructorArgs);
89
+ color = _getFlutterMaterialAccentColor (constructorName, constructorArgs);
94
90
}
95
91
96
- return _tryRecordColorValue (expression, colorValue );
92
+ return _tryRecordColorInformation (expression, color );
97
93
}
98
94
99
- /// Converts ARGB values into a single int value as 0xAARRGGBB as used by
100
- /// the dart:ui Color class.
101
- int _colorValueForComponents (int alpha, int red, int green, int blue) {
102
- return (alpha << 24 ) | (red << 16 ) | (green << 8 ) | (blue << 0 );
103
- }
104
-
105
- /// Extracts the color value from dart:ui Color constructor args.
106
- int ? _getDartUiColorValue (String ? name, List <Literal ?> args) {
95
+ /// Extracts the color information from dart:ui Color constructor args.
96
+ ColorInformation ? _getDartUiColor (String ? name, List <Expression > args) {
107
97
if (name == null && args.length == 1 ) {
98
+ // Color(0xFF000000).
108
99
var arg0 = args[0 ];
109
- return arg0 is IntegerLiteral ? arg0.value : null ;
100
+ return arg0 is IntegerLiteral ? getColorForInt (arg0.value) : null ;
101
+ } else if (name == 'from' ) {
102
+ // Color.from(alpha: 1, red: 1, green: 1, blue: 1).
103
+ double ? alpha, red, green, blue;
104
+ for (var arg in args.whereType <NamedExpression >()) {
105
+ var expression = arg.expression;
106
+ var value = expression is DoubleLiteral
107
+ ? expression.value
108
+ : expression is IntegerLiteral
109
+ ? expression.value? .toDouble ()
110
+ : null ;
111
+ switch (arg.name.label.name) {
112
+ case 'alpha' :
113
+ alpha = value;
114
+ case 'red' :
115
+ red = value;
116
+ case 'green' :
117
+ green = value;
118
+ case 'blue' :
119
+ blue = value;
120
+ }
121
+ }
122
+ return getColorForDoubles (
123
+ alpha: alpha,
124
+ red: red,
125
+ green: green,
126
+ blue: blue,
127
+ );
110
128
} else if (name == 'fromARGB' && args.length == 4 ) {
129
+ // Color.fromARGB(255, 255, 255, 255).
111
130
var arg0 = args[0 ];
112
131
var arg1 = args[1 ];
113
132
var arg2 = args[2 ];
@@ -119,9 +138,10 @@ class ColorComputer {
119
138
var blue = arg3 is IntegerLiteral ? arg3.value : null ;
120
139
121
140
return alpha != null && red != null && green != null && blue != null
122
- ? _colorValueForComponents (alpha, red, green, blue)
141
+ ? ColorInformation (alpha, red, green, blue)
123
142
: null ;
124
143
} else if (name == 'fromRGBO' && args.length == 4 ) {
144
+ // Color.fromRGBO(255, 255, 255, 1.0).
125
145
var arg0 = args[0 ];
126
146
var arg1 = args[1 ];
127
147
var arg2 = args[2 ];
@@ -138,24 +158,26 @@ class ColorComputer {
138
158
var alpha = opacity != null ? (opacity * 255 ).toInt () : null ;
139
159
140
160
return alpha != null && red != null && green != null && blue != null
141
- ? _colorValueForComponents (alpha, red, green, blue)
161
+ ? ColorInformation (alpha, red, green, blue)
142
162
: null ;
143
163
} else {
144
164
return null ;
145
165
}
146
166
}
147
167
148
- /// Extracts the color value from Flutter MaterialAccentColor constructor args.
149
- int ? _getFlutterMaterialAccentColorValue (String ? name, List <Literal ?> args) =>
168
+ /// Extracts the color from Flutter MaterialAccentColor constructor args.
169
+ ColorInformation ? _getFlutterMaterialAccentColor (
170
+ String ? name, List <Expression > args) =>
150
171
// MaterialAccentColor is a subclass of SwatchColor and has the same
151
172
// constructor.
152
- _getFlutterSwatchColorValue (name, args);
173
+ _getFlutterSwatchColor (name, args);
153
174
154
- /// Extracts the color value from Flutter ColorSwatch constructor args.
155
- int ? _getFlutterSwatchColorValue (String ? name, List <Literal ?> args) {
175
+ /// Extracts the color information from Flutter ColorSwatch constructor args.
176
+ ColorInformation ? _getFlutterSwatchColor (
177
+ String ? name, List <Expression > args) {
156
178
if (name == null && args.isNotEmpty) {
157
179
var arg0 = args[0 ];
158
- return arg0 is IntegerLiteral ? arg0.value : null ;
180
+ return arg0 is IntegerLiteral ? getColorForInt ( arg0.value) : null ;
159
181
} else {
160
182
return null ;
161
183
}
@@ -166,25 +188,25 @@ class ColorComputer {
166
188
/// Well-known getters like `shade500` will be mapped onto the swatch value
167
189
/// with a matching index.
168
190
DartObject ? _getMember (DartObject target, String memberName) {
169
- var colorValue = target.getFieldFromHierarchy (memberName);
170
- if (colorValue != null ) {
171
- return colorValue ;
191
+ var color = target.getFieldFromHierarchy (memberName);
192
+ if (color != null ) {
193
+ return color ;
172
194
}
173
195
174
- // If we didn't get a value but it's a getter we know how to read from a
196
+ // If we didn't get a color but it's a getter we know how to read from a
175
197
// swatch, try that.
176
198
if (memberName.startsWith ('shade' )) {
177
199
var shadeNumber = int .tryParse (memberName.substring (5 ));
178
200
if (shadeNumber != null ) {
179
- return _getSwatchValue (target, shadeNumber);
201
+ return _getSwatchColor (target, shadeNumber);
180
202
}
181
203
}
182
204
183
205
return null ;
184
206
}
185
207
186
208
/// Extracts a specific shade index from a Flutter SwatchColor.
187
- DartObject ? _getSwatchValue (DartObject target, int swatchValue) {
209
+ DartObject ? _getSwatchColor (DartObject target, int swatchValue) {
188
210
var swatch = target.getFieldFromHierarchy ('_swatch' )? .toMapValue ();
189
211
if (swatch == null ) return null ;
190
212
@@ -209,66 +231,90 @@ class ColorComputer {
209
231
element? .library? .identifier ==
210
232
'package:flutter/src/painting/colors.dart' ;
211
233
212
- /// Tries to record a color value from [colorConst] for [expression] .
234
+ /// Tries to record a color from [colorConst] for [expression] .
213
235
///
214
236
/// Returns whether a valid color was found and recorded.
215
237
bool _tryRecordColor (Expression expression, DartObject ? colorConst) =>
216
- _tryRecordColorValue (expression, _colorValueForColorConst (colorConst));
238
+ _tryRecordColorInformation (expression, getColorForObject (colorConst));
217
239
218
- /// Tries to record the [colorValue ] for [expression] .
240
+ /// Tries to record the [color ] for [expression] .
219
241
///
220
242
/// Returns whether a valid color was found and recorded.
221
- bool _tryRecordColorValue (Expression expression, int ? colorValue) {
222
- if (colorValue == null ) return false ;
223
-
224
- // Build color information from the Color value.
225
- var color = _colorInformationForColorValue (colorValue);
243
+ bool _tryRecordColorInformation (
244
+ Expression expression, ColorInformation ? color) {
245
+ if (color == null ) return false ;
226
246
227
247
// Record the color against the original entire expression.
228
248
_colors.add (ColorReference (expression.offset, expression.length, color));
229
249
return true ;
230
250
}
231
251
232
- static ColorInformation ? getColorForValue (DartObject object) {
233
- if (object.type.isColor) {
234
- var colorValue = _colorValueForColorConst (object);
235
- if (colorValue != null ) {
236
- return _colorInformationForColorValue (colorValue);
237
- }
238
- }
239
- return null ;
252
+ /// Gets [ColorInformation] from a set of doubles that are stored internally
253
+ /// in a dart:ui Color object.
254
+ static ColorInformation ? getColorForDoubles ({
255
+ required double ? alpha,
256
+ required double ? red,
257
+ required double ? green,
258
+ required double ? blue,
259
+ }) {
260
+ return alpha != null && red != null && green != null && blue != null
261
+ ? ColorInformation (
262
+ (alpha * 255.0 ).round () & 0xff ,
263
+ (red * 255.0 ).round () & 0xff ,
264
+ (green * 255.0 ).round () & 0xff ,
265
+ (blue * 255.0 ).round () & 0xff ,
266
+ )
267
+ : null ;
240
268
}
241
269
242
- /// Creates a [ColorInformation] by extracting the argb values from
243
- /// [value] encoded as 0xAARRGGBB as in the dart:ui Color class.
244
- static ColorInformation _colorInformationForColorValue (int value) {
245
- // Extract color information according to dart:ui Color values.
246
- var alpha = (0xff000000 & value) >> 24 ;
247
- var red = (0x00ff0000 & value) >> 16 ;
248
- var blue = (0x000000ff & value) >> 0 ;
249
- var green = (0x0000ff00 & value) >> 8 ;
250
-
251
- return ColorInformation (alpha, red, green, blue);
270
+ /// Gets [ColorInformation] from a value like `0xFFFF9000` which is used in
271
+ /// the default `Color()` constructor.
272
+ static ColorInformation ? getColorForInt (int ? value) {
273
+ return value != null
274
+ ? ColorInformation (
275
+ (value >> 24 ) & 0xff ,
276
+ (value >> 16 ) & 0xff ,
277
+ (value >> 8 ) & 0xff ,
278
+ value & 0xff ,
279
+ )
280
+ : null ;
252
281
}
253
282
254
- /// Extracts the integer color value from the dart:ui Color constant [color] .
255
- static int ? _colorValueForColorConst (DartObject ? color) {
256
- if (color == null || color.isNull) return null ;
283
+ /// Gets [ColorInformation] from the dart:ui Color object [color] .
284
+ static ColorInformation ? getColorForObject (DartObject ? color) {
285
+ if (color == null || color.isNull || ! color.type.isColor ) return null ;
257
286
258
287
// If the object has a "color" field, walk down to that, because some colors
259
288
// like CupertinoColors have a "value=0" with an overridden getter that
260
289
// would always result in a value representing black.
261
290
color = color.getFieldFromHierarchy ('color' ) ?? color;
262
291
263
- return color.getFieldFromHierarchy ('value' )? .toIntValue ();
292
+ var alpha = color.getFieldFromHierarchy ('a' )? .toDoubleValue ();
293
+ var red = color.getFieldFromHierarchy ('r' )? .toDoubleValue ();
294
+ var green = color.getFieldFromHierarchy ('g' )? .toDoubleValue ();
295
+ var blue = color.getFieldFromHierarchy ('b' )? .toDoubleValue ();
296
+
297
+ return getColorForDoubles (
298
+ alpha: alpha,
299
+ red: red,
300
+ green: green,
301
+ blue: blue,
302
+ );
264
303
}
265
304
}
266
305
267
306
/// Information about a color that is present in a document.
268
307
class ColorInformation {
308
+ /// Alpha as a value from 0 to 255.
269
309
final int alpha;
310
+
311
+ /// Red as a value from 0 to 255.
270
312
final int red;
313
+
314
+ /// Green as a value from 0 to 255.
271
315
final int green;
316
+
317
+ /// Blue as a value from 0 to 255.
272
318
final int blue;
273
319
274
320
ColorInformation (this .alpha, this .red, this .green, this .blue);
0 commit comments