sharp-chat/SharpChat/ChatColour.cs

80 lines
2.1 KiB
C#
Raw Normal View History

using System.Diagnostics.CodeAnalysis;
namespace SharpChat {
public readonly struct ChatColour {
public readonly byte Red;
public readonly byte Green;
public readonly byte Blue;
public readonly bool Inherits;
2023-02-07 15:13:38 +00:00
public static ChatColour None { get; } = new();
public ChatColour() {
Red = 0;
Green = 0;
Blue = 0;
Inherits = true;
2022-08-30 15:00:58 +00:00
}
2023-02-07 15:13:38 +00:00
public ChatColour(byte red, byte green, byte blue) {
Red = red;
Green = green;
Blue = blue;
Inherits = false;
2022-08-30 15:00:58 +00:00
}
public override bool Equals([NotNullWhen(true)] object obj) {
return obj is ChatColour colour && Equals(colour);
}
public bool Equals(ChatColour other) {
return Red == other.Red
&& Green == other.Green
&& Blue == other.Blue
&& Inherits == other.Inherits;
}
public override int GetHashCode() {
return ToMisuzu();
}
2023-02-07 15:13:38 +00:00
public override string ToString() {
return Inherits
? "inherit"
: string.Format("#{0:x2}{1:x2}{2:x2}", Red, Green, Blue);
2022-08-30 15:00:58 +00:00
}
2023-02-07 15:13:38 +00:00
public int ToRawRGB() {
return (Red << 16) | (Green << 8) | Blue;
2022-08-30 15:00:58 +00:00
}
2023-02-07 15:13:38 +00:00
public static ChatColour FromRawRGB(int rgb) {
return new(
(byte)((rgb >> 16) & 0xFF),
(byte)((rgb >> 8) & 0xFF),
(byte)(rgb & 0xFF)
);
2022-08-30 15:00:58 +00:00
}
2023-02-07 15:13:38 +00:00
private const int MSZ_INHERIT = 0x40000000;
public int ToMisuzu() {
return Inherits ? MSZ_INHERIT : ToRawRGB();
2022-08-30 15:00:58 +00:00
}
2023-02-07 15:13:38 +00:00
public static ChatColour FromMisuzu(int raw) {
return (raw & MSZ_INHERIT) > 0
? None
: FromRawRGB(raw);
2022-08-30 15:00:58 +00:00
}
public static bool operator ==(ChatColour left, ChatColour right) {
return left.Equals(right);
}
public static bool operator !=(ChatColour left, ChatColour right) {
return !(left == right);
}
2022-08-30 15:00:58 +00:00
}
}