toUvColor method

UvColor toUvColor()

Rebuilds this metadata as a validated UV color.

Implementation

UvColor toUvColor() {
  switch (kind) {
    case 'basic16':
      final value = index;
      if (value == null || value < 0 || value > 7) {
        throw const FormatException('invalid basic16 color');
      }
      return UvColor.basic16(value, bright: bright ?? false);
    case 'indexed256':
      final value = index;
      if (value == null || value < 0 || value > 255) {
        throw const FormatException('invalid indexed256 color');
      }
      return UvColor.indexed256(value);
    case 'rgb':
      final red = r;
      final green = g;
      final blue = b;
      final alpha = a ?? 255;
      if (red == null ||
          green == null ||
          blue == null ||
          red < 0 ||
          red > 255 ||
          green < 0 ||
          green > 255 ||
          blue < 0 ||
          blue > 255 ||
          alpha < 0 ||
          alpha > 255) {
        throw const FormatException('invalid RGB color');
      }
      return UvColor.rgb(red, green, blue, a: alpha);
    default:
      throw FormatException('unsupported native color kind: $kind');
  }
}