Resolution
Represents a resolution value.
using System;
namespace AngleSharp.Css.Values
{
public struct Resolution : IEquatable<Resolution>, IComparable<Resolution>, IFormattable
{
public enum Unit
{
Dpi,
Dpcm,
Dppx
}
private readonly float _value;
private readonly Unit _unit;
public float Value => _value;
public Unit Type => _unit;
public string UnitString {
get {
switch (_unit) {
case Unit.Dpcm:
return Units.Dpcm;
case Unit.Dpi:
return Units.Dpi;
case Unit.Dppx:
return Units.Dppx;
default:
return string.Empty;
}
}
}
public Resolution(float value, Unit unit)
{
_value = value;
_unit = unit;
}
public float ToDotsPerPixel()
{
if (_unit == Unit.Dpi)
return _value / 96;
if (_unit == Unit.Dpcm)
return _value * 127 / 4800;
return _value;
}
public float To(Unit unit)
{
float num = ToDotsPerPixel();
switch (unit) {
case Unit.Dpi:
return num * 96;
case Unit.Dpcm:
return num * 50 * 96 / 127;
default:
return num;
}
}
public bool Equals(Resolution other)
{
if (_value == other._value)
return _unit == other._unit;
return false;
}
public int CompareTo(Resolution other)
{
return ToDotsPerPixel().CompareTo(other.ToDotsPerPixel());
}
public override bool Equals(object obj)
{
if (obj is Resolution)
return Equals((Resolution)obj);
return false;
}
public override int GetHashCode()
{
return _value.GetHashCode();
}
public override string ToString()
{
return _value.ToString() + UnitString;
}
public string ToString(string format, IFormatProvider formatProvider)
{
return _value.ToString(format, formatProvider) + UnitString;
}
}
}