Merge pull request #35505 from dalexeev/rtl_colors

Unified named colors in RichTextLabel
This commit is contained in:
Rémi Verschelde 2021-01-08 09:03:55 +01:00 committed by GitHub
commit 9349a5507f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 36 additions and 89 deletions

View file

@ -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);

View file

@ -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);

View file

@ -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() },
};