aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/tui/input.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/nvim/tui/input.c')
-rw-r--r--src/nvim/tui/input.c25
1 files changed, 22 insertions, 3 deletions
diff --git a/src/nvim/tui/input.c b/src/nvim/tui/input.c
index e0dd4022ed..3eb88366d6 100644
--- a/src/nvim/tui/input.c
+++ b/src/nvim/tui/input.c
@@ -357,12 +357,28 @@ static bool handle_forced_escape(TermInput *input)
static void set_bg_deferred(void **argv)
{
char *bgvalue = argv[0];
- set_string_default("bg", bgvalue, false);
- if (!option_was_set("bg")) {
- set_option_value("bg", 0, bgvalue, 0);
+ if (starting) {
+ // Wait until after startup, so OptionSet is triggered.
+ loop_schedule(&main_loop, event_create(set_bg_deferred, 1, bgvalue));
+ return;
+ }
+ if (!option_was_set("bg") && !strequal((char *)p_bg, bgvalue)) {
+ // Value differs, apply it.
+ set_option_value("bg", 0L, bgvalue, 0);
+ reset_option_was_set("bg");
}
}
+// During startup, tui.c requests the background color (see `ext.get_bg`).
+//
+// Here in input.c, we watch for the terminal response `\e]11;COLOR\a`. If
+// COLOR matches `rgb:RRRR/GGGG/BBBB` where R, G, and B are hex digits, then
+// compute the luminance[1] of the RGB color and classify it as light/dark
+// accordingly. Note that the color components may have anywhere from one to
+// four hex digits, and require scaling accordingly as values out of 4, 8, 12,
+// or 16 bits.
+//
+// [1] https://en.wikipedia.org/wiki/Luma_%28video%29
static bool handle_background_color(TermInput *input)
{
size_t count = 0;
@@ -407,7 +423,10 @@ static bool handle_background_color(TermInput *input)
double b = (double)rgb[2] / (double)rgb_max[2];
double luminance = (0.299 * r) + (0.587 * g) + (0.114 * b); // CCIR 601
char *bgvalue = luminance < 0.5 ? "dark" : "light";
+ DLOG("bg response: %s", bgvalue);
loop_schedule(&main_loop, event_create(set_bg_deferred, 1, bgvalue));
+ } else {
+ DLOG("failed to parse bg response");
}
return true;
}