Color.fromHEX constructor
Color.fromHEX(
- String hex
Implementation
factory Color.fromHEX(String hex) {
// Check if string contains '#'
if (hex.contains('#', 1) || // Contains # after first position
(hex.contains('#') &&
hex.length != 7) || // Contains # but length is invalid
(!hex.contains('#') && hex.length != 6)) {
// Lenght is invalid
throw Exception("Invalid hex value");
}
// Remove trailing #
if (hex.contains('#')) hex = hex.substring(1);
// Parse values
try {
return Color(
int.parse(hex.substring(0, 2), radix: 16),
int.parse(hex.substring(2, 4), radix: 16),
int.parse(hex.substring(4, 6), radix: 16));
} catch (e) {
throw Exception("Invalid hex value");
}
}