Skip to content

Commit

Permalink
Add ToOkHsv and ToOkHsl methods for Color in C#
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bonjorno7 committed Apr 26, 2023
1 parent e8d296e commit 56d7c2c
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 0 deletions.
22 changes: 22 additions & 0 deletions modules/mono/glue/GodotSharp/GodotSharp/Core/Color.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/// <summary>
/// Converts a color to OK HSV values.
/// </summary>
/// <param name="hue">Output parameter for the OK HSV hue.</param>
/// <param name="saturation">Output parameter for the OK HSV saturation.</param>
/// <param name="value">Output parameter for the OK HSV value.</param>
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);
}

/// <summary>
/// Constructs a color from an OK HSL profile. The <paramref name="hue"/>,
/// <paramref name="saturation"/>, and <paramref name="lightness"/> are typically
Expand All @@ -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);
}

/// <summary>
/// Converts a color to OK HSL values.
/// </summary>
/// <param name="hue">Output parameter for the OK HSL hue.</param>
/// <param name="saturation">Output parameter for the OK HSL saturation.</param>
/// <param name="lightness">Output parameter for the OK HSL lightness.</param>
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);
}

/// <summary>
/// Encodes a <see cref="Color"/> from a RGBE9995 format integer.
/// See <see cref="Image.Format.Rgbe9995"/>.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 16 additions & 0 deletions modules/mono/glue/runtime_interop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -524,13 +524,27 @@ 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;
memnew_placement(dest, Color(Color::from_ok_hsl(p_h, p_s, p_l, p_alpha)));
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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 56d7c2c

Please sign in to comment.