resolveColor method

UvRgb resolveColor(
  1. UvColor? color,
  2. UvRgb fallback
)

Resolves a UV color against fallback, including basic16 and indexed256.

Implementation

UvRgb resolveColor(UvColor? color, UvRgb fallback) {
  if (color == null) return fallback;
  final index = switch (color) {
    UvRgb() => null,
    UvBasic16 c when c.index >= 0 && c.index <= 7 =>
      c.index + (c.bright ? 8 : 0),
    UvIndexed256 c when c.index >= 0 && c.index <= 255 => c.index,
    _ => throw ArgumentError('Invalid terminal palette color'),
  };
  if (index == null) return color as UvRgb;
  if (palette != null && index < palette!.length) return palette![index];
  if (index < 16) return _ansiPalette[index];
  if (index >= 232) {
    final v = 8 + (index - 232) * 10;
    return UvRgb(v, v, v);
  }
  final n = index - 16;
  int level(int v) => v == 0 ? 0 : 55 + v * 40;
  return UvRgb(level(n ~/ 36), level((n ~/ 6) % 6), level(n % 6));
}