From 8eda7408d78bf88e1e704d7ab57b2b44196de28e Mon Sep 17 00:00:00 2001 From: Martin Evans Date: Mon, 24 Mar 2025 01:07:44 +0000 Subject: [PATCH] - Converted all colour type to structs - Made all colour structs readonly --- ColorHelper/Color/CMYK.cs | 22 +++++----------------- ColorHelper/Color/HEX.cs | 19 ++++--------------- ColorHelper/Color/HSL.cs | 19 ++++--------------- ColorHelper/Color/HSV.cs | 19 ++++--------------- ColorHelper/Color/RGB.cs | 19 ++++--------------- ColorHelper/Color/XYZ.cs | 19 ++++--------------- ColorHelper/Color/YIQ.cs | 19 ++++--------------- ColorHelper/Color/YUV.cs | 19 ++++--------------- ColorHelper/ColorHelper.csproj | 1 + 9 files changed, 34 insertions(+), 122 deletions(-) diff --git a/ColorHelper/Color/CMYK.cs b/ColorHelper/Color/CMYK.cs index 6273e7e..9218fa2 100644 --- a/ColorHelper/Color/CMYK.cs +++ b/ColorHelper/Color/CMYK.cs @@ -1,11 +1,11 @@ namespace ColorHelper { - public class CMYK : IColor + public readonly record struct CMYK : IColor { - public byte C { get; set; } - public byte M { get; set; } - public byte Y { get; set; } - public byte K { get; set; } + public byte C { get; } + public byte M { get; } + public byte Y { get; } + public byte K { get; } public CMYK(byte c, byte m, byte y, byte k) { @@ -15,18 +15,6 @@ public CMYK(byte c, byte m, byte y, byte k) this.K = k; } - public override bool Equals(object obj) - { - var result = (CMYK)obj; - - return ( - result != null && - this.C == result.C && - this.M == result.M && - this.Y == result.Y && - this.K == result.K); - } - public override string ToString() { return $"{this.C}% {this.M}% {this.Y}% {this.K}%"; diff --git a/ColorHelper/Color/HEX.cs b/ColorHelper/Color/HEX.cs index fc57f1d..d411c5e 100644 --- a/ColorHelper/Color/HEX.cs +++ b/ColorHelper/Color/HEX.cs @@ -1,28 +1,17 @@ namespace ColorHelper { - public class HEX : IColor + public readonly record struct HEX : IColor { - private string _value; - - public string Value - { - get { return _value; } - set { _value = (value.IndexOf('#') == 0) ? value.Substring(1) : value; } - } + public string Value { get; } public HEX(string value) { - this.Value = value; - } - - public override bool Equals(object obj) - { - return this.Value == (obj as HEX)?.Value; + Value = value.IndexOf('#') == 0 ? value.Substring(1) : value; } public override string ToString() { - return $"{this.Value}"; + return Value; } } } \ No newline at end of file diff --git a/ColorHelper/Color/HSL.cs b/ColorHelper/Color/HSL.cs index ae9bdaa..6c59181 100644 --- a/ColorHelper/Color/HSL.cs +++ b/ColorHelper/Color/HSL.cs @@ -1,10 +1,10 @@ namespace ColorHelper { - public class HSL : IColor + public readonly record struct HSL : IColor { - public int H { get; set; } - public byte S { get; set; } - public byte L { get; set; } + public int H { get; } + public byte S { get; } + public byte L { get; } public HSL(int h, byte s, byte l) { @@ -13,17 +13,6 @@ public HSL(int h, byte s, byte l) this.L = l; } - public override bool Equals(object obj) - { - var result = (HSL)obj; - - return ( - result != null && - this.H == result.H && - this.S == result.S && - this.L == result.L); - } - public override string ToString() { return $"{this.H}° {this.S}% {this.L}%"; diff --git a/ColorHelper/Color/HSV.cs b/ColorHelper/Color/HSV.cs index 41f864b..da1603a 100644 --- a/ColorHelper/Color/HSV.cs +++ b/ColorHelper/Color/HSV.cs @@ -1,10 +1,10 @@ namespace ColorHelper { - public class HSV : IColor + public readonly record struct HSV : IColor { - public int H { get; set; } - public byte S { get; set; } - public byte V { get; set; } + public int H { get; } + public byte S { get; } + public byte V { get; } public HSV(int h, byte s, byte v) { @@ -13,17 +13,6 @@ public HSV(int h, byte s, byte v) this.V = v; } - public override bool Equals(object obj) - { - var result = (HSV)obj; - - return ( - result != null && - this.H == result.H && - this.S == result.S && - this.V == result.V); - } - public override string ToString() { return $"{this.H}° {this.S}% {this.V}%"; diff --git a/ColorHelper/Color/RGB.cs b/ColorHelper/Color/RGB.cs index 5b7c185..e9e7e72 100644 --- a/ColorHelper/Color/RGB.cs +++ b/ColorHelper/Color/RGB.cs @@ -1,10 +1,10 @@ namespace ColorHelper { - public class RGB : IColor + public readonly record struct RGB : IColor { - public byte R { get; set; } - public byte G { get; set; } - public byte B { get; set; } + public byte R { get; } + public byte G { get; } + public byte B { get; } public RGB(byte r, byte g, byte b) { @@ -13,17 +13,6 @@ public RGB(byte r, byte g, byte b) this.B = b; } - public override bool Equals(object obj) - { - var result = (RGB) obj; - - return ( - result != null && - this.R == result.R && - this.G == result.G && - this.B == result.B); - } - public override string ToString() { return $"rgb({this.R}, {this.G}, {this.B})"; diff --git a/ColorHelper/Color/XYZ.cs b/ColorHelper/Color/XYZ.cs index cb9f496..5ff1107 100644 --- a/ColorHelper/Color/XYZ.cs +++ b/ColorHelper/Color/XYZ.cs @@ -1,10 +1,10 @@ namespace ColorHelper { - public class XYZ : IColor + public readonly record struct XYZ : IColor { - public double X { get; set; } - public double Y { get; set; } - public double Z { get; set; } + public double X { get; } + public double Y { get; } + public double Z { get; } public XYZ(double x, double y, double z) { @@ -13,17 +13,6 @@ public XYZ(double x, double y, double z) this.Z = z; } - public override bool Equals(object obj) - { - var result = (XYZ)obj; - - return ( - result != null && - this.X == result.X && - this.Y == result.Y && - this.Z == result.Z); - } - public override string ToString() { return $"{this.X} {this.Y} {this.Z}"; diff --git a/ColorHelper/Color/YIQ.cs b/ColorHelper/Color/YIQ.cs index 348ffdd..49f6555 100644 --- a/ColorHelper/Color/YIQ.cs +++ b/ColorHelper/Color/YIQ.cs @@ -1,10 +1,10 @@ namespace ColorHelper { - public class YIQ : IColor + public readonly record struct YIQ : IColor { - public double Y { get; set; } - public double I { get; set; } - public double Q { get; set; } + public double Y { get; } + public double I { get; } + public double Q { get; } public YIQ(double y, double i, double q) { @@ -13,17 +13,6 @@ public YIQ(double y, double i, double q) this.Q = q; } - public override bool Equals(object obj) - { - var result = (YIQ)obj; - - return ( - result != null && - this.Y == result.Y && - this.I == result.I && - this.Q == result.Q); - } - public override string ToString() { return $"{this.Y} {this.I} {this.Q}"; diff --git a/ColorHelper/Color/YUV.cs b/ColorHelper/Color/YUV.cs index 5b15d29..7d04bb7 100644 --- a/ColorHelper/Color/YUV.cs +++ b/ColorHelper/Color/YUV.cs @@ -1,10 +1,10 @@ namespace ColorHelper { - public class YUV : IColor + public readonly record struct YUV : IColor { - public double Y { get; set; } - public double U { get; set; } - public double V { get; set; } + public double Y { get; } + public double U { get; } + public double V { get; } public YUV(double y, double u, double v) { @@ -13,17 +13,6 @@ public YUV(double y, double u, double v) this.V = v; } - public override bool Equals(object obj) - { - var result = (YUV)obj; - - return ( - result != null && - this.Y == result.Y && - this.U == result.U && - this.V == result.V); - } - public override string ToString() { return $"{this.Y} {this.U} {this.V}"; diff --git a/ColorHelper/ColorHelper.csproj b/ColorHelper/ColorHelper.csproj index 3568d8b..6f0d08d 100644 --- a/ColorHelper/ColorHelper.csproj +++ b/ColorHelper/ColorHelper.csproj @@ -2,6 +2,7 @@ netstandard2.0 + latest Artyom Gritsuk https://github.com/iamartyom/ColorHelper https://github.com/iamartyom/ColorHelper