Skip to content

Commit

Permalink
Add Color.from_rgba8 and deprecate Color8
Browse files Browse the repository at this point in the history
  • Loading branch information
KoBeWi committed Dec 27, 2024
1 parent 0f95e9f commit 4e48b19
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 6 deletions.
4 changes: 4 additions & 0 deletions core/math/color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) + ")";
}
Expand Down
1 change: 1 addition & 0 deletions core/math/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion core/variant/variant_call.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
16 changes: 16 additions & 0 deletions doc/classes/Color.xml
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,22 @@
[/codeblocks]
</description>
</method>
<method name="from_rgba8" qualifiers="static">
<return type="Color" />
<param index="0" name="r8" type="int" />
<param index="1" name="g8" type="int" />
<param index="2" name="b8" type="int" />
<param index="3" name="a8" type="int" default="255" />
<description>
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.
</description>
</method>
<method name="from_rgbe9995" qualifiers="static">
<return type="Color" />
<param index="0" name="rgbe" type="int" />
Expand Down
2 changes: 1 addition & 1 deletion modules/gdscript/doc_classes/@GDScript.xml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<link title="GDScript exports">$DOCS_URL/tutorials/scripting/gdscript/gdscript_exports.html</link>
</tutorials>
<methods>
<method name="Color8">
<method name="Color8" deprecated="Use [method Color.from_rgba8] instead.">
<return type="Color" />
<param index="0" name="r8" type="int" />
<param index="1" name="g8" type="int" />
Expand Down
11 changes: 7 additions & 4 deletions modules/gdscript/gdscript_utility_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,21 +315,22 @@ struct GDScriptUtilityFunctionsDefinitions {
}
}

#ifndef DISABLE_DEPRECATED
static inline void Color8(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
DEBUG_VALIDATE_ARG_COUNT(3, 4);
DEBUG_VALIDATE_ARG_TYPE(0, Variant::INT);
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);
}
#endif // DISABLE_DEPRECATED

static inline void print_debug(Variant *r_ret, const Variant **p_args, int p_arg_count, Callable::CallError &r_error) {
String s;
Expand Down Expand Up @@ -577,8 +578,10 @@ void GDScriptUtilityFunctions::register_functions() {
REGISTER_FUNC( load, false, RETCLS("Resource"), ARGS( ARG("path", STRING) ), false, varray( ));
REGISTER_FUNC( inst_to_dict, false, RET(DICTIONARY), ARGS( ARG("instance", OBJECT) ), false, varray( ));
REGISTER_FUNC( dict_to_inst, false, RET(OBJECT), ARGS( ARG("dictionary", DICTIONARY) ), false, varray( ));
#ifndef DISABLE_DEPRECATED
REGISTER_FUNC( Color8, true, RET(COLOR), ARGS( ARG("r8", INT), ARG("g8", INT),
ARG("b8", INT), ARG("a8", INT) ), false, varray( 255 ));
#endif // DISABLE_DEPRECATED
REGISTER_FUNC( print_debug, false, RET(NIL), NOARGS, true, varray( ));
REGISTER_FUNC( print_stack, false, RET(NIL), NOARGS, false, varray( ));
REGISTER_FUNC( get_stack, false, RET(ARRAY), NOARGS, false, varray( ));
Expand Down

0 comments on commit 4e48b19

Please sign in to comment.