colorResolver function

Color colorResolver(
  1. String? value
)

Implementation

Color colorResolver(String? value) {
  if (value == null) return Colors.transparent;
  if (value.startsWith('transparent')) return Colors.transparent;
  if (value.startsWith('#')) return HexColor.fromHex(value);
  if (value.startsWith('rgba')) {
    var matchColorValues = RegExp(r'rgba?\((\d+),(\d+),(\d+),?([\d.]+)?');
    final match = matchColorValues.firstMatch(value);

    int r = int.parse(match!.group(1)!);
    int g = int.parse(match.group(2)!);
    int b = int.parse(match.group(3)!);
    double a = double.parse(match.group(4)!);
    return Color.fromRGBO(r, g, b, a);
  }
  if (value.startsWith('rgb')) {
    var matchColorValues = RegExp(r'rgba?\((\d+),(\d+),(\d+),?([\d.]+)?');
    final match = matchColorValues.firstMatch(value);

    int r = int.parse(match!.group(1)!);
    int g = int.parse(match.group(2)!);
    int b = int.parse(match.group(3)!);
    return Color.fromRGBO(r, g, b, 1);
  }

  return Colors.transparent;
}