fillRect function

Image fillRect (
  1. Image src,
  2. int x1,
  3. int y1,
  4. int x2,
  5. int y2,
  6. int color
)

Fill a rectangle in the image src with the given color with the corners x1,y1 and x2,y2.

Implementation

Image fillRect(Image src, int x1, int y1, int x2, int y2, int color) {
  int _x0 = clamp(min(x1, x2), 0, src.width - 1);
  int _y0 = clamp(min(y1, y2), 0, src.height - 1);
  int _x1 = clamp(max(x1, x2), 0, src.width - 1);
  int _y1 = clamp(max(y1, y2), 0, src.height - 1);

  // If no blending is necessary, use a faster fill method.
  if (getAlpha(color) == 255) {
    int w = src.width;
    int start = _y0 * w + _x0;
    int end = start + (_x1 - _x0) + 1;
    for (int sy = _y0; sy <= _y1; ++sy) {
      src.data.fillRange(start, end, color);
      start += w;
      end += w;
    }
  } else {
    for (int sy = _y0; sy <= _y1; ++sy) {
      int pi = sy * src.width + _x0;
      for (int sx = _x0; sx <= _x1; ++sx, ++pi) {
        src[pi] = alphaBlendColors(src[pi], color);
      }
    }
  }

  return src;
}