this repo has no description
at develop 2.8 kB view raw
1using System.Drawing; 2using System.Numerics; 3 4namespace Peridot; 5 6/// <summary> 7/// Represetns a RGBA color with floating point values. 8/// All components must be between 0.0 and 1.0. 9/// </summary> 10public partial struct ColorF : IEquatable<ColorF> 11{ 12 private Vector4 m_rgba; 13 14 /// <summary> 15 /// Constructs a new ColorF from components. 16 /// </summary> 17 /// <param name="r">Red component.</param> 18 /// <param name="g">Green component.</param> 19 /// <param name="b">Blue component.</param> 20 /// <param name="a">Alpha component.</param> 21 public ColorF(float r, float g, float b, float a) 22 { 23 m_rgba = new(r, g, b, a); 24 } 25 26 /// <summary> 27 /// Constructs a new ColorF from a Color. 28 /// </summary> 29 /// <param name="color">Color.</param> 30 public ColorF(Color color) 31 { 32 m_rgba = new Vector4(color.R, color.G, color.B, color.A) * (1f / 255f); 33 } 34 35 /// <summary> 36 /// Constructs a new ColorF from a Vector4. 37 /// </summary> 38 /// <param name="color">Color.</param> 39 public ColorF(Vector4 color) 40 { 41 m_rgba = color; 42 } 43 44 /// <summary> 45 /// Constructs a new gray color. 46 /// </summary> 47 /// <param name="grayColor"></param> 48 public ColorF(float grayColor) 49 { 50 m_rgba = new Vector4(grayColor, grayColor, grayColor, 1f); 51 } 52 53 /// <summary> 54 /// Constructs a new gray color. 55 /// </summary> 56 /// <param name="grayColor"></param> 57 /// <param name="alpha"></param> 58 public ColorF(float grayColor, float alpha) 59 { 60 m_rgba = new Vector4(grayColor, grayColor, grayColor, alpha); 61 } 62 63 /// <summary> 64 /// The red color component. 65 /// </summary> 66 public float R { get => m_rgba.X; set => m_rgba.X = value; } 67 68 /// <summary> 69 /// The green color component. 70 /// </summary> 71 public float G { get => m_rgba.Y; set => m_rgba.Y = value; } 72 73 /// <summary> 74 /// The blue color component. 75 /// </summary> 76 public float B { get => m_rgba.Z; set => m_rgba.Z = value; } 77 78 /// <summary> 79 /// The alpha color component. 80 /// </summary> 81 public float A { get => m_rgba.W; set => m_rgba.W = value; } 82 83 /// <summary> 84 /// Gets or sets components RGBA as a <see cref="Vector4"/>. 85 /// </summary> 86 public Vector4 AsVector { get => m_rgba; set => m_rgba = value; } 87 88 /// <inheritdoc/> 89 public bool Equals(ColorF other) 90 { 91 return m_rgba.Equals(other.m_rgba); 92 } 93 94 /// <inheritdoc/> 95 public override bool Equals(object? obj) 96 { 97 return obj != null && obj is ColorF other && Equals(other); 98 } 99 100 /// <inheritdoc/> 101 public override int GetHashCode() 102 { 103 return m_rgba.GetHashCode(); 104 } 105 106 /// <inheritdoc/> 107 public override string ToString() 108 { 109 return $"{{R: {R}, G: {G}, B: {B}, A: {A}}}"; 110 } 111}