class AdjustBounds {

    public static void squareBounds(@NonNull Bounds b, int width, int height, @NonNull IntBounds padding) {
        final int activeWidth = width - padding.right - padding.left;
        final int activeHeight = height - padding.bottom - padding.top;
        final double aspectCanvas = ((double) activeWidth) / activeHeight;
        final double boundsWidth = b.x.max - b.x.min;
        final double boundsHeight = b.y.max - b.y.min;
        final double aspectBounds = boundsWidth / boundsHeight;
        if (aspectCanvas < aspectBounds) {
            // Anchor b.y.max
            final double delta = boundsWidth / aspectCanvas - boundsHeight;
            b.set(b.x.min, b.y.max, b.x.max, b.y.min - delta);
        } else {
            // Anchor b.x.min side
            final double delta = (boundsHeight * aspectCanvas - boundsWidth);
            b.set(b.x.min, b.y.max, b.x.max + delta, b.y.min);
        }
    }

}