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 the image to fit the target cell dimensions before encoding.
  // Each cell is cellPixelWidth × cellPixelHeight pixels, so the Sixel
  // output needs to be cols*cellPixelWidth wide and rws*cellPixelHeight tall.
  final targetWidth = cols * cellPixelWidth;
  // Round target height up to the next multiple of 6 for clean Sixel bands.
  final rawHeight = rws * cellPixelHeight;
  final targetHeight = ((rawHeight + 5) ~/ 6) * 6;

  final resized = img.copyResize(
    image,
    width: targetWidth,
    height: targetHeight,
    interpolation: img.Interpolation.average,
  );

  final sequence = SixelImage.encode(resized, maxColors: maxColors);

  for (var y = area.minY; y < area.minY + rws && y < area.maxY; y++) {
    for (var x = area.minX; x < area.minX + cols && x < area.maxX; x++) {
      if (x == area.minX && y == area.minY) {
        screen.setCell(x, y, Cell(content: sequence, width: 1));
      } else {
        screen.setCell(x, y, Cell(content: '', width: 0));
      }
    }
  }
}