using System;
using System.Runtime.InteropServices;
namespace Cosmos.System.Graphics
{
/*
* This struct represents a video mode in term of its number of columns, rows and color_depth
*/
///
/// Mode struct. Represents a video mode in term of its number of columns, rows and color depth.
///
public struct Mode
{
int columns;
int rows;
ColorDepth color_depth;
/* Constuctor of our struct */
///
/// Create new instance of the struct.
///
/// Number of columns.
/// Number of rows.
/// Color depth.
public Mode(int columns, int rows, ColorDepth color_depth)
{
this.columns = columns;
this.rows = rows;
this.color_depth = color_depth;
}
///
/// Check if modes equal.
///
/// Other mode.
/// bool value.
public bool Equals(Mode other)
{
if (columns == other.columns && rows == other.rows && color_depth == other.color_depth)
{
return true;
}
return false;
}
///
/// Check if modes equal.
///
/// Object to compare to.
/// bool value.
public override bool Equals(object obj) => obj is Mode mode && Equals(mode);
/* If you ovveride Equals you should ovveride GetHashCode too! */
///
/// Get hash code.
///
/// int value.
public override int GetHashCode()
{
// overflow is acceptable in this case
unchecked
{
int hash = columns.GetHashCode();
hash = (hash * 17) + rows.GetHashCode();
hash = (hash * 31) + (int)color_depth.GetHashCode();
return hash;
}
}
///
/// Compare modes.
///
/// Other mode to compare to.
/// -1 if this smaller, +1 if this bigger, 0 otherwise.
public int CompareTo(Mode other)
{
// color_depth has no effect on the orderiring
if (columns < other.columns && rows < other.rows)
{
return -1;
}
if (columns > other.columns && rows > other.rows)
{
return 1;
}
// They are effectively Equals
return 0;
}
///
/// Check if modes are equal.
///
/// lhs mode.
/// rhs mode.
/// bool value.
public static bool operator ==(Mode mode_a, Mode mode_b) => mode_a.Equals(mode_b);
///
/// Check if modes are not equal.
///
/// lhs mode.
/// rhs mode.
/// bool value.
public static bool operator !=(Mode mode_a, Mode mode_b) => !(mode_a == mode_b);
///
/// Compare modes.
///
/// lhs mode.
/// rhs mode.
/// bool value.
public static bool operator >(Mode mode_a, Mode mode_b)
{
int result;
result = mode_a.CompareTo(mode_b);
return (result > 0) ? true : false;
}
///
/// Compare modes.
///
/// lhs mode.
/// rhs mode.
/// bool value.
public static bool operator <(Mode mode_a, Mode mode_b)
{
int result;
result = mode_a.CompareTo(mode_b);
return (result < 0) ? true : false;
}
///
/// Compare modes.
///
/// lhs mode.
/// rhs mode.
/// bool value.
public static bool operator >=(Mode mode_a, Mode mode_b)
{
int result;
result = mode_a.CompareTo(mode_b);
return (result == 0 || result > 0) ? true : false;
}
///
/// Compare modes.
///
/// lhs mode.
/// rhs mode.
/// bool value.
public static bool operator <=(Mode mode_a, Mode mode_b)
{
int result;
result = mode_a.CompareTo(mode_b);
if (result == 0 || result < 0)
{
return true;
}
return false;
}
///
/// Get columns.
///
public int Columns
{
get
{
return columns;
}
}
///
/// Get rows.
///
public int Rows
{
get
{
return rows;
}
}
///
/// Get color depth
///
public ColorDepth ColorDepth
{
get
{
return color_depth;
}
}
///
/// To string.
///
/// string value.
public override string ToString()
{
return $"{columns}x{rows}@{(int)color_depth}";
}
}
}