toColor method

Color toColor()

Converts a hex color string to a Color object.

Implementation

Color toColor() {
  var hexStringColor = this;
  final buffer = StringBuffer();

  // Validate hex string length and format
  if (hexStringColor.length == 6 || hexStringColor.length == 7) {
    buffer.write('ff'); // Add 'ff' for full opacity
    buffer
        .write(hexStringColor.replaceFirst("#", "")); // Remove '#' if present
    return Color(
        int.parse(buffer.toString(), radix: 16)); // Parse and create Color
  }
  throw ArgumentError('Invalid hex color format'); // Invalid format
}