parseColor static method
Color?
parseColor(
- dynamic value
)
Implementation
static Color? parseColor(dynamic value) {
if (value == null) return null;
if (value is Color) return value;
if (value is int) return Color(value);
if (value is String) {
String hex = value.trim();
if (hex.startsWith('#')) {
hex = hex.substring(1);
if (hex.length == 6) {
hex = 'FF$hex';
} else if (hex.length == 3) {
hex = 'FF${hex[0]}${hex[0]}${hex[1]}${hex[1]}${hex[2]}${hex[2]}';
}
final intVal = int.tryParse(hex, radix: 16);
if (intVal != null) return Color(intVal);
} else if (hex.startsWith('rgba(') || hex.startsWith('rgb(')) {
final match = RegExp(r'\d+').allMatches(hex).toList();
if (match.length >= 3) {
final r = int.parse(match[0].group(0)!);
final g = int.parse(match[1].group(0)!);
final b = int.parse(match[2].group(0)!);
double a = 1.0;
if (match.length >= 4) {
a = double.tryParse(match[3].group(0)!) ?? 1.0;
}
return Color.fromRGBO(r, g, b, a);
}
}
}
return null;
}