Archived
1
0
Fork 0
This repository has been archived on 2024-05-21. You can view files and clone it, but cannot push or open issues or pull requests.
maki/Maki/DiscordColour.cs

70 lines
1.6 KiB
C#

using System;
using System.Globalization;
namespace Maki
{
public class DiscordColour
{
public int Raw = 0;
public byte Red
{
get => (byte)(Raw >> 16 & 0xFF);
set
{
Raw &= ~0xFF0000;
Raw |= value << 16;
}
}
public byte Green
{
get => (byte)(Raw >> 8 & 0xFF);
set
{
Raw &= ~0xFF00;
Raw |= value << 8;
}
}
public byte Blue
{
get => (byte)(Raw & 0xFF);
set
{
Raw &= ~0xFF;
Raw |= value;
}
}
public string Hex => $"{Red:X2}{Green:X2}{Blue:X2}";
public override string ToString() => $"#{Hex}";
public DiscordColour(int raw)
{
Raw = raw;
}
public DiscordColour(byte red, byte green, byte blue)
{
Raw |= red << 16;
Raw |= green << 8;
Raw |= blue;
}
public DiscordColour(string hex)
{
hex = hex.ToUpper();
if (hex.Length == 3)
hex = new string(hex[0], 2) + new string(hex[1], 2) + new string(hex[2], 2);
if (hex.Length != 6)
throw new FormatException("Invalid hex colour format!");
Red = byte.Parse(hex.Substring(0, 2), NumberStyles.HexNumber);
Green = byte.Parse(hex.Substring(2, 2), NumberStyles.HexNumber);
Blue = byte.Parse(hex.Substring(4, 2), NumberStyles.HexNumber);
}
}
}