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

56 lines
1.4 KiB
C#
Raw Normal View History

2017-05-14 12:02:51 +00:00
using System;
using System.Globalization;
namespace Maki
{
public class DiscordColour
{
public DiscordColour(uint raw)
{
Raw = raw;
}
public DiscordColour(byte red, byte green, byte blue)
{
Raw = (uint)((red << 16) + (green << 8) + 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);
}
public uint Raw = uint.MinValue;
public byte Red
{
get => (byte)(Raw >> 16 & 0xFF);
set => Raw = (uint)((value << 16) + (Green << 8) + Blue);
}
public byte Green
{
get => (byte)(Raw >> 8 & 0xFF);
set => Raw = (uint)((Red << 16) + (value << 8) + Blue);
}
public byte Blue
{
get => (byte)(Raw & 0xFF);
set => Raw = (uint)((Red << 16) + (Green << 8) + value);
}
public string Hex => $"{Red:X2}{Green:X2}{Blue:X2}";
}
}