using System; using System.Collections.Generic; namespace CC_Functions.Commandline.TUI { /// /// Represents a pixel /// public class Pixel { /// /// This pixels background color /// public ConsoleColor BackColor; /// /// This pixels content character /// public char Content; /// /// This pixels foregound color /// public ConsoleColor ForeColor; /// /// Generates a new pixel /// /// The background color /// The foreground color /// The new content public Pixel(ConsoleColor backColor, ConsoleColor foreColor, char content) { BackColor = backColor; ForeColor = foreColor; Content = content; } /// /// Generates a new pixel /// /// The content for this pixel public Pixel(char content) : this(Console.BackgroundColor, Console.ForegroundColor, content) { } /// /// Generates a new pixel /// public Pixel() : this(' ') { } /// /// Use this in functions that require equality comparers /// public static IEqualityComparer ColorContentComparer { get; } = new ColorContentEqualityComparer(); /// /// Whether this is equal to another pixel /// /// The other pixel to compare /// Whether they are equal protected bool Equals(Pixel other) => ColorContentComparer.Equals(this, other); /// /// Whether this is equal to another object /// /// The other object to compare /// 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((Pixel) obj); } /// /// Generates an integer for comparing this object /// /// The generated hash public override int GetHashCode() => HashCode.Combine((int) BackColor, (int) ForeColor, Content); /// /// Whether two pixels are equal /// /// First pixel to compare /// Second pixel to compare /// Whether they are equal public static bool operator ==(Pixel a, Pixel b) => a.Equals(b); /// /// Whether to pixels are not equal /// /// First pixel to compare /// Second pixel to compare /// Whether they are not equal public static bool operator !=(Pixel a, Pixel b) => !a.Equals(b); /// /// Returns the content of this pixel /// /// The content of this pixel public override string ToString() => Content.ToString(); private sealed class ColorContentEqualityComparer : IEqualityComparer { public bool Equals(Pixel x, Pixel y) { if (ReferenceEquals(x, y)) return true; if (ReferenceEquals(x, null)) return false; if (ReferenceEquals(y, null)) return false; if (x.GetType() != y.GetType()) return false; return x.GetHashCode().Equals(y.GetHashCode()); } public int GetHashCode(Pixel obj) => obj.GetHashCode(); } } }