fillRect method

void fillRect(
  1. Rect rect, {
  2. String? char,
  3. int? fg,
  4. int? bg,
  5. int? modifiers,
})

Fills a rectangular region with the given character and attributes.

Implementation

void fillRect(Rect rect, {String? char, int? fg, int? bg, int? modifiers}) {
  final clipped = _clipStack.isEmpty ? rect : activeClip.intersect(rect);
  if (clipped.width <= 0 || clipped.height <= 0) return;

  final startX = max(0, clipped.left);
  final startY = max(0, clipped.top);
  final endX = min(width, clipped.right);
  final endY = min(height, clipped.bottom);
  if (startX >= endX || startY >= endY) return;

  if (char != null) {
    for (var y = startY; y < endY; y++) {
      final rowStart = y * width + startX;
      final rowEnd = y * width + endX;
      characters.fillRange(rowStart, rowEnd, char);
    }
  }

  if (fg != null || bg != null || modifiers != null) {
    for (var y = startY; y < endY; y++) {
      final rowOffset = y * width;
      for (var x = startX; x < endX; x++) {
        final idx = rowOffset + x;
        final attrIdx = idx * 3;
        if (fg != null) {
          attributes[attrIdx + 0] = blendColor(fg, attributes[attrIdx + 0]);
        }
        if (bg != null) {
          attributes[attrIdx + 1] = blendColor(bg, attributes[attrIdx + 1]);
        }
        if (modifiers != null) attributes[attrIdx + 2] = modifiers;
      }
    }
  }
}