summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRĂ©mi Verschelde <remi@verschelde.fr>2021-12-17 19:28:47 +0100
committerGitHub <noreply@github.com>2021-12-17 19:28:47 +0100
commit33bc761c7397b21c30a49d3f6a40b505bc6353aa (patch)
tree1ee96dcdcdd93ac10be1c1649a86f7cdfa428e9d
parent52217244f4008367e441944c578f59506b914f8f (diff)
parent1fb59d13c2e32b9543195601149071ff4b2887d4 (diff)
Merge pull request #55976 from timothyqiu/font-preview-fg
-rw-r--r--core/math/color.h4
-rw-r--r--core/variant/variant_call.cpp1
-rw-r--r--doc/classes/Color.xml7
-rw-r--r--editor/editor_settings.cpp2
-rw-r--r--editor/plugins/editor_preview_plugins.cpp4
5 files changed, 16 insertions, 2 deletions
diff --git a/core/math/color.h b/core/math/color.h
index ffd0fd8f6e..815dd98d96 100644
--- a/core/math/color.h
+++ b/core/math/color.h
@@ -94,6 +94,10 @@ struct Color {
void invert();
Color inverted() const;
+ _FORCE_INLINE_ float get_luminance() const {
+ return 0.2126 * r + 0.7152 * g + 0.0722 * b;
+ }
+
_FORCE_INLINE_ Color lerp(const Color &p_to, float p_weight) const {
Color res = *this;
diff --git a/core/variant/variant_call.cpp b/core/variant/variant_call.cpp
index 51b9119933..d2bda34818 100644
--- a/core/variant/variant_call.cpp
+++ b/core/variant/variant_call.cpp
@@ -1644,6 +1644,7 @@ static void _register_variant_builtin_methods() {
bind_method(Color, lightened, sarray("amount"), varray());
bind_method(Color, darkened, sarray("amount"), varray());
bind_method(Color, blend, sarray("over"), varray());
+ bind_method(Color, get_luminance, sarray(), varray());
bind_method(Color, is_equal_approx, sarray("to"), varray());
diff --git a/doc/classes/Color.xml b/doc/classes/Color.xml
index 22fb853b40..21d17969e2 100644
--- a/doc/classes/Color.xml
+++ b/doc/classes/Color.xml
@@ -178,6 +178,13 @@
<description>
</description>
</method>
+ <method name="get_luminance" qualifiers="const">
+ <return type="float" />
+ <description>
+ 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.
+ </description>
+ </method>
<method name="get_named_color" qualifiers="static">
<return type="Color" />
<argument index="0" name="idx" type="int" />
diff --git a/editor/editor_settings.cpp b/editor/editor_settings.cpp
index 68ac122e63..1ac1d6f048 100644
--- a/editor/editor_settings.cpp
+++ b/editor/editor_settings.cpp
@@ -1190,7 +1190,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 3b85fad345..ed1194efce 100644
--- a/editor/plugins/editor_preview_plugins.cpp
+++ b/editor/plugins/editor_preview_plugins.cpp
@@ -851,7 +851,9 @@ Ref<Texture2D> EditorFontPreviewPlugin::generate_from_path(const String &p_path,
Ref<Font> font = sampled_font;
- font->draw_string(canvas_item, pos, sample, HORIZONTAL_ALIGNMENT_LEFT, -1.f, 50, Color(1, 1, 1));
+ const Color c = GLOBAL_GET("rendering/environment/defaults/default_clear_color");
+ const float fg = c.get_luminance() < 0.5 ? 1.0 : 0.0;
+ font->draw_string(canvas_item, pos, sample, HORIZONTAL_ALIGNMENT_LEFT, -1.f, 50, Color(fg, fg, fg));
RS::get_singleton()->connect(SNAME("frame_pre_draw"), callable_mp(const_cast<EditorFontPreviewPlugin *>(this), &EditorFontPreviewPlugin::_generate_frame_started), Vector<Variant>(), Object::CONNECT_ONESHOT);