Convert Colour class to a struct.

This commit is contained in:
flash 2023-02-07 16:13:38 +01:00
parent 4104e40843
commit 5e3eecda8c
3 changed files with 46 additions and 47 deletions

View file

@ -1,55 +1,54 @@
namespace SharpChat {
public class ChatColour {
public const int INHERIT = 0x40000000;
public struct ChatColour {
public byte Red { get; }
public byte Green { get; }
public byte Blue { get; }
public bool Inherits { get; }
public int Raw { get; set; }
public static ChatColour None { get; } = new();
public ChatColour(bool inherit = true) {
Inherit = inherit;
public ChatColour() {
Red = 0;
Green = 0;
Blue = 0;
Inherits = true;
}
public ChatColour(int colour) {
Raw = colour;
}
public bool Inherit {
get => (Raw & INHERIT) > 0;
set {
if(value)
Raw |= INHERIT;
else
Raw &= ~INHERIT;
}
}
public int Red {
get => (Raw >> 16) & 0xFF;
set {
Raw &= ~0xFF0000;
Raw |= (value & 0xFF) << 16;
}
}
public int Green {
get => (Raw >> 8) & 0xFF;
set {
Raw &= ~0xFF00;
Raw |= (value & 0xFF) << 8;
}
}
public int Blue {
get => Raw & 0xFF;
set {
Raw &= ~0xFF;
Raw |= value & 0xFF;
}
public ChatColour(byte red, byte green, byte blue) {
Red = red;
Green = green;
Blue = blue;
Inherits = false;
}
public override string ToString() {
if(Inherit)
return @"inherit";
return string.Format(@"#{0:X6}", Raw);
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 ChatColour 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 ChatColour FromMisuzu(int raw) {
return (raw & MSZ_INHERIT) > 0
? None
: FromRawRGB(raw);
}
}
}

View file

@ -131,7 +131,7 @@ namespace SharpChat {
if(Status == ChatUserStatus.Offline)
Status = ChatUserStatus.Online;
Colour = new(auth.ColourRaw);
Colour = ChatColour.FromMisuzu(auth.ColourRaw);
Rank = auth.Rank;
Permissions = auth.Permissions;

View file

@ -137,7 +137,7 @@ namespace SharpChat {
new MySqlParameter(@"data", JsonSerializer.SerializeToUtf8Bytes(evt, evt.GetType())),
new MySqlParameter(@"sender", evt.Sender?.UserId < 1 ? null : (long?)evt.Sender.UserId),
new MySqlParameter(@"sender_name", evt.Sender?.Username),
new MySqlParameter(@"sender_colour", evt.Sender?.Colour.Raw),
new MySqlParameter(@"sender_colour", evt.Sender?.Colour.ToMisuzu()),
new MySqlParameter(@"sender_rank", evt.Sender?.Rank),
new MySqlParameter(@"sender_nick", evt.Sender?.Nickname),
new MySqlParameter(@"sender_perms", evt.Sender?.Permissions)
@ -164,7 +164,7 @@ namespace SharpChat {
evt.Sender = new BasicUser {
UserId = reader.GetInt64(@"event_sender"),
Username = reader.GetString(@"event_sender_name"),
Colour = new ChatColour(reader.GetInt32(@"event_sender_colour")),
Colour = ChatColour.FromMisuzu(reader.GetInt32(@"event_sender_colour")),
Rank = reader.GetInt32(@"event_sender_rank"),
Nickname = reader.IsDBNull(reader.GetOrdinal(@"event_sender_nick")) ? null : reader.GetString(@"event_sender_nick"),
Permissions = (ChatUserPermissions)reader.GetInt32(@"event_sender_perms")