toColor method
Implementation
Color toColor() {
if (_value is Color) {
return _value as Color;
}
if (_value is String) {
final hex = _value as String;
if (hex.startsWith('#')) {
return Color(int.parse(hex.substring(1), radix: 16) + 0xFF000000);
}
return Color(int.parse(hex, radix: 16) + 0xFF000000);
}
if (_value is Map) {
String? hex = ((_value as Map)['hex'])?.value;
dynamic opacity = ((_value as Map)['opacity'] as Data<dynamic>?)?.value;
if (hex == null) throw Exception('Cannot convert $_value to color');
if (hex.startsWith('#')) hex = hex.substring(1);
int result = int.parse(hex, radix: 16);
if (opacity != null) {
opacity = (opacity is String ? int.tryParse(opacity) : opacity) ?? 1;
result += ((opacity * 255).round() << 24) as int;
} else {
result += 0xFF000000;
}
return Color(result);
}
throw Exception('Cannot convert $_value to color');
}