Przeglądaj źródła

Fix font preview text color on light background

Haoyu Qiu 3 lat temu
rodzic
commit
d99a313fd3

+ 4 - 0
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;
 

+ 2 - 0
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());

+ 7 - 0
doc/classes/Color.xml

@@ -111,6 +111,13 @@
 				[/codeblock]
 			</description>
 		</method>
+		<method name="get_luminance">
+			<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="gray">
 			<return type="float" />
 			<description>

+ 1 - 1
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() {

+ 3 - 1
editor/plugins/editor_preview_plugins.cpp

@@ -867,7 +867,9 @@ Ref<Texture> EditorFontPreviewPlugin::generate_from_path(const String &p_path, c
 
 	Ref<Font> 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