using System;
using System.Globalization;
using Avalonia.Utilities;
namespace Avalonia
{
///
/// Represents a size in device pixels.
///
public readonly struct PixelSize : IEquatable
{
///
/// A size representing zero
///
public static readonly PixelSize Empty = new PixelSize(0, 0);
///
/// Initializes a new instance of the structure.
///
/// The width.
/// The height.
public PixelSize(int width, int height)
{
Width = width;
Height = height;
}
///
/// Gets the aspect ratio of the size.
///
public double AspectRatio => (double)Width / Height;
///
/// Gets the width.
///
public int Width { get; }
///
/// Gets the height.
///
public int Height { get; }
///
/// Checks for equality between two s.
///
/// The first size.
/// The second size.
/// True if the sizes are equal; otherwise false.
public static bool operator ==(PixelSize left, PixelSize right)
{
return left.Equals(right);
}
///
/// Checks for inequality between two s.
///
/// The first size.
/// The second size.
/// True if the sizes are unequal; otherwise false.
public static bool operator !=(PixelSize left, PixelSize right)
{
return !(left == right);
}
///
/// Parses a string.
///
/// The string.
/// The .
public static PixelSize Parse(string s)
{
using (var tokenizer = new StringTokenizer(s, CultureInfo.InvariantCulture, exceptionMessage: "Invalid PixelSize."))
{
return new PixelSize(
tokenizer.ReadInt32(),
tokenizer.ReadInt32());
}
}
///
/// Returns a boolean indicating whether the size is equal to the other given size.
///
/// The other size to test equality against.
/// True if this size is equal to other; False otherwise.
public bool Equals(PixelSize other)
{
return Width == other.Width && Height == other.Height;
}
///
/// Checks for equality between a size and an object.
///
/// The object.
///
/// True if is a size that equals the current size.
///
public override bool Equals(object? obj) => obj is PixelSize other && Equals(other);
///
/// Returns a hash code for a .
///
/// The hash code.
public override int GetHashCode()
{
unchecked
{
int hash = 17;
hash = (hash * 23) + Width.GetHashCode();
hash = (hash * 23) + Height.GetHashCode();
return hash;
}
}
///
/// Returns a new with the same height and the specified width.
///
/// The width.
/// The new .
public PixelSize WithWidth(int width) => new PixelSize(width, Height);
///
/// Returns a new with the same width and the specified height.
///
/// The height.
/// The new .
public PixelSize WithHeight(int height) => new PixelSize(Width, height);
///
/// Converts the to a device-independent using the
/// specified scaling factor.
///
/// The scaling factor.
/// The device-independent size.
public Size ToSize(double scale) => new Size(Width / scale, Height / scale);
///
/// Converts the to a device-independent using the
/// specified scaling factor.
///
/// The scaling factor.
/// The device-independent size.
public Size ToSize(Vector scale) => new Size(Width / scale.X, Height / scale.Y);
///
/// Converts the to a device-independent using the
/// specified dots per inch (DPI).
///
/// The dots per inch.
/// The device-independent size.
public Size ToSizeWithDpi(double dpi) => ToSize(dpi / 96);
///
/// Converts the to a device-independent using the
/// specified dots per inch (DPI).
///
/// The dots per inch.
/// The device-independent size.
public Size ToSizeWithDpi(Vector dpi) => ToSize(new Vector(dpi.X / 96, dpi.Y / 96));
///
/// Converts a to device pixels using the specified scaling factor.
///
/// The size.
/// The scaling factor.
/// The device-independent size.
public static PixelSize FromSize(Size size, double scale) => new PixelSize(
(int)Math.Ceiling(size.Width * scale),
(int)Math.Ceiling(size.Height * scale));
///
/// Converts a to device pixels using the specified scaling factor.
///
/// The size.
/// The scaling factor.
/// The device-independent size.
public static PixelSize FromSize(Size size, Vector scale) => new PixelSize(
(int)Math.Ceiling(size.Width * scale.X),
(int)Math.Ceiling(size.Height * scale.Y));
///
/// Converts a to device pixels using the specified dots per inch (DPI).
///
/// The size.
/// The dots per inch.
/// The device-independent size.
public static PixelSize FromSizeWithDpi(Size size, double dpi) => FromSize(size, dpi / 96);
///
/// Converts a to device pixels using the specified dots per inch (DPI).
///
/// The size.
/// The dots per inch.
/// The device-independent size.
public static PixelSize FromSizeWithDpi(Size size, Vector dpi) => FromSize(size, new Vector(dpi.X / 96, dpi.Y / 96));
///
/// Returns the string representation of the size.
///
/// The string representation of the size.
public override string ToString()
{
return string.Format(CultureInfo.InvariantCulture, "{0}, {1}", Width, Height);
}
}
}