From 51925dd42de1157e03bf19dc6d6749137cd82a4f Mon Sep 17 00:00:00 2001
From: Nicky McDonald <43690021+nickyMcDonald@users.noreply.github.com>
Date: Sat, 10 Aug 2024 04:19:43 -0400
Subject: [PATCH] Additional Color Constructors (#261)
---
Raylib-cs/types/Color.cs | 58 ++++++++++++++++++++++++++++++++++++++++
1 file changed, 58 insertions(+)
diff --git a/Raylib-cs/types/Color.cs b/Raylib-cs/types/Color.cs
index 8a47d63..4ba3476 100644
--- a/Raylib-cs/types/Color.cs
+++ b/Raylib-cs/types/Color.cs
@@ -43,6 +43,9 @@ public partial struct Color
public static readonly Color Magenta = new Color(255, 0, 255, 255);
public static readonly Color RayWhite = new Color(245, 245, 245, 255);
+ ///
+ /// Constructor with transparency
+ ///
public Color(byte r, byte g, byte b, byte a)
{
this.R = r;
@@ -51,6 +54,21 @@ public partial struct Color
this.A = a;
}
+ ///
+ /// Constructor without transparency, the color is made opaque by setting to 255
+ ///
+ public Color(byte r, byte g, byte b)
+ {
+ this.R = r;
+ this.G = g;
+ this.B = b;
+ this.A = 255;
+ }
+
+ ///
+ /// .
+ /// Accepts 's and converts them into 's by
+ ///
public Color(int r, int g, int b, int a)
{
this.R = Convert.ToByte(r);
@@ -59,6 +77,46 @@ public partial struct Color
this.A = Convert.ToByte(a);
}
+ ///
+ /// .
+ /// Accepts 's and converts them into 's by
+ ///
+ public Color(int r, int g, int b)
+ {
+ this.R = Convert.ToByte(r);
+ this.G = Convert.ToByte(g);
+ this.B = Convert.ToByte(b);
+ this.A = 255;
+ }
+
+ ///
+ /// .
+ /// Accepts 's, upscales and clamps them to the range 0..255.
+ /// Then they are converted to 's by rounding.
+ ///
+ public Color(float r, float g, float b, float a)
+ {
+ // X = (byte)Math.Clamp(MathF.Round(x * 255), 0f, 255f);
+ this.R = (byte)((r < 0) ? 0 : ((r > 1) ? 255 : ((r * 255) + .5f)));
+ this.G = (byte)((g < 0) ? 0 : ((g > 1) ? 255 : ((g * 255) + .5f)));
+ this.B = (byte)((b < 0) ? 0 : ((b > 1) ? 255 : ((b * 255) + .5f)));
+ this.A = (byte)((a < 0) ? 0 : ((a > 1) ? 255 : ((a * 255) + .5f)));
+ }
+
+ ///
+ /// .
+ /// Accepts 's, upscales and clamps them to the range 0..255.
+ /// Then they are converted to 's by rounding.
+ ///
+ public Color(float r, float g, float b)
+ {
+ // X = (byte)Math.Clamp(MathF.Round(x * 255), 0f, 255f);
+ this.R = (byte)((r < 0) ? 0 : ((r > 1) ? 255 : ((r * 255) + .5f)));
+ this.G = (byte)((g < 0) ? 0 : ((g > 1) ? 255 : ((g * 255) + .5f)));
+ this.B = (byte)((b < 0) ? 0 : ((b > 1) ? 255 : ((b * 255) + .5f)));
+ this.A = 255;
+ }
+
public override string ToString()
{
return $"{{R:{R} G:{G} B:{B} A:{A}}}";