summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <remi@verschelde.fr>2021-01-08 09:03:55 +0100
committerGitHub <noreply@github.com>2021-01-08 09:03:55 +0100
commit9349a5507f56024131c278e29080a891e54f5933 (patch)
tree4367308439643cbe1193964285faa44d14af5eb9 /core
parent830e6296b73ceb734d94b3dcef156d5b21e6d87b (diff)
parent01d0addf565df5afb4596b5f4f13bb5d3f3ee26d (diff)
Merge pull request #35505 from dalexeev/rtl_colors
Unified named colors in RichTextLabel
Diffstat (limited to 'core')
-rw-r--r--core/math/color.cpp33
-rw-r--r--core/math/color.h3
-rw-r--r--core/math/color_names.inc2
3 files changed, 33 insertions, 5 deletions
diff --git a/core/math/color.cpp b/core/math/color.cpp
index 4d58c8c84a..588aedf821 100644
--- a/core/math/color.cpp
+++ b/core/math/color.cpp
@@ -355,6 +355,23 @@ bool Color::html_is_valid(const String &p_color) {
}
Color Color::named(const String &p_name) {
+ int idx = find_named_color(p_name);
+ if (idx == -1) {
+ ERR_FAIL_V_MSG(Color(), "Invalid color name: " + p_name + ".");
+ return Color();
+ }
+ return get_named_color(idx);
+}
+
+Color Color::named(const String &p_name, const Color &p_default) {
+ int idx = find_named_color(p_name);
+ if (idx == -1) {
+ return p_default;
+ }
+ return get_named_color(idx);
+}
+
+int Color::find_named_color(const String &p_name) {
String name = p_name;
// Normalize name
name = name.replace(" ", "");
@@ -367,14 +384,12 @@ Color Color::named(const String &p_name) {
int idx = 0;
while (named_colors[idx].name != nullptr) {
if (name == named_colors[idx].name) {
- return named_colors[idx].color;
+ return idx;
}
idx++;
}
- ERR_FAIL_V_MSG(Color(), "Invalid color name: " + p_name + ".");
-
- return Color();
+ return -1;
}
int Color::get_named_color_count() {
@@ -384,13 +399,23 @@ int Color::get_named_color_count() {
}
return idx;
}
+
String Color::get_named_color_name(int p_idx) {
return named_colors[p_idx].name;
}
+
Color Color::get_named_color(int p_idx) {
return named_colors[p_idx].color;
}
+Color Color::from_string(const String &p_string, const Color &p_default) {
+ if (html_is_valid(p_string)) {
+ return html(p_string);
+ } else {
+ return named(p_string, p_default);
+ }
+}
+
String _to_hex(float p_val) {
int v = Math::round(p_val * 255);
v = CLAMP(v, 0, 255);
diff --git a/core/math/color.h b/core/math/color.h
index c7b5ceca3d..779f770761 100644
--- a/core/math/color.h
+++ b/core/math/color.h
@@ -182,9 +182,12 @@ struct Color {
static Color html(const String &p_rgba);
static bool html_is_valid(const String &p_color);
static Color named(const String &p_name);
+ static Color named(const String &p_name, const Color &p_default);
+ static int find_named_color(const String &p_name);
static int get_named_color_count();
static String get_named_color_name(int p_idx);
static Color get_named_color(int p_idx);
+ static Color from_string(const String &p_string, const Color &p_default);
String to_html(bool p_alpha = true) const;
Color from_hsv(float p_h, float p_s, float p_v, float p_a) const;
static Color from_rgbe9995(uint32_t p_rgbe);
diff --git a/core/math/color_names.inc b/core/math/color_names.inc
index 523c7e3c59..e5b935ea9c 100644
--- a/core/math/color_names.inc
+++ b/core/math/color_names.inc
@@ -156,5 +156,5 @@ static NamedColor named_colors[] = {
{ "whitesmoke", Color(0.96, 0.96, 0.96) },
{ "yellow", Color(1.00, 1.00, 0.00) },
{ "yellowgreen", Color(0.60, 0.80, 0.20) },
- { nullptr, Color(0.60, 0.80, 0.20) },
+ { nullptr, Color() },
};