ColorParser function

Color ColorParser([
  1. dynamic red,
  2. num? green,
  3. num? blue,
  4. num? alpha,
])

Factory for Color class Default values for rgba components specify in DEF_RED, DEF_GREEN, DEF_BLUE and DEF_ALPHA

allow follow color format:

  • rgba(255, 255, 255, 0.2)
  • rgb(255, 255, 255)
  • #FfF
  • #FaFaFa

return Color object

expect(ColorParser(10, null, null, null).toString(), "rgb(10, 0, 0)");

Implementation

Color ColorParser([dynamic? red, num? green, num? blue, num? alpha]) {
  var args;

  if (red is String) {
    if (red.contains(")")) {
      if (red.startsWith("rgba(")) {
        args = _parseRgba(red);
      } else if (red.startsWith("rgb(")) {
        args = _parseRgb(red);
      }
    } else if (red.startsWith("#")) {
      args = _parseHex(red.substring(1));
    }
  } else if (red is List<num?>) {
    args = _parseList(red);
  } else if (red is Map<dynamic, num>) {
    args = _parseMap(red);
  } else if (red is num || green is num || blue is num || alpha is num) {
    args = [red, green, blue, alpha];
  }

  if (args == null) {
    throw Exception(
        "Invalid color format. Allowed formats: #FFF, #FFFFFF, rgba(255, 255, 255, 1.0) and rgb(255, 255, 255)");
  }

  args = _parseArgs(args);

  return Color(args[0], args[1], args[2], args[3]);
}