colorParse static method

Color? colorParse(
  1. String? text
)

Implementation

static Color? colorParse(String? text) {
  try {
    String t = text?.replaceAll('#', '').trim().toLowerCase() ?? '';
    if (!t.startsWith('0x')) {
      if (t.length < 3) {
        throw Exception('Length less than 3.');
      }

      t = switch (t.length) {
        3 => 'ff${t[0]}${t[0]}${t[1]}${t[1]}${t[2]}${t[2]}',
        4 => '${t[0]}${t[0]}${t[1]}${t[1]}${t[2]}${t[2]}${t[3]}${t[3]}',
        5 => '',
        6 => 'ff$t',
        7 => '',
        _ => t
      };

      t = '0x$t';
    }

    if (t.length > 10) {
      t = t.substring(0, 10);
    }

    return Color(int.parse(t));
  } on Exception catch (_) {
    return null;
  }
}