using System;
using NvAPIWrapper.Native.Display;
using NvAPIWrapper.Native.Display.Structures;
namespace NvAPIWrapper.Display
{
///
/// Hold information about a custom display resolution
///
public class CustomResolution : IEquatable
{
///
/// Creates an instance of .
///
/// The screen width.
/// The screen height.
/// The color format.
/// The resolution timing.
/// The horizontal scaling ratio.
/// The vertical scaling ratio.
public CustomResolution(
uint width,
uint height,
ColorFormat colorFormat,
Timing timing,
float xRatio = 1,
float yRatio = 1
)
{
if (xRatio <= 0)
{
throw new ArgumentOutOfRangeException(nameof(xRatio));
}
if (yRatio <= 0)
{
throw new ArgumentOutOfRangeException(nameof(yRatio));
}
Width = width;
Height = height;
ColorFormat = colorFormat;
XRatio = xRatio;
YRatio = yRatio;
Timing = timing;
switch (ColorFormat)
{
case ColorFormat.P8:
ColorDepth = 8;
break;
case ColorFormat.R5G6B5:
ColorDepth = 16;
break;
case ColorFormat.A8R8G8B8:
ColorDepth = 24;
break;
case ColorFormat.A16B16G16R16F:
ColorDepth = 32;
break;
default:
throw new ArgumentException("Color format is invalid.", nameof(colorFormat));
}
}
///
/// Creates an instance of .
///
/// The screen width.
/// The screen height.
/// The color depth.
/// The resolution timing.
/// The horizontal scaling ratio.
/// The vertical scaling ratio.
public CustomResolution(
uint width,
uint height,
uint colorDepth,
Timing timing,
float xRatio = 1,
float yRatio = 1)
{
if (xRatio <= 0)
{
throw new ArgumentOutOfRangeException(nameof(xRatio));
}
if (yRatio <= 0)
{
throw new ArgumentOutOfRangeException(nameof(yRatio));
}
if (colorDepth != 0 && colorDepth != 8 && colorDepth != 16 && colorDepth != 24 && colorDepth != 32)
{
throw new ArgumentOutOfRangeException(nameof(colorDepth));
}
Width = width;
Height = height;
ColorDepth = colorDepth;
ColorFormat = ColorFormat.Unknown;
XRatio = xRatio;
YRatio = yRatio;
Timing = timing;
}
internal CustomResolution(CustomDisplay customDisplay)
{
Width = customDisplay.Width;
Height = customDisplay.Height;
ColorDepth = customDisplay.Depth;
ColorFormat = customDisplay.ColorFormat;
Timing = customDisplay.Timing;
XRatio = customDisplay.XRatio;
YRatio = customDisplay.YRatio;
}
///
/// Gets the source surface color depth. "0" means all 8/16/32bpp.
///
public uint ColorDepth { get; }
///
/// Gets the color format (optional)
///
public ColorFormat ColorFormat { get; }
///
/// Gets the source surface (source mode) height.
///
public uint Height { get; }
///
/// Gets the timing used to program TMDS/DAC/LVDS/HDMI/TVEncoder, etc.
///
public Timing Timing { get; }
///
/// Gets the source surface (source mode) width.
///
public uint Width { get; }
///
/// Gets the horizontal scaling ratio.
///
public float XRatio { get; }
///
/// Gets the vertical scaling ratio.
///
public float YRatio { get; }
///
public bool Equals(CustomResolution other)
{
if (ReferenceEquals(null, other))
{
return false;
}
if (ReferenceEquals(this, other))
{
return true;
}
return Width == other.Width &&
Height == other.Height &&
ColorDepth == other.ColorDepth &&
Timing.Equals(other.Timing) &&
ColorFormat == other.ColorFormat &&
XRatio.Equals(other.XRatio) &&
YRatio.Equals(other.YRatio);
}
///
/// Compares two instance of for equality.
///
/// An first instance of to compare.
/// An Second instance of to compare.
/// True if both instances are equal, otherwise false.
public static bool operator ==(CustomResolution left, CustomResolution right)
{
return Equals(left, right);
}
///
/// Compares two instance of for inequality.
///
/// An first instance of to compare.
/// An Second instance of to compare.
/// True if both instances are not equal, otherwise false.
public static bool operator !=(CustomResolution left, CustomResolution right)
{
return !Equals(left, right);
}
///
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj))
{
return false;
}
if (ReferenceEquals(this, obj))
{
return true;
}
if (obj.GetType() != GetType())
{
return false;
}
return Equals((CustomResolution) obj);
}
///
public override int GetHashCode()
{
unchecked
{
var hashCode = (int) Width;
hashCode = (hashCode * 397) ^ (int) Height;
hashCode = (hashCode * 397) ^ (int) ColorDepth;
hashCode = (hashCode * 397) ^ Timing.GetHashCode();
hashCode = (hashCode * 397) ^ (int) ColorFormat;
hashCode = (hashCode * 397) ^ XRatio.GetHashCode();
hashCode = (hashCode * 397) ^ YRatio.GetHashCode();
return hashCode;
}
}
internal CustomDisplay AsCustomDisplay(bool hardwareModeSetOnly)
{
return new CustomDisplay(Width, Height, ColorDepth, ColorFormat, XRatio, YRatio, Timing,
hardwareModeSetOnly);
}
}
}