asColor property

Color get asColor

Converts a string to a color.

  • If the string is empty, it returns Colors.transparent as the default color.
  • If the string is a number, it converts it to a Color object.
  • If the string is a valid hexadecimal color, it converts it to a Color object.
  • If the string is a named color, it converts it to a Color object.
  • If the string is a valid RGB color, it converts it to a Color object.
  • If the string is a valid HSL color, it converts it to a Color object.
  • If the string is a valid HSV color, it converts it to a Color object.
  • If the string is a valid RGBA color, it converts it to a Color object.
  • If the string is a valid HSLA color, it converts it to a Color object.
  • If the string is a valid HSVA color, it converts it to a Color object.
  • If the string is not a valid color, it computes a hash value and converts it to a Color object.

Returns the converted color.

Implementation

Color get asColor {
  try {
    if (isBlank) {
      return Colors.transparent; // Default color if the string is empty
    } else if (isNumber) {
      return Color(num.parse(this).round());
    } else if (flatEqualAny(["random", "rand", "aleatorio"])) {
      return randomWord().asColor;
    } else if (isHexadecimalColor) {
      final hexColor = replaceFirst('#', ''); // Remove any leading '#'
      final alpha = hexColor.length == 8 ? hexColor.substring(0, 2) : 'FF'; // Extract alpha value or use default 'FF'
      final color = hexColor.substring(hexColor.length - 6); // Extract RGB value
      return Color(int.parse('$alpha$color', radix: 16));
    } else if (startsWith('rgba(') && endsWith(')')) {
      var values = substring(5, length - 1).split(',');
      if (values.length == 4) {
        return Color.fromRGBO(int.parse(values[0]), int.parse(values[1]), int.parse(values[2]), double.parse(values[3]));
      } else if (values.length == 3) {
        return Color.fromRGBO(int.parse(values[0]), int.parse(values[1]), int.parse(values[2]), 1.0);
      }
    } else if (startsWith('rgb(') && endsWith(')')) {
      var values = substring(4, length - 1).split(',');
      if (values.length == 3) {
        return Color.fromRGBO(int.parse(values[0]), int.parse(values[1]), int.parse(values[2]), 1.0);
      } else if (values.length == 4) {
        return Color.fromRGBO(int.parse(values[0]), int.parse(values[1]), int.parse(values[2]), double.parse(values[3]));
      }
    } else if (startsWith('hsl(') && endsWith(')')) {
      var values = substring(4, length - 1).split(',');
      if (values.length == 3) {
        return HSLColor.fromAHSL(1.0, double.parse(values[0]), double.parse(values[1]), double.parse(values[2])).toColor();
      } else if (values.length == 4) {
        return HSLColor.fromAHSL(double.parse(values[3]), double.parse(values[0]), double.parse(values[1]), double.parse(values[2])).toColor();
      }
    } else if (startsWith('hsv(') && endsWith(')')) {
      var values = substring(4, length - 1).split(',');
      if (values.length == 3) {
        return HSVColor.fromAHSV(1.0, double.parse(values[0]), double.parse(values[1]), double.parse(values[2])).toColor();
      } else if (values.length == 4) {
        return HSVColor.fromAHSV(double.parse(values[3]), double.parse(values[0]), double.parse(values[1]), double.parse(values[2])).toColor();
      }
    } else {
      try {
        return NamedColor.fromValue(this);
      } finally {}
    }

    if (contains("*")) {
      var various = split("*").whereValid;
      if (various.isNotEmpty) {
        if (various.length == 1) return various.first.trim().asColor;
        return various.map((x) => x.trim().asColor).reduce((a, b) => a * b);
      }
    }
    if (contains("+")) {
      var various = split("+").whereValid;
      if (various.isNotEmpty) {
        if (various.length == 1) return various.first.trim().asColor;
        return various.map((x) => x.trim().asColor).reduce((a, b) => a + b);
      }
    }

    if (contains("-")) {
      var various = split("-").whereValid;
      if (various.isNotEmpty) {
        if (various.length == 1) return various.first.trim().asColor;
        return various.map((x) => x.trim().asColor).reduce((a, b) => a - b);
      }
    }
  } catch (e) {
    consoleLog(e);
  }

  // If not a valid color, compute a hash value
  var hash = 0;
  for (var i = 0; i < length; i++) {
    hash = codeUnitAt(i) + ((hash << 5) - hash);
  }

  // Add an alpha channel (0xFF) to the hash value
  return Color(hash + 0xFF000000);
}