blend method

void blend(
  1. PImage other, {
  2. required Rect sourceRect,
  3. required Rect destRect,
  4. required SketchBlendMode mode,
})

Blends pixels from the other image into this PImage, using the given blend mode.

The sourceRect is the rectangle within the other image that's copied and blended into this PImage. The destRect is the rectangle within this PImage where the blend is painted. sourceRect and destRect must have the same dimensions.

Implementation

void blend(
  PImage other, {
  required Rect sourceRect,
  required Rect destRect,
  required SketchBlendMode mode,
}) {
  if (sourceRect.width != destRect.width) {
    throw Exception(
        "PImage#blend() source and destination rects must have same width. Actually: ${sourceRect.width} vs ${destRect.width}");
  }
  if (sourceRect.height != destRect.height) {
    throw Exception(
        "PImage#blend() source and destination rects must have same height. Actually: ${sourceRect.height} vs ${destRect.height}");
  }

  final sourceOrigin = sourceRect.topLeft;
  final destOrigin = destRect.topLeft;
  for (int col = 0; col < sourceRect.width; col += 1) {
    for (int row = 0; row < sourceRect.height; row += 1) {
      final pixelBlendColor = blendColor(
        get(col + sourceOrigin.dx.floor(), row + sourceOrigin.dy.floor()),
        other.get(col + destOrigin.dx.floor(), row + destOrigin.dy.floor()),
        mode,
      );

      set(
        col + sourceOrigin.dx.floor(),
        row + sourceOrigin.dy.floor(),
        pixelBlendColor,
      );
    }
  }

  _isFlutterImageDirty = true;
}