Skip to content

Commit dedb23a

Browse files
committed
Fixed color parsing.
1 parent 3318494 commit dedb23a

File tree

2 files changed

+75
-0
lines changed

2 files changed

+75
-0
lines changed

Diff for: lib/util/utils.dart

+15
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,8 @@ class Utils {
343343

344344
static Color? getColor(dynamic value) {
345345
if (value is String) {
346+
value = value.trim();
347+
346348
// Check for hexadecimal color pattern (with or without alpha). It begins with #
347349
RegExp hexColor = RegExp(r'^#?([0-9a-fA-F]{6}|[0-9a-fA-F]{8})$');
348350
if (hexColor.hasMatch(value)) {
@@ -352,6 +354,10 @@ class Utils {
352354
if (hexValue.length == 6) {
353355
hexValue = 'FF$hexValue'; // Add full opacity for RGB values
354356
}
357+
// else move alpha to the front (Flutter specific)
358+
else if (hexValue.length == 8) {
359+
hexValue = "${hexValue.substring(6, 8)}${hexValue.substring(0, 6)}";
360+
}
355361
// Convert to an integer and create a Color object
356362
try {
357363
return Color(int.parse('0x$hexValue'));
@@ -360,7 +366,16 @@ class Utils {
360366
print('Failed to convert hex to Color: $e');
361367
return null;
362368
}
369+
} else if (value.startsWith("0x")) {
370+
try {
371+
return Color(int.parse(value));
372+
} catch (e) {
373+
debugPrint("fail to convert 0x to Color");
374+
return null;
375+
}
363376
}
377+
378+
// check for name values
364379
switch (value) {
365380
case '.transparent':
366381
case 'transparent':

Diff for: test/utils_test.dart

+60
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,66 @@ void main() {
310310
expect(hotelsSortWithRestaurantKey.last['price'], 100);
311311
});
312312
});
313+
314+
group('getColor Tests', () {
315+
const myColor = Color(0xff123456);
316+
317+
test('color hex with #', () {
318+
expect(Utils.getColor('#123456'), equals(myColor));
319+
});
320+
321+
test('color hex without #', () {
322+
expect(Utils.getColor('123456'), equals(myColor));
323+
});
324+
325+
test('hex with alpha and #', () {
326+
expect(Utils.getColor('#123456ff'), equals(myColor));
327+
});
328+
329+
test('hex with alpha', () {
330+
expect(Utils.getColor('123456ff'), equals(myColor));
331+
});
332+
333+
test('returns null for invalid hex string', () {
334+
expect(Utils.getColor('12345g'), isNull);
335+
});
336+
337+
test('returns Color for valid integer color value', () {
338+
expect(Utils.getColor(0xff123456), equals(const Color(0xff123456)));
339+
});
340+
341+
test('test color as a string', () {
342+
expect(Utils.getColor("0xff123456"), equals(const Color(0xff123456)));
343+
});
344+
345+
test('returns correct Color for named color strings', () {
346+
expect(Utils.getColor('red'), equals(Colors.red));
347+
expect(Utils.getColor('blue'), equals(Colors.blue));
348+
});
349+
350+
test('returns Colors.transparent for transparent string', () {
351+
expect(Utils.getColor('transparent'), equals(Colors.transparent));
352+
});
353+
354+
test('returns null for non-string, non-int types', () {
355+
expect(Utils.getColor([255, 0, 0]), isNull);
356+
});
357+
358+
test('returns null for undefined named colors', () {
359+
expect(Utils.getColor('not a color'), isNull);
360+
});
361+
362+
test('returns correct Color for transparent keyword variations', () {
363+
expect(Utils.getColor('.transparent'), equals(Colors.transparent));
364+
});
365+
366+
// Additional tests for each named color
367+
test('returns correct Color for each named color string', () {
368+
expect(Utils.getColor('black'), equals(Colors.black));
369+
expect(Utils.getColor('white'), equals(Colors.white));
370+
// Continue testing all other named colors...
371+
});
372+
});
313373
}
314374

315375
void assertIconEquality(IconModel first, IconModel second) {

0 commit comments

Comments
 (0)