Pārlūkot izejas kodu

Merge pull request #18282 from aaronfranke/better-mathf

[Core] [Mono] Fix Color missing int export methods, added 64-bit
Rémi Verschelde 7 gadi atpakaļ
vecāks
revīzija
47a7dc7b3c

+ 53 - 0
core/color.cpp

@@ -49,6 +49,7 @@ uint32_t Color::to_argb32() const {
 }
 
 uint32_t Color::to_abgr32() const {
+
 	uint32_t c = (uint8_t)Math::round(a * 255);
 	c <<= 8;
 	c |= (uint8_t)Math::round(b * 255);
@@ -73,6 +74,45 @@ uint32_t Color::to_rgba32() const {
 	return c;
 }
 
+uint64_t Color::to_abgr64() const {
+
+	uint64_t c = (uint16_t)Math::round(a * 65535);
+	c <<= 16;
+	c |= (uint16_t)Math::round(b * 65535);
+	c <<= 16;
+	c |= (uint16_t)Math::round(g * 65535);
+	c <<= 16;
+	c |= (uint16_t)Math::round(r * 65535);
+
+	return c;
+}
+
+uint64_t Color::to_argb64() const {
+
+	uint64_t c = (uint16_t)Math::round(a * 65535);
+	c <<= 16;
+	c |= (uint16_t)Math::round(r * 65535);
+	c <<= 16;
+	c |= (uint16_t)Math::round(g * 65535);
+	c <<= 16;
+	c |= (uint16_t)Math::round(b * 65535);
+
+	return c;
+}
+
+uint64_t Color::to_rgba64() const {
+
+	uint64_t c = (uint16_t)Math::round(r * 65535);
+	c <<= 16;
+	c |= (uint16_t)Math::round(g * 65535);
+	c <<= 16;
+	c |= (uint16_t)Math::round(b * 65535);
+	c <<= 16;
+	c |= (uint16_t)Math::round(a * 65535);
+
+	return c;
+}
+
 float Color::get_h() const {
 
 	float min = MIN(r, g);
@@ -200,6 +240,19 @@ Color Color::hex(uint32_t p_hex) {
 	return Color(r, g, b, a);
 }
 
+Color Color::hex64(uint64_t p_hex) {
+
+	float a = (p_hex & 0xFFFF) / 65535.0;
+	p_hex >>= 16;
+	float b = (p_hex & 0xFFFF) / 65535.0;
+	p_hex >>= 16;
+	float g = (p_hex & 0xFFFF) / 65535.0;
+	p_hex >>= 16;
+	float r = (p_hex & 0xFFFF) / 65535.0;
+
+	return Color(r, g, b, a);
+}
+
 static float _parse_col(const String &p_str, int p_ofs) {
 
 	int ig = 0;

+ 4 - 0
core/color.h

@@ -55,6 +55,9 @@ struct Color {
 	uint32_t to_rgba32() const;
 	uint32_t to_argb32() const;
 	uint32_t to_abgr32() const;
+	uint64_t to_rgba64() const;
+	uint64_t to_argb64() const;
+	uint64_t to_abgr64() const;
 	float gray() const;
 	float get_h() const;
 	float get_s() const;
@@ -186,6 +189,7 @@ struct Color {
 	}
 
 	static Color hex(uint32_t p_hex);
+	static Color hex64(uint64_t p_hex);
 	static Color html(const String &p_color);
 	static bool html_is_valid(const String &p_color);
 	static Color named(const String &p_name);

+ 10 - 2
core/variant_call.cpp

@@ -446,8 +446,12 @@ struct _VariantCall {
 	VCALL_LOCALMEM1(Quat, set_euler);
 	VCALL_LOCALMEM2(Quat, set_axis_angle);
 
-	VCALL_LOCALMEM0R(Color, to_rgba32);
 	VCALL_LOCALMEM0R(Color, to_argb32);
+	VCALL_LOCALMEM0R(Color, to_abgr32);
+	VCALL_LOCALMEM0R(Color, to_rgba32);
+	VCALL_LOCALMEM0R(Color, to_argb64);
+	VCALL_LOCALMEM0R(Color, to_abgr64);
+	VCALL_LOCALMEM0R(Color, to_rgba64);
 	VCALL_LOCALMEM0R(Color, gray);
 	VCALL_LOCALMEM0R(Color, inverted);
 	VCALL_LOCALMEM0R(Color, contrasted);
@@ -1613,8 +1617,12 @@ void register_variant_methods() {
 	ADDFUNC1(QUAT, NIL, Quat, set_euler, VECTOR3, "euler", varray());
 	ADDFUNC2(QUAT, NIL, Quat, set_axis_angle, VECTOR3, "axis", REAL, "angle", varray());
 
-	ADDFUNC0R(COLOR, INT, Color, to_rgba32, varray());
 	ADDFUNC0R(COLOR, INT, Color, to_argb32, varray());
+	ADDFUNC0R(COLOR, INT, Color, to_abgr32, varray());
+	ADDFUNC0R(COLOR, INT, Color, to_rgba32, varray());
+	ADDFUNC0R(COLOR, INT, Color, to_argb64, varray());
+	ADDFUNC0R(COLOR, INT, Color, to_abgr64, varray());
+	ADDFUNC0R(COLOR, INT, Color, to_rgba64, varray());
 	ADDFUNC0R(COLOR, REAL, Color, gray, varray());
 	ADDFUNC0R(COLOR, COLOR, Color, inverted, varray());
 	ADDFUNC0R(COLOR, COLOR, Color, contrasted, varray());

+ 57 - 14
doc/classes/Color.xml

@@ -187,17 +187,6 @@
 				[/codeblock]
 			</description>
 		</method>
-		<method name="to_argb32">
-			<return type="int">
-			</return>
-			<description>
-				Returns the color's 32-bit integer in ARGB format (each byte represents a component of the ARGB profile). More compatible with DirectX.
-				[codeblock]
-				var c = Color(1, .5, .2)
-				print(str(c.to_32())) # prints 4294934323
-				[/codeblock]
-			</description>
-		</method>
 		<method name="to_html">
 			<return type="String">
 			</return>
@@ -213,16 +202,70 @@
 				[/codeblock]
 			</description>
 		</method>
+		<method name="to_argb32">
+			<return type="int">
+			</return>
+			<description>
+				Returns the color's 32-bit integer in ARGB format (each byte represents a component of the ARGB profile). ARGB is more compatible with DirectX.
+				[codeblock]
+				var c = Color(1, .5, .2)
+				print(c.to_argb32()) # Prints 4294934323
+				[/codeblock]
+			</description>
+		</method>
+		<method name="to_abgr32">
+			<return type="int">
+			</return>
+			<description>
+				Returns the color's 32-bit integer in ABGR format (each byte represents a component of the ABGR profile). ABGR is the reversed version of the default format.
+				[codeblock]
+				var c = Color(1, .5, .2)
+				print(c.to_abgr32()) # Prints 4281565439
+				[/codeblock]
+			</description>
+		</method>
 		<method name="to_rgba32">
 			<return type="int">
 			</return>
 			<description>
-				Returns the color's 32-bit integer in ARGB format (each byte represents a component of the ARGB profile).
+				Returns the color's 32-bit integer in RGBA format (each byte represents a component of the RGBA profile). RGBA is the format that Godot uses by default.
+				[codeblock]
+				var c = Color(1, .5, .2)
+				print(c.to_rgba32()) # Prints 4286526463
+				[/codeblock]
+			</description>
+		</method>
+		<method name="to_argb64">
+			<return type="int">
+			</return>
+			<description>
+				Returns the color's 64-bit integer in ARGB format (each word represents a component of the ARGB profile). ARGB is more compatible with DirectX.
+				[codeblock]
+				var c = Color(1, .5, .2)
+				print(c.to_argb64()) # Prints -2147470541
+				[/codeblock]
+			</description>
+		</method>
+		<method name="to_abgr64">
+			<return type="int">
+			</return>
+			<description>
+				Returns the color's 64-bit integer in ABGR format (each word represents a component of the ABGR profile). ABGR is the reversed version of the default format.
+				[codeblock]
+				var c = Color(1, .5, .2)
+				print(c.to_abgr64()) # Prints -225178692812801
+				[/codeblock]
+			</description>
+		</method>
+		<method name="to_rgba64">
+			<return type="int">
+			</return>
+			<description>
+				Returns the color's 64-bit integer in RGBA format (each word represents a component of the RGBA profile). RGBA is the format that Godot uses by default.
 				[codeblock]
 				var c = Color(1, .5, .2)
-				print(str(c.to_32())) # prints 4294934323
+				print(c.to_rgba64()) # Prints -140736629309441
 				[/codeblock]
-				[i]This is same as [method to_argb32] but may be changed later to support RGBA format instead[/i].
 			</description>
 		</method>
 	</methods>

+ 73 - 10
modules/mono/glue/cs_files/Color.cs

@@ -293,28 +293,80 @@ namespace Godot
             return res;
         }
 
-        public int ToRgba32()
+        public int ToAbgr32()
         {
-            int c = (byte)(r * 255);
+            int c = (byte)Math.Round(a * 255);
             c <<= 8;
-            c |= (byte)(g * 255);
+            c |= (byte)Math.Round(b * 255);
             c <<= 8;
-            c |= (byte)(b * 255);
+            c |= (byte)Math.Round(g * 255);
             c <<= 8;
-            c |= (byte)(a * 255);
+            c |= (byte)Math.Round(r * 255);
+
+            return c;
+        }
+
+        public long ToAbgr64()
+        {
+            long c = (ushort)Math.Round(a * 65535);
+            c <<= 16;
+            c |= (ushort)Math.Round(b * 65535);
+            c <<= 16;
+            c |= (ushort)Math.Round(g * 65535);
+            c <<= 16;
+            c |= (ushort)Math.Round(r * 65535);
 
             return c;
         }
 
         public int ToArgb32()
         {
-            int c = (byte)(a * 255);
+            int c = (byte)Math.Round(a * 255);
+            c <<= 8;
+            c |= (byte)Math.Round(r * 255);
+            c <<= 8;
+            c |= (byte)Math.Round(g * 255);
+            c <<= 8;
+            c |= (byte)Math.Round(b * 255);
+
+            return c;
+        }
+
+        public long ToArgb64()
+        {
+            long c = (ushort)Math.Round(a * 65535);
+            c <<= 16;
+            c |= (ushort)Math.Round(r * 65535);
+            c <<= 16;
+            c |= (ushort)Math.Round(g * 65535);
+            c <<= 16;
+            c |= (ushort)Math.Round(b * 65535);
+
+            return c;
+        }
+
+        public int ToRgba32()
+        {
+            int c = (byte)Math.Round(r * 255);
             c <<= 8;
-            c |= (byte)(r * 255);
+            c |= (byte)Math.Round(g * 255);
             c <<= 8;
-            c |= (byte)(g * 255);
+            c |= (byte)Math.Round(b * 255);
             c <<= 8;
-            c |= (byte)(b * 255);
+            c |= (byte)Math.Round(a * 255);
+
+            return c;
+        }
+
+        public long ToRgba64()
+        {
+            long c = (ushort)Math.Round(r * 65535);
+            c <<= 16;
+            c |= (ushort)Math.Round(g * 65535);
+            c <<= 16;
+            c |= (ushort)Math.Round(b * 65535);
+            c <<= 16;
+            c |= (ushort)Math.Round(a * 65535);
 
             return c;
         }
@@ -353,6 +405,17 @@ namespace Godot
             r = (rgba & 0xFF) / 255.0f;
         }
 
+        public Color(long rgba)
+        {
+            a = (rgba & 0xFFFF) / 65535.0f;
+            rgba >>= 16;
+            b = (rgba & 0xFFFF) / 65535.0f;
+            rgba >>= 16;
+            g = (rgba & 0xFFFF) / 65535.0f;
+            rgba >>= 16;
+            r = (rgba & 0xFFFF) / 65535.0f;
+        }
+
         private static int _parse_col(string str, int ofs)
         {
             int ig = 0;
@@ -392,7 +455,7 @@ namespace Godot
 
         private String _to_hex(float val)
         {
-            var v = (int) Mathf.Clamp(val * 255.0f, 0, 255);
+            int v = Mathf.RoundToInt(Mathf.Clamp(val * 255, 0, 255));
 
             var ret = string.Empty;
 

+ 1 - 1
modules/mono/glue/cs_files/VERSION.txt

@@ -1 +1 @@
-5
+6