namespace MediaBrowser.Model.Drawing
{
///
/// Class DrawingUtils
///
public static class DrawingUtils
{
///
/// Resizes a set of dimensions
///
/// Width of the current.
/// Height of the current.
/// The scale factor.
/// ImageSize.
public static ImageSize Scale(double currentWidth, double currentHeight, double scaleFactor)
{
return Scale(new ImageSize { Width = currentWidth, Height = currentHeight }, scaleFactor);
}
///
/// Resizes a set of dimensions
///
/// The size.
/// The scale factor.
/// ImageSize.
public static ImageSize Scale(ImageSize size, double scaleFactor)
{
var newWidth = size.Width * scaleFactor;
return Resize(size.Width, size.Height, newWidth);
}
///
/// Resizes a set of dimensions
///
/// Width of the current.
/// Height of the current.
/// The width.
/// The height.
/// A max fixed width, if desired
/// A max fixed height, if desired
/// ImageSize.
public static ImageSize Resize(double currentWidth, double currentHeight, double? width = null, double? height = null, double? maxWidth = null, double? maxHeight = null)
{
return Resize(new ImageSize { Width = currentWidth, Height = currentHeight }, width, height, maxWidth, maxHeight);
}
///
/// Resizes a set of dimensions
///
/// The original size object
/// A new fixed width, if desired
/// A new fixed height, if desired
/// A max fixed width, if desired
/// A max fixed height, if desired
/// A new size object
public static ImageSize Resize(ImageSize size, double? width = null, double? height = null, double? maxWidth = null, double? maxHeight = null)
{
double newWidth = size.Width;
double newHeight = size.Height;
if (width.HasValue && height.HasValue)
{
newWidth = width.Value;
newHeight = height.Value;
}
else if (height.HasValue)
{
newWidth = GetNewWidth(newHeight, newWidth, height.Value);
newHeight = height.Value;
}
else if (width.HasValue)
{
newHeight = GetNewHeight(newHeight, newWidth, width.Value);
newWidth = width.Value;
}
if (maxHeight.HasValue && maxHeight < newHeight)
{
newWidth = GetNewWidth(newHeight, newWidth, maxHeight.Value);
newHeight = maxHeight.Value;
}
if (maxWidth.HasValue && maxWidth < newWidth)
{
newHeight = GetNewHeight(newHeight, newWidth, maxWidth.Value);
newWidth = maxWidth.Value;
}
return new ImageSize { Width = newWidth, Height = newHeight };
}
///
/// Gets the new width.
///
/// Height of the current.
/// Width of the current.
/// The new height.
/// System.Double.
private static double GetNewWidth(double currentHeight, double currentWidth, double newHeight)
{
var scaleFactor = newHeight;
scaleFactor /= currentHeight;
scaleFactor *= currentWidth;
return scaleFactor;
}
///
/// Gets the new height.
///
/// Height of the current.
/// Width of the current.
/// The new width.
/// System.Double.
private static double GetNewHeight(double currentHeight, double currentWidth, double newWidth)
{
var scaleFactor = newWidth;
scaleFactor /= currentWidth;
scaleFactor *= currentHeight;
return scaleFactor;
}
}
///
/// Struct ImageSize
///
public struct ImageSize
{
///
/// Gets or sets the height.
///
/// The height.
public double Height { get; set; }
///
/// Gets or sets the width.
///
/// The width.
public double Width { get; set; }
}
}