draw method

  1. @override
void draw(
  1. Screen screen,
  2. Rectangle area
)
override

Draws this drawable into screen within area.

Implementation

@override
void draw(Screen screen, Rectangle area) {
  final cols = columns ?? area.width;
  final rws = rows ?? area.height;

  if (cols <= 0 || rws <= 0) return;

  // Resize image to match the target columns and 2x rows (since each cell is 2 pixels high).
  final resized = img.copyResize(
    image,
    width: cols,
    height: rws * 2,
    interpolation: img.Interpolation.average,
  );

  for (var y = 0; y < rws; y++) {
    for (var x = 0; x < cols; x++) {
      final topPixel = resized.getPixel(x, y * 2);
      final bottomPixel = resized.getPixel(x, y * 2 + 1);

      final topColor = UvRgb(
        topPixel.r.toInt(),
        topPixel.g.toInt(),
        topPixel.b.toInt(),
      );
      final bottomColor = UvRgb(
        bottomPixel.r.toInt(),
        bottomPixel.g.toInt(),
        bottomPixel.b.toInt(),
      );

      final cell = Cell(
        content: '▀',
        style: UvStyle(fg: topColor, bg: bottomColor),
      );

      if (area.minX + x < area.maxX && area.minY + y < area.maxY) {
        screen.setCell(area.minX + x, area.minY + y, cell);
      }
    }
  }
}