From b6672a9bebff546a8afca988db36f71b4daf4b38 Mon Sep 17 00:00:00 2001 From: kobewi Date: Thu, 26 Dec 2024 13:02:37 +0100 Subject: [PATCH] Add Color.from_rgba8 and deprecate Color8 --- core/math/color.cpp | 4 ++++ core/math/color.h | 1 + core/variant/variant_call.cpp | 3 ++- doc/classes/Color.xml | 16 ++++++++++++++++ modules/gdscript/doc_classes/@GDScript.xml | 2 +- modules/gdscript/gdscript_utility_functions.cpp | 7 +++---- 6 files changed, 27 insertions(+), 6 deletions(-) diff --git a/core/math/color.cpp b/core/math/color.cpp index 1638acd74d8b..4c31d8149d8a 100644 --- a/core/math/color.cpp +++ b/core/math/color.cpp @@ -482,6 +482,10 @@ Color Color::from_rgbe9995(uint32_t p_rgbe) { return Color(rd, gd, bd, 1.0f); } +Color Color::from_rgba8(int64_t p_r8, int64_t p_g8, int64_t p_b8, int64_t p_a8) { + return Color(p_r8 / 255.0f, p_g8 / 255.0f, p_b8 / 255.0f, p_a8 / 255.0f); +} + Color::operator String() const { return "(" + String::num(r, 4) + ", " + String::num(g, 4) + ", " + String::num(b, 4) + ", " + String::num(a, 4) + ")"; } diff --git a/core/math/color.h b/core/math/color.h index 3943d5ada656..c2ffde1849ca 100644 --- a/core/math/color.h +++ b/core/math/color.h @@ -213,6 +213,7 @@ struct [[nodiscard]] Color { static Color from_hsv(float p_h, float p_s, float p_v, float p_alpha = 1.0f); static Color from_ok_hsl(float p_h, float p_s, float p_l, float p_alpha = 1.0f); static Color from_rgbe9995(uint32_t p_rgbe); + static Color from_rgba8(int64_t p_r8, int64_t p_g8, int64_t p_b8, int64_t p_a8 = 255); _FORCE_INLINE_ bool operator<(const Color &p_color) const; // Used in set keys. operator String() const; diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp index d612cb9cead1..5f6eb8399a27 100644 --- a/core/variant/variant_call.cpp +++ b/core/variant/variant_call.cpp @@ -2104,11 +2104,12 @@ static void _register_variant_builtin_methods_math() { bind_static_method(Color, hex64, sarray("hex"), varray()); bind_static_method(Color, html, sarray("rgba"), varray()); bind_static_method(Color, html_is_valid, sarray("color"), varray()); + bind_static_method(Color, from_string, sarray("str", "default"), varray()); bind_static_method(Color, from_hsv, sarray("h", "s", "v", "alpha"), varray(1.0)); bind_static_method(Color, from_ok_hsl, sarray("h", "s", "l", "alpha"), varray(1.0)); - bind_static_method(Color, from_rgbe9995, sarray("rgbe"), varray()); + bind_static_method(Color, from_rgba8, sarray("r8", "g8", "b8", "a8"), varray(255)); } static void _register_variant_builtin_methods_misc() { diff --git a/doc/classes/Color.xml b/doc/classes/Color.xml index 546d90fa3c4f..5434b5ed5915 100644 --- a/doc/classes/Color.xml +++ b/doc/classes/Color.xml @@ -177,6 +177,22 @@ [/codeblocks] + + + + + + + + Returns a [Color] constructed from red ([param r8]), green ([param g8]), blue ([param b8]), and optionally alpha ([param a8]) integer channels, each divided by [code]255.0[/code] for their final value. + [codeblock] + var red = Color.from_rgba8(255, 0, 0) # Same as Color(1, 0, 0). + var dark_blue = Color.from_rgba8(0, 0, 51) # Same as Color(0, 0, 0.2). + var my_color = Color.from_rgba8(306, 255, 0, 102) # Same as Color(1.2, 1, 0, 0.4). + [/codeblock] + [b]Note:[/b] Due to the lower precision of [method from_rgba8] compared to the standard [Color] constructor, a color created with [method from_rgba8] will generally not be equal to the same color created with the standard [Color] constructor. Use [method is_equal_approx] for comparisons to avoid issues with floating-point precision error. + + diff --git a/modules/gdscript/doc_classes/@GDScript.xml b/modules/gdscript/doc_classes/@GDScript.xml index e394d194d1a2..1f7bb9d2fb3a 100644 --- a/modules/gdscript/doc_classes/@GDScript.xml +++ b/modules/gdscript/doc_classes/@GDScript.xml @@ -11,7 +11,7 @@ $DOCS_URL/tutorials/scripting/gdscript/gdscript_exports.html - + diff --git a/modules/gdscript/gdscript_utility_functions.cpp b/modules/gdscript/gdscript_utility_functions.cpp index 82460696964d..c46ec9bb062a 100644 --- a/modules/gdscript/gdscript_utility_functions.cpp +++ b/modules/gdscript/gdscript_utility_functions.cpp @@ -321,14 +321,13 @@ struct GDScriptUtilityFunctionsDefinitions { DEBUG_VALIDATE_ARG_TYPE(1, Variant::INT); DEBUG_VALIDATE_ARG_TYPE(2, Variant::INT); - Color color((int64_t)*p_args[0] / 255.0f, (int64_t)*p_args[1] / 255.0f, (int64_t)*p_args[2] / 255.0f); - + int64_t alpha = 255; if (p_arg_count == 4) { DEBUG_VALIDATE_ARG_TYPE(3, Variant::INT); - color.a = (int64_t)*p_args[3] / 255.0f; + alpha = *p_args[3]; } - *r_ret = color; + *r_ret = Color::from_rgba8(*p_args[0], *p_args[1], *p_args[2], alpha); } static inline void print_debug(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {