Files
Voile/Voile/Source/Utils/Color.cs

130 lines
4.3 KiB
C#

namespace Voile
{
// Based on https://github.com/ppr-game/PPR/blob/engine/PER.Util/src/Color.cs
/// <summary>
/// A record struct representing a color.
/// </summary>
public record struct Color
{
// TODO: add more HTML colors.
public static Color AliceBlue = new(0xF0F8FF);
public static Color AntiqueWhite = new(0xFAEBD7);
public static Color Aqua = new(0x00FFFF);
public static Color Aquamarine = new(0x7FFFD4);
public static Color Azure = new(0xF0FFFF);
public static Color Beige = new(0xF5F5DC);
public static Color Bisque = new(0xFFE4C4);
public static Color Black = new(0x000000);
public static Color BlanchedAlmond = new(0xFFEBCD);
public static Color Blue = new(0x0000FF);
public static Color BlueViolet = new(0x8A2BE2);
public static Color Brown = new(0xA52A2A);
public static Color BurlyWood = new(0xDEB887);
public static Color CadetBlue = new(0x5F9EA0);
public static Color Chartreuse = new(0x7FFF00);
public static Color Chocolate = new(0xD2691E);
public static Color Coral = new(0xFF7F50);
public static Color CornflowerBlue = new(0x6495ED);
public static Color Cornsilk = new(0xFFF8DC);
public static Color Crimson = new(0xDC143C);
public static Color Cyan = new(0x00FFFF);
public static Color DarkBlue = new(0x00008B);
public static Color DarkCyan = new(0x008B8B);
public static Color White = new(0xFFFFFF);
public static Color Green = new(0x00FF00);
public static Color Red = new(0xFF0000);
public byte R { get; set; }
public byte G { get; set; }
public byte B { get; set; }
public byte A { get; set; } = 255;
public int Argb
{
get
{
int a = A << 24;
int r = R << 16;
int g = G << 8;
int b = B;
return a | r | g | b;
}
}
public Color(float r, float g, float b, float a = 1.0f)
{
R = (byte)Math.Clamp(r * 255, 0, 255);
G = (byte)Math.Clamp(g * 255, 0, 255);
B = (byte)Math.Clamp(b * 255, 0, 255);
A = (byte)Math.Clamp(a * 255, 0, 255);
}
public Color(byte r, byte g, byte b, byte a = 255)
{
R = r;
G = g;
B = b;
A = a;
}
public Color(int hex)
{
A = 255; // Default alpha to 255 if not provided
B = (byte)(hex & 0xFF);
G = (byte)((hex >> 8) & 0xFF);
R = (byte)((hex >> 16) & 0xFF);
if (hex > 0xFFFFFF) // If the hex value includes alpha
{
A = (byte)((hex >> 24) & 0xFF);
}
}
public static Color FromHexString(string hex)
{
if (hex.StartsWith("#"))
{
hex = hex[1..];
}
if (hex.Length == 6)
{
int rgb = int.Parse(hex, System.Globalization.NumberStyles.HexNumber);
return new Color(rgb);
}
else if (hex.Length == 8)
{
int rgba = int.Parse(hex, System.Globalization.NumberStyles.HexNumber);
return new Color(rgba);
}
else
{
throw new ArgumentException("Invalid hex color format. Use #RRGGBB or #RRGGBBAA.");
}
}
public Color Lightened(float amount)
{
var result = this;
result.R = (byte)Math.Min(255, R + (255 - R) * amount);
result.G = (byte)Math.Min(255, G + (255 - G) * amount);
result.B = (byte)Math.Min(255, B + (255 - B) * amount);
return result;
}
public Color Darkened(float amount)
{
var result = this;
result.R = (byte)(R * (1.0f - amount));
result.G = (byte)(G * (1.0f - amount));
result.B = (byte)(B * (1.0f - amount));
return result;
}
public System.Drawing.Color ToSystemColor()
{
var result = System.Drawing.Color.FromArgb(Argb);
return result;
}
}
}