diff --git a/core/color.h b/core/color.h
index 8455b7c797c..9c83210048e 100644
--- a/core/color.h
+++ b/core/color.h
@@ -91,6 +91,10 @@ struct Color {
Color inverted() const;
Color contrasted() const;
+ _FORCE_INLINE_ float get_luminance() const {
+ return 0.2126 * r + 0.7152 * g + 0.0722 * b;
+ }
+
_FORCE_INLINE_ Color linear_interpolate(const Color &p_to, float p_weight) const {
Color res = *this;
diff --git a/core/variant_call.cpp b/core/variant_call.cpp
index c0f6da47ecf..24e16e13361 100644
--- a/core/variant_call.cpp
+++ b/core/variant_call.cpp
@@ -521,6 +521,7 @@ struct _VariantCall {
VCALL_LOCALMEM0R(Color, to_abgr64);
VCALL_LOCALMEM0R(Color, to_rgba64);
VCALL_LOCALMEM0R(Color, gray);
+ VCALL_LOCALMEM0R(Color, get_luminance);
VCALL_LOCALMEM0R(Color, inverted);
VCALL_LOCALMEM0R(Color, contrasted);
VCALL_LOCALMEM2R(Color, linear_interpolate);
@@ -1849,6 +1850,7 @@ void register_variant_methods() {
ADDFUNC0R(COLOR, INT, Color, to_abgr64, varray());
ADDFUNC0R(COLOR, INT, Color, to_rgba64, varray());
ADDFUNC0R(COLOR, REAL, Color, gray, varray());
+ ADDFUNC0R(COLOR, REAL, Color, get_luminance, varray());
ADDFUNC0R(COLOR, COLOR, Color, inverted, varray());
ADDFUNC0R(COLOR, COLOR, Color, contrasted, varray());
ADDFUNC2R(COLOR, COLOR, Color, linear_interpolate, COLOR, "to", REAL, "weight", varray());
diff --git a/doc/classes/Color.xml b/doc/classes/Color.xml
index 7fb98b17541..5fceb9f7b9f 100644
--- a/doc/classes/Color.xml
+++ b/doc/classes/Color.xml
@@ -111,6 +111,13 @@
[/codeblock]
+
+
+
+ Returns the luminance of the color in the [code][0.0, 1.0][/code] range.
+ This is useful when determining light or dark color. Colors with a luminance smaller than 0.5 can be generally considered dark.
+
+
diff --git a/editor/editor_settings.cpp b/editor/editor_settings.cpp
index 67145002b86..e22a1585c6c 100644
--- a/editor/editor_settings.cpp
+++ b/editor/editor_settings.cpp
@@ -1296,7 +1296,7 @@ bool EditorSettings::is_dark_theme() {
int LIGHT_COLOR = 2;
Color base_color = get("interface/theme/base_color");
int icon_font_color_setting = get("interface/theme/icon_and_font_color");
- return (icon_font_color_setting == AUTO_COLOR && ((base_color.r + base_color.g + base_color.b) / 3.0) < 0.5) || icon_font_color_setting == LIGHT_COLOR;
+ return (icon_font_color_setting == AUTO_COLOR && base_color.get_luminance() < 0.5) || icon_font_color_setting == LIGHT_COLOR;
}
void EditorSettings::list_text_editor_themes() {
diff --git a/editor/plugins/editor_preview_plugins.cpp b/editor/plugins/editor_preview_plugins.cpp
index e1f4facff0b..0c061091a24 100644
--- a/editor/plugins/editor_preview_plugins.cpp
+++ b/editor/plugins/editor_preview_plugins.cpp
@@ -867,7 +867,9 @@ Ref EditorFontPreviewPlugin::generate_from_path(const String &p_path, c
Ref font = sampled_font;
- font->draw(canvas_item, pos, sampled_text);
+ const Color c = GLOBAL_GET("rendering/environment/default_clear_color");
+ const float fg = c.get_luminance() < 0.5 ? 1.0 : 0.0;
+ font->draw(canvas_item, pos, sampled_text, Color(fg, fg, fg));
preview_done.clear();
VS::get_singleton()->viewport_set_update_mode(viewport, VS::VIEWPORT_UPDATE_ONCE); //once used for capture