From 56d7c2c83e70c0ccef67e79207295314f9b29f2b Mon Sep 17 00:00:00 2001 From: bonjorno7 Date: Wed, 26 Apr 2023 02:39:40 +0200 Subject: [PATCH] Add ToOkHsv and ToOkHsl methods for Color in C# It builds, but I can't seem to get Godot working with C# on my end, so I can't really test whether it works. It's not very efficient, converting the color 3 times for one call, but I'm not sure this minor performance improvement would be worth the hassle, so I'm committing it as is. Especially since GDScript doesn't do it efficiently either. --- .../glue/GodotSharp/GodotSharp/Core/Color.cs | 22 +++++++++++++++++++ .../Core/NativeInterop/NativeFuncs.cs | 4 ++++ modules/mono/glue/runtime_interop.cpp | 16 ++++++++++++++ 3 files changed, 42 insertions(+) diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Color.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Color.cs index 0c6d8e916153..ad128adb378a 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Color.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Color.cs @@ -916,6 +916,17 @@ public static Color FromOkHsv(float hue, float saturation, float value, float al return NativeFuncs.godotsharp_color_from_ok_hsv(hue, saturation, value, alpha); } + /// + /// Converts a color to OK HSV values. + /// + /// Output parameter for the OK HSV hue. + /// Output parameter for the OK HSV saturation. + /// Output parameter for the OK HSV value. + public readonly void ToOkHsv(out float hue, out float saturation, out float value) + { + NativeFuncs.godotsharp_color_to_ok_hsv(this, out hue, out saturation, out value); + } + /// /// Constructs a color from an OK HSL profile. The , /// , and are typically @@ -931,6 +942,17 @@ public static Color FromOkHsl(float hue, float saturation, float lightness, floa return NativeFuncs.godotsharp_color_from_ok_hsl(hue, saturation, lightness, alpha); } + /// + /// Converts a color to OK HSL values. + /// + /// Output parameter for the OK HSL hue. + /// Output parameter for the OK HSL saturation. + /// Output parameter for the OK HSL lightness. + public readonly void ToOkHsl(out float hue, out float saturation, out float lightness) + { + NativeFuncs.godotsharp_color_to_ok_hsl(this, out hue, out saturation, out lightness); + } + /// /// Encodes a from a RGBE9995 format integer. /// See . diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/NativeFuncs.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/NativeFuncs.cs index 87efbaf9d9a6..fba8474e364d 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/NativeFuncs.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/NativeInterop/NativeFuncs.cs @@ -155,8 +155,12 @@ internal static partial void godotsharp_callable_call_deferred(in godot_callable internal static partial Color godotsharp_color_from_ok_hsv(float p_h, float p_s, float p_v, float p_alpha); + internal static partial void godotsharp_color_to_ok_hsv(in Color p_color, out float r_h, out float r_s, out float r_v); + internal static partial Color godotsharp_color_from_ok_hsl(float p_h, float p_s, float p_l, float p_alpha); + internal static partial void godotsharp_color_to_ok_hsl(in Color p_color, out float r_h, out float r_s, out float r_l); + // GDNative functions // gdnative.h diff --git a/modules/mono/glue/runtime_interop.cpp b/modules/mono/glue/runtime_interop.cpp index a6a44962a341..1730a590385a 100644 --- a/modules/mono/glue/runtime_interop.cpp +++ b/modules/mono/glue/runtime_interop.cpp @@ -524,6 +524,13 @@ godot_color godotsharp_color_from_ok_hsv(float p_h, float p_s, float p_v, float return ret; } +void godotsharp_color_to_ok_hsv(godot_color *p_color, float *r_h, float *r_s, float *r_v) { + Color *color_val = (Color *)p_color; + *r_h = color_val->get_ok_hsv_h(); + *r_s = color_val->get_ok_hsv_s(); + *r_v = color_val->get_ok_hsv_v(); +} + godot_color godotsharp_color_from_ok_hsl(float p_h, float p_s, float p_l, float p_alpha) { godot_color ret; Color *dest = (Color *)&ret; @@ -531,6 +538,13 @@ godot_color godotsharp_color_from_ok_hsl(float p_h, float p_s, float p_l, float return ret; } +void godotsharp_color_to_ok_hsl(godot_color *p_color, float *r_h, float *r_s, float *r_l) { + Color *color_val = (Color *)p_color; + *r_h = color_val->get_ok_hsl_h(); + *r_s = color_val->get_ok_hsl_s(); + *r_l = color_val->get_ok_hsl_l(); +} + // GDNative functions // gdnative.h @@ -1440,7 +1454,9 @@ static const void *unmanaged_callbacks[]{ (void *)godotsharp_callable_call, (void *)godotsharp_callable_call_deferred, (void *)godotsharp_color_from_ok_hsv, + (void *)godotsharp_color_to_ok_hsv, (void *)godotsharp_color_from_ok_hsl, + (void *)godotsharp_color_to_ok_hsl, (void *)godotsharp_method_bind_ptrcall, (void *)godotsharp_method_bind_call, (void *)godotsharp_variant_new_string_name,