sharp-chat/SharpChat/Colour.cs

61 lines
1.6 KiB
C#

namespace SharpChat {
public readonly struct Colour {
public readonly byte Red;
public readonly byte Green;
public readonly byte Blue;
public readonly bool Inherits;
public static Colour None { get; } = new();
public Colour() {
Red = 0;
Green = 0;
Blue = 0;
Inherits = true;
}
public Colour(byte red, byte green, byte blue) {
Red = red;
Green = green;
Blue = blue;
Inherits = false;
}
public bool Equals(Colour other) {
return Inherits == other.Inherits
&& Red == other.Red
&& Green == other.Green
&& Blue == other.Blue;
}
public override string ToString() {
return Inherits
? "inherit"
: string.Format("#{0:x2}{1:x2}{2:x2}", Red, Green, Blue);
}
public int ToRawRGB() {
return (Red << 16) | (Green << 8) | Blue;
}
public static Colour FromRawRGB(int rgb) {
return new(
(byte)((rgb >> 16) & 0xFF),
(byte)((rgb >> 8) & 0xFF),
(byte)(rgb & 0xFF)
);
}
private const int MSZ_INHERIT = 0x40000000;
public int ToMisuzu() {
return Inherits ? MSZ_INHERIT : ToRawRGB();
}
public static Colour FromMisuzu(int raw) {
return (raw & MSZ_INHERIT) > 0
? None
: FromRawRGB(raw);
}
}
}