Skip to content

Commit

Permalink
feat: Add YoutubeColorConverter to handle youtube color number
Browse files Browse the repository at this point in the history
- Add a new method `YoutubeColorConverter` in the `Helper.cs` file that converts a long color value to a valid RGB color.
- Update the `Chat.cs` file to use `long` data type for properties `headerBackgroundColor`, `headerTextColor`, `bodyBackgroundColor`, `bodyTextColor`, `authorNameTextColor`, and `timestampColor`.
- Update the `DiscordService.cs` file to use the `YoutubeColorConverter` method in `Helper.cs` to convert color values for `bodyBackgroundColor` and `backgroundColor` properties.

Signed-off-by: 陳鈞 <[email protected]>
  • Loading branch information
jim60105 committed Dec 13, 2023
1 parent 615bdf8 commit 6c22e18
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 8 deletions.
15 changes: 15 additions & 0 deletions Helper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,4 +75,19 @@ public static string GetOriginalImage(string? url)

return url;
}

public static string YoutubeColorConverter(long color)
{
color &= 16777215;
long[] temp = [(color & 16711680) >> 16, (color & 65280) >> 8, color & 255];
int r = (int)temp[0];
int g = (int)temp[1];
int b = (int)temp[2];

if (r != (r & 255) || g != (g & 255) || b != (b & 255))
throw new Exception($"\"({r},{g},{b})\" is not a valid RGB color");

int hex = r << 16 | g << 8 | b;
return r < 16 ? "#" + (16777216 | hex).ToString("X")[1..] : "#" + hex.ToString("X");
}
}
12 changes: 6 additions & 6 deletions Models/Chat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,14 +163,14 @@ public class LiveChatPaidMessageRenderer
public AuthorPhoto authorPhoto { get; set; }
public PurchaseAmountText purchaseAmountText { get; set; }
public Message message { get; set; }
public object headerBackgroundColor { get; set; }
public object headerTextColor { get; set; }
public object bodyBackgroundColor { get; set; }
public object bodyTextColor { get; set; }
public long headerBackgroundColor { get; set; }
public long headerTextColor { get; set; }
public long bodyBackgroundColor { get; set; }
public long bodyTextColor { get; set; }
public string authorExternalChannelId { get; set; }
public object authorNameTextColor { get; set; }
public long authorNameTextColor { get; set; }
public ContextMenuEndpoint contextMenuEndpoint { get; set; }
public object timestampColor { get; set; }
public long timestampColor { get; set; }
public ContextMenuAccessibility contextMenuAccessibility { get; set; }
public string trackingParams { get; set; }
}
Expand Down
4 changes: 2 additions & 2 deletions Services/DiscordService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ private static EmbedBuilder BuildSuperChatMessage(ref EmbedBuilder eb, LiveChatP
eb.WithFields(new EmbedFieldBuilder[] { new EmbedFieldBuilder().WithName("Amount").WithValue(liveChatPaidMessage.purchaseAmountText?.simpleText) });

// Super Chat Background Color
Color bgColor = (Color)System.Drawing.ColorTranslator.FromHtml(string.Format("#{0:X}", liveChatPaidMessage.bodyBackgroundColor));
Color bgColor = (Color)System.Drawing.ColorTranslator.FromHtml(Helper.YoutubeColorConverter(liveChatPaidMessage.bodyBackgroundColor));
eb.WithColor(bgColor);

// Timestamp
Expand Down Expand Up @@ -236,7 +236,7 @@ private static EmbedBuilder BuildSuperChatStickerMessage(ref EmbedBuilder eb, Li
eb.WithFields(new EmbedFieldBuilder[] { new EmbedFieldBuilder().WithName("Amount").WithValue(liveChatPaidSticker.purchaseAmountText?.simpleText) });

// Super Chat Background Color
Color bgColor = (Color)System.Drawing.ColorTranslator.FromHtml(string.Format("#{0:X}", liveChatPaidSticker.backgroundColor));
Color bgColor = (Color)System.Drawing.ColorTranslator.FromHtml(Helper.YoutubeColorConverter(liveChatPaidSticker.backgroundColor));
eb.WithColor(bgColor);

// Super Chat Sticker Picture
Expand Down

0 comments on commit 6c22e18

Please sign in to comment.