fromHexString static method

void fromHexString(
  1. String value,
  2. Vector4 result
)

Convert the color as a string in the format '#FF0F00', '#FFFF0F00', '#FF0' or '#FFF0' (with or without a leading '#', case insensitive) to the corresponding color value and store it in result. The first group is treated as the alpha channel if a value with four groups is passed.

Implementation

static void fromHexString(String value, Vector4 result) {
  final fullMatch = _hexStringFullRegex.matchAsPrefix(value);

  if (fullMatch != null) {
    if (fullMatch[4] == null) {
      final r = int.parse(fullMatch[1]!, radix: 16);
      final g = int.parse(fullMatch[2]!, radix: 16);
      final b = int.parse(fullMatch[3]!, radix: 16);

      fromRgba(r, g, b, 255, result);
      return;
    } else {
      final a = int.parse(fullMatch[1]!, radix: 16);
      final r = int.parse(fullMatch[2]!, radix: 16);
      final g = int.parse(fullMatch[3]!, radix: 16);
      final b = int.parse(fullMatch[4]!, radix: 16);

      fromRgba(r, g, b, a, result);
      return;
    }
  }

  final smallMatch = _hexStringSmallRegex.matchAsPrefix(value);

  if (smallMatch != null) {
    if (smallMatch[4] == null) {
      final r = int.parse(smallMatch[1]! + smallMatch[1]!, radix: 16);
      final g = int.parse(smallMatch[2]! + smallMatch[2]!, radix: 16);
      final b = int.parse(smallMatch[3]! + smallMatch[3]!, radix: 16);

      fromRgba(r, g, b, 255, result);
      return;
    } else {
      final a = int.parse(smallMatch[1]! + smallMatch[1]!, radix: 16);
      final r = int.parse(smallMatch[2]! + smallMatch[2]!, radix: 16);
      final g = int.parse(smallMatch[3]! + smallMatch[3]!, radix: 16);
      final b = int.parse(smallMatch[4]! + smallMatch[4]!, radix: 16);

      fromRgba(r, g, b, a, result);
      return;
    }
  }

  throw FormatException('Could not parse hex color $value');
}