Split Colour struct into two records.

This commit is contained in:
flash 2025-04-26 13:15:27 +00:00
parent f5eab926de
commit 51f5c4c948
Signed by: flash
GPG key ID: 2C9C2C574D47FE3E
16 changed files with 39 additions and 95 deletions

View file

@ -133,7 +133,7 @@ namespace SharpChat {
User user,
string? userName = null,
string? nickName = null,
Colour? colour = null,
ColourInheritable? colour = null,
UserStatus? status = null,
string? statusText = null,
int? rank = null,

View file

@ -6,7 +6,7 @@
string channelName,
long senderId,
string senderName,
Colour senderColour,
ColourInheritable senderColour,
int senderRank,
string senderNick,
UserPermissions senderPerms,

View file

@ -12,7 +12,7 @@ namespace SharpChat.EventStorage {
string channelName,
long senderId,
string senderName,
Colour senderColour,
ColourInheritable senderColour,
int senderRank,
string senderNick,
UserPermissions senderPerms,
@ -74,7 +74,7 @@ namespace SharpChat.EventStorage {
reader.IsDBNull(reader.GetOrdinal("event_sender")) ? null : new User(
reader.GetInt64("event_sender"),
reader.IsDBNull(reader.GetOrdinal("event_sender_name")) ? string.Empty : reader.GetString("event_sender_name"),
Colour.FromMisuzu(reader.GetInt32("event_sender_colour")),
ColourInheritable.FromMisuzu(reader.GetInt32("event_sender_colour")),
reader.GetInt32("event_sender_rank"),
(UserPermissions)reader.GetInt32("event_sender_perms"),
reader.IsDBNull(reader.GetOrdinal("event_sender_nick")) ? string.Empty : reader.GetString("event_sender_nick")

View file

@ -10,7 +10,7 @@ namespace SharpChat.EventStorage {
string channelName,
long senderId,
string senderName,
Colour senderColour,
ColourInheritable senderColour,
int senderRank,
string senderNick,
UserPermissions senderPerms,

View file

@ -4,7 +4,7 @@
string channelName,
long senderId,
string senderName,
Colour senderColour,
ColourInheritable senderColour,
int senderRank,
string senderNickName,
UserPermissions senderPerms,
@ -18,7 +18,7 @@
public string ChannelName { get; } = channelName;
public long SenderId { get; } = senderId;
public string SenderName { get; } = senderName;
public Colour SenderColour { get; } = senderColour;
public ColourInheritable SenderColour { get; } = senderColour;
public int SenderRank { get; } = senderRank;
public string SenderNickName { get; } = senderNickName;
public UserPermissions SenderPerms { get; } = senderPerms;

View file

@ -17,7 +17,7 @@ namespace SharpChat.Misuzu {
[JsonPropertyName("colour_raw")]
public int ColourRaw { get; set; }
public Colour Colour => Colour.FromMisuzu(ColourRaw);
public ColourInheritable Colour => ColourInheritable.FromMisuzu(ColourRaw);
[JsonPropertyName("hierarchy")]
public int Rank { get; set; }

View file

@ -26,6 +26,6 @@ namespace SharpChat.Misuzu {
public bool HasExpired => !IsPermanent && DateTimeOffset.UtcNow >= ExpiresAt;
public Colour UserColour => Colour.FromMisuzu(UserColourRaw);
public ColourInheritable UserColour => ColourInheritable.FromMisuzu(UserColourRaw);
}
}

View file

@ -4,7 +4,7 @@ namespace SharpChat.S2CPackets {
public class AuthSuccessS2CPacket(
long userId,
string userName,
Colour userColour,
ColourInheritable userColour,
int userRank,
UserPermissions userPerms,
string channelName,

View file

@ -2,7 +2,7 @@
namespace SharpChat.S2CPackets {
public class ContextUsersS2CPacket(IEnumerable<ContextUsersS2CPacket.Entry> entries) : S2CPacket {
public record Entry(long id, string name, Colour colour, int rank, UserPermissions perms, bool visible);
public record Entry(long id, string name, ColourInheritable colour, int rank, UserPermissions perms, bool visible);
public string Pack() {
StringBuilder sb = new();

View file

@ -5,7 +5,7 @@ namespace SharpChat.S2CPackets {
long msgId,
long userId,
string userName,
Colour userColour,
ColourInheritable userColour,
int userRank,
UserPermissions userPerms
) : S2CPacket {

View file

@ -6,7 +6,7 @@ namespace SharpChat.S2CPackets {
DateTimeOffset joined,
long userId,
string userName,
Colour userColour,
ColourInheritable userColour,
int userRank,
UserPermissions userPerms
) : S2CPacket {

View file

@ -4,7 +4,7 @@ namespace SharpChat.S2CPackets {
public class UserUpdateS2CPacket(
long userId,
string userName,
Colour userColour,
ColourInheritable userColour,
int userRank,
UserPermissions userPerms
) : S2CPacket {

View file

@ -6,7 +6,7 @@ namespace SharpChat {
public class User(
long userId,
string userName,
Colour colour,
ColourInheritable colour,
int rank,
UserPermissions perms,
string nickName = "",
@ -19,7 +19,7 @@ namespace SharpChat {
public long UserId { get; } = userId;
public string UserName { get; set; } = userName ?? throw new ArgumentNullException(nameof(userName));
public Colour Colour { get; set; } = colour;
public ColourInheritable Colour { get; set; } = colour;
public int Rank { get; set; } = rank;
public UserPermissions Permissions { get; set; } = perms;
public string NickName { get; set; } = nickName;

View file

@ -1,79 +0,0 @@
using System.Diagnostics.CodeAnalysis;
namespace SharpChat {
public readonly struct Colour {
public byte Red { get; }
public byte Green { get; }
public byte Blue { get; }
public bool Inherits { get; }
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 override bool Equals([NotNullWhen(true)] object? obj) {
return obj is Colour colour && Equals(colour);
}
public bool Equals(Colour other) {
return Red == other.Red
&& Green == other.Green
&& Blue == other.Blue
&& Inherits == other.Inherits;
}
public override int GetHashCode() {
return ToMisuzu();
}
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);
}
public static bool operator ==(Colour left, Colour right) {
return left.Equals(right);
}
public static bool operator !=(Colour left, Colour right) {
return !(left == right);
}
}
}

View file

@ -0,0 +1,14 @@
namespace SharpChat {
public readonly record struct ColourInheritable(ColourRgb? rgb) {
public static readonly ColourInheritable None = new(null);
public override string ToString() => rgb.HasValue ? rgb.Value.ToString() : "inherit";
public static ColourInheritable FromRaw(int? raw) => raw is null ? None : new(new ColourRgb(raw.Value));
public static ColourInheritable FromRgb(byte red, byte green, byte blue) => new(ColourRgb.FromRgb(red, green, blue));
// these should go Away
private const int MSZ_INHERIT = 0x40000000;
public int ToMisuzu() => rgb.HasValue ? rgb.Value.Raw : MSZ_INHERIT;
public static ColourInheritable FromMisuzu(int msz) => (msz & MSZ_INHERIT) > 0 ? None : new(new ColourRgb(msz & 0xFFFFFF));
}
}

View file

@ -0,0 +1,9 @@
namespace SharpChat {
public readonly record struct ColourRgb(int Raw) {
public byte Red => (byte)((Raw >> 16) & 0xFF);
public byte Green => (byte)((Raw >> 8) & 0xFF);
public byte Blue => (byte)(Raw & 0xFF);
public override string ToString() => string.Format("#{0:x2}{1:x2}{2:x2}", Red, Green, Blue);
public static ColourRgb FromRgb(byte red, byte green, byte blue) => new(red << 16 | green << 8 | blue);
}
}