getColorFromHex method
Implementation
Color getColorFromHex() {
// Regular expression for validating hex color code
final hexRegex = RegExp(r'^#?([0-9A-Fa-f]{6}|[0-9A-Fa-f]{8})$');
// If the string doesn't match the regex pattern, throw an exception
if (!hexRegex.hasMatch(this)) {
throw FormatException('Invalid Hex color code');
}
// Remove the '#' if it exists
String hexColor = replaceAll('#', '');
// Parse the color from the hex string
int colorValue = int.parse(hexColor, radix: 16);
// If it's a 6-character hex code, add full opacity (0xFF)
if (hexColor.length == 6) {
colorValue = 0xFF000000 | colorValue; // 0xFF is for full opacity
}
return Color(colorValue);
}