Convert Colour class to a struct.
This commit is contained in:
parent
4104e40843
commit
5e3eecda8c
3 changed files with 46 additions and 47 deletions
|
@ -1,55 +1,54 @@
|
||||||
namespace SharpChat {
|
namespace SharpChat {
|
||||||
public class ChatColour {
|
public struct ChatColour {
|
||||||
public const int INHERIT = 0x40000000;
|
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) {
|
public ChatColour() {
|
||||||
Inherit = inherit;
|
Red = 0;
|
||||||
|
Green = 0;
|
||||||
|
Blue = 0;
|
||||||
|
Inherits = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
public ChatColour(int colour) {
|
public ChatColour(byte red, byte green, byte blue) {
|
||||||
Raw = colour;
|
Red = red;
|
||||||
}
|
Green = green;
|
||||||
|
Blue = blue;
|
||||||
public bool Inherit {
|
Inherits = false;
|
||||||
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 override string ToString() {
|
public override string ToString() {
|
||||||
if(Inherit)
|
return Inherits
|
||||||
return @"inherit";
|
? "inherit"
|
||||||
return string.Format(@"#{0:X6}", Raw);
|
: 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -131,7 +131,7 @@ namespace SharpChat {
|
||||||
if(Status == ChatUserStatus.Offline)
|
if(Status == ChatUserStatus.Offline)
|
||||||
Status = ChatUserStatus.Online;
|
Status = ChatUserStatus.Online;
|
||||||
|
|
||||||
Colour = new(auth.ColourRaw);
|
Colour = ChatColour.FromMisuzu(auth.ColourRaw);
|
||||||
Rank = auth.Rank;
|
Rank = auth.Rank;
|
||||||
Permissions = auth.Permissions;
|
Permissions = auth.Permissions;
|
||||||
|
|
||||||
|
|
|
@ -137,7 +137,7 @@ namespace SharpChat {
|
||||||
new MySqlParameter(@"data", JsonSerializer.SerializeToUtf8Bytes(evt, evt.GetType())),
|
new MySqlParameter(@"data", JsonSerializer.SerializeToUtf8Bytes(evt, evt.GetType())),
|
||||||
new MySqlParameter(@"sender", evt.Sender?.UserId < 1 ? null : (long?)evt.Sender.UserId),
|
new MySqlParameter(@"sender", evt.Sender?.UserId < 1 ? null : (long?)evt.Sender.UserId),
|
||||||
new MySqlParameter(@"sender_name", evt.Sender?.Username),
|
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_rank", evt.Sender?.Rank),
|
||||||
new MySqlParameter(@"sender_nick", evt.Sender?.Nickname),
|
new MySqlParameter(@"sender_nick", evt.Sender?.Nickname),
|
||||||
new MySqlParameter(@"sender_perms", evt.Sender?.Permissions)
|
new MySqlParameter(@"sender_perms", evt.Sender?.Permissions)
|
||||||
|
@ -164,7 +164,7 @@ namespace SharpChat {
|
||||||
evt.Sender = new BasicUser {
|
evt.Sender = new BasicUser {
|
||||||
UserId = reader.GetInt64(@"event_sender"),
|
UserId = reader.GetInt64(@"event_sender"),
|
||||||
Username = reader.GetString(@"event_sender_name"),
|
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"),
|
Rank = reader.GetInt32(@"event_sender_rank"),
|
||||||
Nickname = reader.IsDBNull(reader.GetOrdinal(@"event_sender_nick")) ? null : reader.GetString(@"event_sender_nick"),
|
Nickname = reader.IsDBNull(reader.GetOrdinal(@"event_sender_nick")) ? null : reader.GetString(@"event_sender_nick"),
|
||||||
Permissions = (ChatUserPermissions)reader.GetInt32(@"event_sender_perms")
|
Permissions = (ChatUserPermissions)reader.GetInt32(@"event_sender_perms")
|
||||||
|
|
Loading…
Reference in a new issue