// -----------------------------------------------------------------------
//
// Copyright 2014 MIT Licence. See licence.md for more information.
//
// -----------------------------------------------------------------------
namespace Perspex
{
using System;
using System.Globalization;
///
/// Defines a size.
///
public struct Size
{
///
/// A size representing infinity.
///
public static readonly Size Infinity = new Size(double.PositiveInfinity, double.PositiveInfinity);
///
/// The width.
///
private double width;
///
/// The height.
///
private double height;
///
/// Initializes a new instance of the structure.
///
/// The width.
/// The height.
public Size(double width, double height)
{
this.width = width;
this.height = height;
}
///
/// Gets the width.
///
public double Width
{
get { return this.width; }
}
///
/// Gets the height.
///
public double Height
{
get { return this.height; }
}
///
/// Checks for equality between two s.
///
/// The first size.
/// The second size.
/// True if the sizes are equal; otherwise false.
public static bool operator ==(Size left, Size right)
{
return left.width == right.width && left.height == right.height;
}
///
/// Checks for unequality between two s.
///
/// The first size.
/// The second size.
/// True if the sizes are unequal; otherwise false.
public static bool operator !=(Size left, Size right)
{
return !(left == right);
}
///
/// Scales a size.
///
/// The size
/// The scaling factor.
/// The scaled size.
public static Size operator *(Size size, Vector scale)
{
return new Size(size.width * scale.X, size.height * scale.Y);
}
///
/// Scales a size.
///
/// The size
/// The scaling factor.
/// The scaled size.
public static Size operator /(Size size, Vector scale)
{
return new Size(size.width / scale.X, size.height / scale.Y);
}
///
/// Constrains the size.
///
/// The size to constrain to.
/// The constrained size.
public Size Constrain(Size constraint)
{
return new Size(
Math.Min(this.width, constraint.width),
Math.Min(this.height, constraint.height));
}
///
/// Deflates the size by a .
///
/// The thickness.
/// The deflated size.
/// The deflated size cannot be less than 0.
public Size Deflate(Thickness thickness)
{
return new Size(
Math.Max(0, this.width - thickness.Left - thickness.Right),
Math.Max(0, this.height - thickness.Top - thickness.Bottom));
}
public override bool Equals(object obj)
{
if (obj is Size)
{
var other = (Size)obj;
return this.Width == other.Width && this.Height == other.Height;
}
return false;
}
public override int GetHashCode()
{
unchecked
{
int hash = 17;
hash = (hash * 23) + this.Width.GetHashCode();
hash = (hash * 23) + this.Height.GetHashCode();
return hash;
}
}
///
/// Inflates the size by a .
///
/// The thickness.
/// The inflated size.
public Size Inflate(Thickness thickness)
{
return new Size(
this.width + thickness.Left + thickness.Right,
this.height + thickness.Top + thickness.Bottom);
}
///
/// Returns a new with the same height and the specified width.
///
/// The width.
/// The new .
public Size WithWidth(double width)
{
return new Size(width, this.height);
}
///
/// Returns a new with the same width and the specified height.
///
/// The height.
/// The new .
public Size WithHeight(double height)
{
return new Size(this.width, height);
}
///
/// Returns the string representation of the size.
///
/// The string representation of the size
public override string ToString()
{
return string.Format(CultureInfo.InvariantCulture, "{0}, {1}", this.width, this.height);
}
}
}