From 90df1d67cbc06f2742c11dad57ca940b9ed7c0d4 Mon Sep 17 00:00:00 2001 From: Aaron Franke Date: Thu, 18 Jul 2019 20:15:39 -0400 Subject: [PATCH] Color with alpha constructor --- core/color.h | 12 +++++++++++- core/variant.cpp | 5 ----- core/variant_call.cpp | 7 +++++++ doc/classes/Color.xml | 14 ++++++++++++++ .../mono/glue/GodotSharp/GodotSharp/Core/Color.cs | 10 +++++++++- 5 files changed, 41 insertions(+), 7 deletions(-) diff --git a/core/color.h b/core/color.h index d95e80f5dae..8b689fdde1e 100644 --- a/core/color.h +++ b/core/color.h @@ -205,7 +205,7 @@ struct Color { operator String() const; /** - * No construct parameters, r=0, g=0, b=0. a=255 + * No construct parameters, r=0, g=0, b=0. a=1 */ _FORCE_INLINE_ Color() { r = 0; @@ -223,6 +223,16 @@ struct Color { b = p_b; a = p_a; } + + /** + * Construct a Color from another Color, but with the specified alpha value. + */ + _FORCE_INLINE_ Color(const Color &p_c, float p_a) { + r = p_c.r; + g = p_c.g; + b = p_c.b; + a = p_a; + } }; bool Color::operator<(const Color &p_color) const { diff --git a/core/variant.cpp b/core/variant.cpp index b3611536b8a..a91876dd98d 100644 --- a/core/variant.cpp +++ b/core/variant.cpp @@ -102,11 +102,6 @@ String Variant::get_type_name(Variant::Type p_type) { return "Plane"; } break; - /* - case QUAT: { - - - } break;*/ case AABB: { return "AABB"; diff --git a/core/variant_call.cpp b/core/variant_call.cpp index 39bfd04a0b1..395e46d9127 100644 --- a/core/variant_call.cpp +++ b/core/variant_call.cpp @@ -1075,6 +1075,11 @@ struct _VariantCall { r_ret = Color::hex(*p_args[0]); } + static void Color_init5(Variant &r_ret, const Variant **p_args) { + + r_ret = Color(((Color)(*p_args[0])), *p_args[1]); + } + static void AABB_init1(Variant &r_ret, const Variant **p_args) { r_ret = ::AABB(*p_args[0], *p_args[1]); @@ -2209,6 +2214,8 @@ void register_variant_methods() { _VariantCall::add_constructor(_VariantCall::Color_init1, Variant::COLOR, "r", Variant::FLOAT, "g", Variant::FLOAT, "b", Variant::FLOAT, "a", Variant::FLOAT); _VariantCall::add_constructor(_VariantCall::Color_init2, Variant::COLOR, "r", Variant::FLOAT, "g", Variant::FLOAT, "b", Variant::FLOAT); + // init3 and init4 are the constructors for HTML hex strings and integers respectively which don't need binding here, so we skip to init5. + _VariantCall::add_constructor(_VariantCall::Color_init5, Variant::COLOR, "c", Variant::COLOR, "a", Variant::FLOAT); _VariantCall::add_constructor(_VariantCall::AABB_init1, Variant::AABB, "position", Variant::VECTOR3, "size", Variant::VECTOR3); diff --git a/doc/classes/Color.xml b/doc/classes/Color.xml index 1af5c875326..b35d4fb36a9 100644 --- a/doc/classes/Color.xml +++ b/doc/classes/Color.xml @@ -39,6 +39,20 @@ [/codeblock] + + + + + + + + + Constructs a color from an existing color, but with a custom alpha value. + [codeblock] + var red = Color(Color.red, 0.5) # 50% transparent red. + [/codeblock] + + diff --git a/modules/mono/glue/GodotSharp/GodotSharp/Core/Color.cs b/modules/mono/glue/GodotSharp/GodotSharp/Core/Color.cs index facaf746062..6030b72a444 100644 --- a/modules/mono/glue/GodotSharp/GodotSharp/Core/Color.cs +++ b/modules/mono/glue/GodotSharp/GodotSharp/Core/Color.cs @@ -420,7 +420,7 @@ namespace Godot return txt; } - // Constructors + // Constructors public Color(float r, float g, float b, float a = 1.0f) { this.r = r; @@ -429,6 +429,14 @@ namespace Godot this.a = a; } + public Color(Color c, float a = 1.0f) + { + r = c.r; + g = c.g; + b = c.b; + this.a = a; + } + public Color(uint rgba) { a = (rgba & 0xFF) / 255.0f;