screenshotHighlightPercentRect function

Rect screenshotHighlightPercentRect({
  1. required Rect rectInImagePixels,
  2. required Size imageSize,
  3. DeviceInfo? frameDevice,
})

Converts a rect in capture-image pixels into HTML overlay percentages.

When frameDevice is set, percentages are relative to the framed output produced by the device-frame encoder. Otherwise they are relative to the raw capture.

Implementation

Rect screenshotHighlightPercentRect({
  required Rect rectInImagePixels,
  required Size imageSize,
  DeviceInfo? frameDevice,
}) {
  if (frameDevice == null) {
    return Rect.fromLTRB(
      _percent(rectInImagePixels.left, imageSize.width),
      _percent(rectInImagePixels.top, imageSize.height),
      _percent(rectInImagePixels.right, imageSize.width),
      _percent(rectInImagePixels.bottom, imageSize.height),
    );
  }

  final padding = frameDevice.frameSize.shortestSide * 0.025;
  final outputWidth = frameDevice.frameSize.width + padding * 2;
  final outputHeight = frameDevice.frameSize.height + padding * 2;
  final screenRect = frameDevice.screenPath.getBounds().shift(
        Offset(padding, padding),
      );
  final dest = screenshotFittedScreenRect(
    imageSize: imageSize,
    screenRect: screenRect,
  );
  final left =
      dest.left + (rectInImagePixels.left / imageSize.width) * dest.width;
  final top =
      dest.top + (rectInImagePixels.top / imageSize.height) * dest.height;
  final right =
      dest.left + (rectInImagePixels.right / imageSize.width) * dest.width;
  final bottom =
      dest.top + (rectInImagePixels.bottom / imageSize.height) * dest.height;
  return Rect.fromLTRB(
    _percent(left, outputWidth),
    _percent(top, outputHeight),
    _percent(right, outputWidth),
    _percent(bottom, outputHeight),
  );
}