You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
57 lines
1.9 KiB
57 lines
1.9 KiB
2 years ago
|
using System;
|
||
|
using System.Diagnostics.CodeAnalysis;
|
||
|
using System.Runtime.CompilerServices;
|
||
|
using System.Runtime.InteropServices;
|
||
|
using Silk.NET.Maths;
|
||
|
|
||
|
namespace gaemstone.Client;
|
||
|
|
||
|
[StructLayout(LayoutKind.Explicit)]
|
||
|
public readonly struct Color
|
||
|
: IEquatable<Color>
|
||
|
{
|
||
|
public static Color Transparent { get; } = default;
|
||
|
|
||
|
public static Color Black { get; } = FromRGB(0x000000);
|
||
|
public static Color White { get; } = FromRGB(0xFFFFFF);
|
||
|
|
||
|
[FieldOffset(0)]
|
||
|
public readonly uint Value;
|
||
|
|
||
|
[FieldOffset(0)]
|
||
|
public readonly byte R;
|
||
|
[FieldOffset(1)]
|
||
|
public readonly byte G;
|
||
|
[FieldOffset(2)]
|
||
|
public readonly byte B;
|
||
|
[FieldOffset(3)]
|
||
|
public readonly byte A;
|
||
|
|
||
|
private Color(uint value)
|
||
|
{ Unsafe.SkipInit(out this); Value = value; }
|
||
|
private Color(byte r, byte g, byte b, byte a)
|
||
|
{ Unsafe.SkipInit(out this); R = r; G = g; B = b; A = a; }
|
||
|
|
||
|
public static Color FromRGBA(uint rgba) => new(rgba);
|
||
|
public static Color FromRGB(uint rgb) => new(rgb | 0xFF000000);
|
||
|
|
||
|
public static Color FromRGBA(byte r, byte g, byte b, byte a) => new(r, g, b, a);
|
||
|
public static Color FromRGB(byte r, byte g, byte b) => new(r, g, b, 0xFF);
|
||
|
|
||
|
public bool Equals(Color other)
|
||
|
=> Value == other.Value;
|
||
|
public override bool Equals([NotNullWhen(true)] object? obj)
|
||
|
=> (obj is Color color) && Equals(color);
|
||
|
public override int GetHashCode()
|
||
|
=> Value.GetHashCode();
|
||
|
public override string? ToString()
|
||
|
=> $"Color(0x{Value:X8})";
|
||
|
|
||
|
public static bool operator ==(Color left, Color right) => left.Equals(right);
|
||
|
public static bool operator !=(Color left, Color right) => !left.Equals(right);
|
||
|
|
||
|
public static implicit operator System.Drawing.Color(Color color) => System.Drawing.Color.FromArgb(color.A, color.R, color.G, color.B);
|
||
|
public static implicit operator Vector4D<float>(Color color) => new(color.R / 255F, color.G / 255F, color.B / 255F, color.A / 255F);
|
||
|
public static implicit operator Vector4D<byte>(Color color) => new(color.R, color.G, color.B, color.A);
|
||
|
}
|