fromHexString method

void fromHexString (String value, 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 Match fullMatch = _hexStringFullRegex.matchAsPrefix(value);

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

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

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

  final Match smallMatch = _hexStringSmallRegex.matchAsPrefix(value);

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

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

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

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