diff options
author | Rémi Verschelde <remi@verschelde.fr> | 2021-02-02 09:49:35 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-02-02 09:49:35 +0100 |
commit | 122a27523da45d68b98e53dc5d6ca2b95f908ad1 (patch) | |
tree | 861d42cbe83625eb3771c6d43c73599b2c3a0a71 /core | |
parent | b24c24f64b4ac9c30dc5a9e097163cbc64939645 (diff) | |
parent | f55445079ad8e84cd337063b8cbac6eaf06d5592 (diff) |
Merge pull request #45030 from aaronfranke/color-str-construct
Replace ColorN and from HTML with a string constructor
Diffstat (limited to 'core')
-rw-r--r-- | core/math/color.cpp | 2 | ||||
-rw-r--r-- | core/math/color.h | 13 | ||||
-rw-r--r-- | core/variant/variant.cpp | 2 | ||||
-rw-r--r-- | core/variant/variant_construct.cpp | 2 |
4 files changed, 18 insertions, 1 deletions
diff --git a/core/math/color.cpp b/core/math/color.cpp index 0398d43838..e1b45cac9c 100644 --- a/core/math/color.cpp +++ b/core/math/color.cpp @@ -408,6 +408,8 @@ Color Color::get_named_color(int p_idx) { return named_colors[p_idx].color; } +// For a version that errors on invalid values instead of returning +// a default color, use the Color(String) constructor instead. Color Color::from_string(const String &p_string, const Color &p_default) { if (html_is_valid(p_string)) { return html(p_string); diff --git a/core/math/color.h b/core/math/color.h index d3b27a9c65..5eb8b1119a 100644 --- a/core/math/color.h +++ b/core/math/color.h @@ -241,6 +241,19 @@ struct Color { b = p_c.b; a = p_a; } + + Color(const String &p_code) { + if (html_is_valid(p_code)) { + *this = html(p_code); + } else { + *this = named(p_code); + } + } + + Color(const String &p_code, float p_a) { + *this = Color(p_code); + a = p_a; + } }; bool Color::operator<(const Color &p_color) const { diff --git a/core/variant/variant.cpp b/core/variant/variant.cpp index 7824776fdb..015cee09a7 100644 --- a/core/variant/variant.cpp +++ b/core/variant/variant.cpp @@ -2023,7 +2023,7 @@ Variant::operator Color() const { if (type == COLOR) { return *reinterpret_cast<const Color *>(_data._mem); } else if (type == STRING) { - return Color::html(operator String()); + return Color(operator String()); } else if (type == INT) { return Color::hex(operator int()); } else { diff --git a/core/variant/variant_construct.cpp b/core/variant/variant_construct.cpp index 9835734583..52f9f6060e 100644 --- a/core/variant/variant_construct.cpp +++ b/core/variant/variant_construct.cpp @@ -688,6 +688,8 @@ void Variant::_register_variant_constructors() { add_constructor<VariantConstructor<Color, Color, double>>(sarray("from", "alpha")); add_constructor<VariantConstructor<Color, double, double, double>>(sarray("r", "g", "b")); add_constructor<VariantConstructor<Color, double, double, double, double>>(sarray("r", "g", "b", "a")); + add_constructor<VariantConstructor<Color, String>>(sarray("code")); + add_constructor<VariantConstructor<Color, String, double>>(sarray("code", "alpha")); add_constructor<VariantConstructNoArgs<StringName>>(sarray()); add_constructor<VariantConstructor<StringName, StringName>>(sarray("from")); |