using System; using System.Windows; using System.Windows.Controls; namespace Bro.Common.ImageCanvas { internal static class ViewportHelpers { /// /// Limits the extent of a Point to the area where X and Y are at least 0 /// /// Point to be clamped /// public static Point Clamp(this Point value) { return new Point(Math.Max(value.X, 0), Math.Max(value.Y, 0)); } /// /// Limits the extent of a Point to the area where X and Y are at least 0 and the X and /// y valuses specified, returning null if Point is outside this area /// /// Point to be clamped /// Maximum X value /// Maximum Y value /// public static Point? FilterClamp(this Point value, double xMax, double yMax) { return (value.X < 0 || value.X > xMax || value.Y < 0 || value.Y > yMax) ? (Point?)null : value; } /// /// Limits the extent of a Point to the area between two points /// /// /// Point specifiying the Top Left corner /// Point specifiying the Bottom Right corner /// The Point clamped by the Top Left and Bottom Right points public static Point Clamp(this Point value, Point topLeft, Point bottomRight) { return new Point( Math.Max(Math.Min(value.X, bottomRight.X), topLeft.X), Math.Max(Math.Min(value.Y, bottomRight.Y), topLeft.Y)); } /// /// Return a Rect that specificed by two points and clipped by a rectangle specified /// by two other points /// /// First Point specifing the rectangle to be clipped /// Second Point specifing the rectangle to be clipped /// Point specifiying the Top Left corner of the clipping rectangle /// Point specifiying the Bottom Right corner of the clipping rectangle /// Rectangle specified by two points clipped by the other two points public static Rect Clip(Point value1, Point value2, Point topLeft, Point bottomRight) { var point1 = Clamp(value1, topLeft, bottomRight); var point2 = Clamp(value2, topLeft, bottomRight); var newTopLeft = new Point(Math.Min(point1.X, point2.X), Math.Min(point1.Y, point2.Y)); var size = new Size(Math.Abs(point1.X - point2.X), Math.Abs(point1.Y - point2.Y)); return new Rect(newTopLeft, size); } /// /// Moves and sized a border on a Canvas according to a Rect /// /// Border to be moved and sized /// Rect that specifies the size and postion of the Border on the Canvas public static void PositionBorderOnCanvas(Border border, Rect rect) { Canvas.SetLeft(border, rect.Left); Canvas.SetTop(border, rect.Top); border.Width = rect.Width; border.Height = rect.Height; } public static bool IsWithinOnePercent(this double value, double testValue) { return Math.Abs(value - testValue) < .01 * testValue; } public static double FitZoom(double actualWidth, double actualHeight, double? contentWidth, double? contentHeight) { if (!contentWidth.HasValue || !contentHeight.HasValue) return 1; return Math.Min(actualWidth / contentWidth.Value, actualHeight / contentHeight.Value); } public static double FillZoom(double actualWidth, double actualHeight, double? contentWidth, double? contentHeight) { if (!contentWidth.HasValue || !contentHeight.HasValue) return 1; //return Math.Max(actualWidth / contentWidth.Value, actualHeight / contentHeight.Value); return Math.Max(contentWidth.Value / actualWidth, contentHeight.Value / actualHeight); } } }