color function

Color color({
  1. String? color,
  2. Color defaultColor = black,
})

Implementation

Color color({String? color, Color defaultColor = black}) {
  if (color == null) return defaultColor;

  final expression = RegExp(r'rgb\((\d{1,3}),(\d{1,3}),(\d{1,3})\)');
  final match = expression.firstMatch(color.replaceAll(' ', ''));

  if (match == null) return black;

  final red = int.parse(match.group(1)!);
  final green = int.parse(match.group(2)!);
  final blue = int.parse(match.group(3)!);

  return Color.fromRGBO(red, green, blue, 1);
}