sharp-chat/SharpChatCommon/Colour.cs

62 lines
1.6 KiB
C#
Raw Normal View History

2024-05-10 19:18:55 +00:00
namespace SharpChat {
public readonly struct Colour {
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 Colour None { get; } = new();
2023-02-07 15:13:38 +00:00
public Colour() {
2023-02-07 15:13:38 +00:00
Red = 0;
Green = 0;
Blue = 0;
Inherits = true;
2022-08-30 15:00:58 +00:00
}
public Colour(byte red, byte green, byte blue) {
2023-02-07 15:13:38 +00:00
Red = red;
Green = green;
Blue = blue;
Inherits = false;
2022-08-30 15:00:58 +00:00
}
public bool Equals(Colour other) {
return Inherits == other.Inherits
&& Red == other.Red
&& Green == other.Green
&& Blue == other.Blue;
}
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
}
public static Colour FromRawRGB(int rgb) {
2023-02-07 15:13:38 +00:00
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
}
public static Colour FromMisuzu(int raw) {
2023-02-07 15:13:38 +00:00
return (raw & MSZ_INHERIT) > 0
? None
: FromRawRGB(raw);
2022-08-30 15:00:58 +00:00
}
}
}