fromHex static method

Color fromHex(
  1. String? hexString,
  2. Color fallColor
)

String is in the format "aabbcc" or "ffaabbcc" with an optional leading "#".

Implementation

static Color fromHex(String? hexString, Color fallColor) {
  if (hexString == null) return fallColor;
  final StringBuffer buffer = StringBuffer();
  if (hexString.length == 6 || hexString.length == 7) {
    buffer.write('ff');
    buffer.write(hexString.replaceFirst('#', ''));
    return Color(int.parse(buffer.toString(), radix: 16));
  } else if (hexString.length == 9 || hexString.length == 10) {
    buffer.write('ff');
    buffer.write(hexString.replaceFirst('#', ''));
    return Color(int.parse(buffer.toString(), radix: 10));
  } else {
    return fallColor;
  }
}