getPlaceholderData function

Future<PlaceholderData?> getPlaceholderData({
  1. required String url,
  2. required Size viewSize,
})

Implementation

Future<twic_types.PlaceholderData?> getPlaceholderData({
  required String url,
  required twic_types.Size viewSize,
}) async {
  final inspectData = parseInspect(await getAsString("$url/inspect"));
  if (inspectData == null) {
    return null;
  }

  final intrinsicRatio = inspectData.width / inspectData.height;
  final viewRatio = viewSize.width / viewSize.height!;

  // determines the appropriate width for displaying the LQIP
  double actualWidth;
  if (intrinsicRatio > viewRatio) {
    // if the intrinsic ratio is greater than the view ratio, use the view's width
    actualWidth = viewSize.width;
  } else {
    // otherwise, calculate the width based on the view's height and intrinsic ratio
    actualWidth = viewSize.height! * intrinsicRatio;
  }

  // ensure the width is at least 1 pixel wide and round it to a double
  actualWidth = max(1, actualWidth).roundToDouble();

  return twic_types.PlaceholderData(
    image: inspectData.image,
    color: inspectData.color,
    deviation: sqrt((inspectData.height -
                inspectData.padding.top -
                inspectData.padding.bottom) *
            (inspectData.width -
                inspectData.padding.left -
                inspectData.padding.right) /
            (inspectData.intrinsicHeight * inspectData.intrinsicWidth)) *
        0.5,
    height: (actualWidth / intrinsicRatio).roundToDouble(),
    padding: computePadding(inspectData: inspectData, viewSize: viewSize),
    width: actualWidth,
  );
}