Skip to content

Commit

Permalink
Improve colors' appearance in the editor's print_rich() output
Browse files Browse the repository at this point in the history
This matches most terminals' behavior which don't use "pure" ANSI
colors anymore, but use modern color palettes instead.
  • Loading branch information
Calinou committed Jan 3, 2024
1 parent fbaab3c commit cabe476
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
22 changes: 20 additions & 2 deletions editor/editor_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,14 @@ void EditorLog::_update_theme() {
theme_cache.warning_color = get_theme_color(SNAME("warning_color"), EditorStringName(Editor));
theme_cache.warning_icon = get_editor_theme_icon(SNAME("Warning"));
theme_cache.message_color = get_theme_color(SNAME("font_color"), EditorStringName(Editor)) * Color(1, 1, 1, 0.6);

// Used for `print_rich()` coloring.
theme_cache.red_color = get_theme_color(SNAME("red_color"), EditorStringName(Editor));
theme_cache.green_color = get_theme_color(SNAME("green_color"), EditorStringName(Editor));
theme_cache.yellow_color = get_theme_color(SNAME("yellow_color"), EditorStringName(Editor));
theme_cache.blue_color = get_theme_color(SNAME("blue_color"), EditorStringName(Editor));
theme_cache.magenta_color = get_theme_color(SNAME("magenta_color"), EditorStringName(Editor));
theme_cache.cyan_color = get_theme_color(SNAME("cyan_color"), EditorStringName(Editor));
}

void EditorLog::_notification(int p_what) {
Expand Down Expand Up @@ -235,7 +243,7 @@ void EditorLog::_process_message(const String &p_msg, MessageType p_type, bool p

void EditorLog::add_message(const String &p_msg, MessageType p_type) {
// Make text split by new lines their own message.
// See #41321 for reasoning. At time of writing, multiple print()'s in running projects
// See GH-41321 for reasoning. At time of writing, multiple print()'s in running projects
// get grouped together and sent to the editor log as one message. This can mess with the
// search functionality (see the comments on the PR above for more details). This behavior
// also matches that of other IDE's.
Expand Down Expand Up @@ -338,7 +346,17 @@ void EditorLog::_add_log_line(LogMessage &p_message, bool p_replace_previous) {
}

if (p_message.type == MSG_TYPE_STD_RICH) {
log->append_text(p_message.text);
// Replace basic ANSI colors with more pleasant-looking colors, similar to what most terminals do out of the box.
// This also allows the printed colors to adapt to the current editor theme at the time of printing.
// Only replace the end of the color tag so that colors in `bgcolor` and `fgcolor` are replaced too.
log->append_text(
p_message.text
.replace("color=red]", "color=" + theme_cache.red_color.to_html() + "]")
.replace("color=green]", "color=" + theme_cache.green_color.to_html() + "]")
.replace("color=yellow]", "color=" + theme_cache.yellow_color.to_html() + "]")
.replace("color=blue]", "color=" + theme_cache.blue_color.to_html() + "]")
.replace("color=magenta]", "color=" + theme_cache.magenta_color.to_html() + "]")
.replace("color=cyan]", "color=" + theme_cache.cyan_color.to_html() + "]"));
} else {
log->add_text(p_message.text);
}
Expand Down
7 changes: 7 additions & 0 deletions editor/editor_log.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,13 @@ class EditorLog : public HBoxContainer {
Ref<Texture2D> warning_icon;

Color message_color;

Color red_color;
Color green_color;
Color yellow_color;
Color blue_color;
Color magenta_color;
Color cyan_color;
} theme_cache;

// Encapsulates all data and functionality regarding filters.
Expand Down
16 changes: 16 additions & 0 deletions editor/editor_themes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -719,6 +719,9 @@ Ref<Theme> create_editor_theme(const Ref<Theme> p_theme) {
Color success_color = Color(0.45, 0.95, 0.5);
Color warning_color = Color(1, 0.87, 0.4);
Color error_color = Color(1, 0.47, 0.42);
Color blue_color = Color(0.44, 0.73, 0.98);
Color magenta_color = Color(0.98, 0.44, 0.98);
Color cyan_color = Color(0.37, 0.98, 0.98);
Color property_color = font_color.lerp(Color(0.5, 0.5, 0.5), 0.5);
Color readonly_color = property_color.lerp(dark_theme ? Color(0, 0, 0) : Color(1, 1, 1), 0.25);
Color readonly_warning_color = error_color.lerp(dark_theme ? Color(0, 0, 0) : Color(1, 1, 1), 0.25);
Expand All @@ -728,6 +731,9 @@ Ref<Theme> create_editor_theme(const Ref<Theme> p_theme) {
success_color = success_color.lerp(mono_color, 0.35);
warning_color = warning_color.lerp(mono_color, 0.35);
error_color = error_color.lerp(mono_color, 0.25);
blue_color = blue_color.lerp(mono_color, 0.35);
magenta_color = magenta_color.lerp(mono_color, 0.35);
cyan_color = cyan_color.lerp(mono_color, 0.4);
}

theme->set_color("success_color", EditorStringName(Editor), success_color);
Expand All @@ -736,6 +742,16 @@ Ref<Theme> create_editor_theme(const Ref<Theme> p_theme) {
theme->set_color("property_color", EditorStringName(Editor), property_color);
theme->set_color("readonly_color", EditorStringName(Editor), readonly_color);

// Used to color `print_rich()` usage with basic RichTextLabel colors in the editor log.
// NOTE: These color names are non-semantic. Only use these generic colors if no specific
// color suits your use case (such as `error_color`).
theme->set_color("red_color", "Editor", error_color);
theme->set_color("green_color", "Editor", success_color);
theme->set_color("yellow_color", "Editor", warning_color);
theme->set_color("blue_color", "Editor", blue_color);
theme->set_color("magenta_color", "Editor", magenta_color);
theme->set_color("cyan_color", "Editor", cyan_color);

if (!dark_theme) {
theme->set_color("highend_color", EditorStringName(Editor), Color::hex(0xad1128ff));
} else {
Expand Down

0 comments on commit cabe476

Please sign in to comment.