Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 424ddd0

Browse files
HarmtHblueyed
authored andcommitted
tui: support rgba background detection (#10205)
Fixes #10159.
1 parent 6f27f5e commit 424ddd0

File tree

2 files changed

+107
-81
lines changed

2 files changed

+107
-81
lines changed

src/nvim/tui/input.c

Lines changed: 50 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -388,66 +388,75 @@ static void set_bg_deferred(void **argv)
388388
// During startup, tui.c requests the background color (see `ext.get_bg`).
389389
//
390390
// Here in input.c, we watch for the terminal response `\e]11;COLOR\a`. If
391-
// COLOR matches `rgb:RRRR/GGGG/BBBB` where R, G, and B are hex digits, then
392-
// compute the luminance[1] of the RGB color and classify it as light/dark
391+
// COLOR matches `rgb:RRRR/GGGG/BBBB/AAAA` where R, G, B, and A are hex digits,
392+
// then compute the luminance[1] of the RGB color and classify it as light/dark
393393
// accordingly. Note that the color components may have anywhere from one to
394394
// four hex digits, and require scaling accordingly as values out of 4, 8, 12,
395-
// or 16 bits.
395+
// or 16 bits. Also note the A(lpha) component is optional, and is parsed but
396+
// ignored in the calculations.
396397
//
397398
// [1] https://en.wikipedia.org/wiki/Luma_%28video%29
398399
static bool handle_background_color(TermInput *input)
399400
{
400401
size_t count = 0;
401402
size_t component = 0;
403+
size_t header_size = 0;
404+
size_t num_components = 0;
402405
uint16_t rgb[] = { 0, 0, 0 };
403406
uint16_t rgb_max[] = { 0, 0, 0 };
404407
bool eat_backslash = false;
405408
bool done = false;
406409
bool bad = false;
407410
if (rbuffer_size(input->read_stream.buffer) >= 9
408411
&& !rbuffer_cmp(input->read_stream.buffer, "\x1b]11;rgb:", 9)) {
409-
rbuffer_consumed(input->read_stream.buffer, 9);
410-
RBUFFER_EACH(input->read_stream.buffer, c, i) {
411-
count = i + 1;
412-
if (eat_backslash) {
413-
done = true;
414-
break;
415-
} else if (c == '\x07') {
416-
done = true;
417-
break;
418-
} else if (c == '\x1b') {
419-
eat_backslash = true;
420-
} else if (bad) {
421-
// ignore
422-
} else if (c == '/') {
423-
if (component < 3) {
424-
component++;
425-
}
426-
} else if (ascii_isxdigit(c)) {
427-
if (component < 3 && rgb_max[component] != 0xffff) {
428-
rgb_max[component] = (uint16_t)((rgb_max[component] << 4) | 0xf);
429-
rgb[component] = (uint16_t)((rgb[component] << 4) | hex2nr(c));
430-
}
431-
} else {
432-
bad = true;
412+
header_size = 9;
413+
num_components = 3;
414+
} else if (rbuffer_size(input->read_stream.buffer) >= 10
415+
&& !rbuffer_cmp(input->read_stream.buffer, "\x1b]11;rgba:", 10)) {
416+
header_size = 10;
417+
num_components = 4;
418+
} else {
419+
return false;
420+
}
421+
rbuffer_consumed(input->read_stream.buffer, header_size);
422+
RBUFFER_EACH(input->read_stream.buffer, c, i) {
423+
count = i + 1;
424+
if (eat_backslash) {
425+
done = true;
426+
break;
427+
} else if (c == '\x07') {
428+
done = true;
429+
break;
430+
} else if (c == '\x1b') {
431+
eat_backslash = true;
432+
} else if (bad) {
433+
// ignore
434+
} else if ((c == '/') && (++component < num_components)) {
435+
// work done in condition
436+
} else if (ascii_isxdigit(c)) {
437+
if (component < 3 && rgb_max[component] != 0xffff) {
438+
rgb_max[component] = (uint16_t)((rgb_max[component] << 4) | 0xf);
439+
rgb[component] = (uint16_t)((rgb[component] << 4) | hex2nr(c));
433440
}
434-
}
435-
rbuffer_consumed(input->read_stream.buffer, count);
436-
if (done && !bad && rgb_max[0] && rgb_max[1] && rgb_max[2]) {
437-
double r = (double)rgb[0] / (double)rgb_max[0];
438-
double g = (double)rgb[1] / (double)rgb_max[1];
439-
double b = (double)rgb[2] / (double)rgb_max[2];
440-
double luminance = (0.299 * r) + (0.587 * g) + (0.114 * b); // CCIR 601
441-
char *bgvalue = luminance < 0.5 ? "dark" : "light";
442-
DLOG("bg response: %s", bgvalue);
443-
loop_schedule_deferred(&main_loop,
444-
event_create(set_bg_deferred, 1, bgvalue));
445441
} else {
446-
DLOG("failed to parse bg response");
442+
bad = true;
447443
}
448-
return true;
449444
}
450-
return false;
445+
rbuffer_consumed(input->read_stream.buffer, count);
446+
if (done && !bad && rgb_max[0] && rgb_max[1] && rgb_max[2]) {
447+
double r = (double)rgb[0] / (double)rgb_max[0];
448+
double g = (double)rgb[1] / (double)rgb_max[1];
449+
double b = (double)rgb[2] / (double)rgb_max[2];
450+
double luminance = (0.299 * r) + (0.587 * g) + (0.114 * b); // CCIR 601
451+
char *bgvalue = luminance < 0.5 ? "dark" : "light";
452+
DLOG("bg response: %s", bgvalue);
453+
loop_schedule_deferred(&main_loop,
454+
event_create(set_bg_deferred, 1, bgvalue));
455+
} else {
456+
DLOG("failed to parse bg response");
457+
return false;
458+
}
459+
return true;
451460
}
452461

453462
static void tinput_read_cb(Stream *stream, RBuffer *buf, size_t count_,

test/functional/terminal/tui_spec.lua

Lines changed: 57 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -871,9 +871,9 @@ describe('TUI background color', function()
871871
screen:expect{any='did OptionSet, yay!'}
872872
end)
873873

874-
local function assert_bg(color, bg)
874+
local function assert_bg(colorspace, color, bg)
875875
it('handles '..color..' as '..bg, function()
876-
feed_data('\027]11;rgb:'..color..'\007')
876+
feed_data('\027]11;'..colorspace..':'..color..'\007')
877877
-- Retry until the terminal response is handled.
878878
retry(100, nil, function()
879879
feed_data(':echo &background\n')
@@ -893,42 +893,59 @@ describe('TUI background color', function()
893893
end)
894894
end
895895

896-
assert_bg('0000/0000/0000', 'dark')
897-
assert_bg('ffff/ffff/ffff', 'light')
898-
assert_bg('000/000/000', 'dark')
899-
assert_bg('fff/fff/fff', 'light')
900-
assert_bg('00/00/00', 'dark')
901-
assert_bg('ff/ff/ff', 'light')
902-
assert_bg('0/0/0', 'dark')
903-
assert_bg('f/f/f', 'light')
904-
905-
assert_bg('f/0/0', 'dark')
906-
assert_bg('0/f/0', 'light')
907-
assert_bg('0/0/f', 'dark')
908-
909-
assert_bg('1/1/1', 'dark')
910-
assert_bg('2/2/2', 'dark')
911-
assert_bg('3/3/3', 'dark')
912-
assert_bg('4/4/4', 'dark')
913-
assert_bg('5/5/5', 'dark')
914-
assert_bg('6/6/6', 'dark')
915-
assert_bg('7/7/7', 'dark')
916-
assert_bg('8/8/8', 'light')
917-
assert_bg('9/9/9', 'light')
918-
assert_bg('a/a/a', 'light')
919-
assert_bg('b/b/b', 'light')
920-
assert_bg('c/c/c', 'light')
921-
assert_bg('d/d/d', 'light')
922-
assert_bg('e/e/e', 'light')
923-
924-
assert_bg('0/e/0', 'light')
925-
assert_bg('0/d/0', 'light')
926-
assert_bg('0/c/0', 'dark')
927-
assert_bg('0/b/0', 'dark')
928-
929-
assert_bg('f/0/f', 'dark')
930-
assert_bg('f/1/f', 'dark')
931-
assert_bg('f/2/f', 'dark')
932-
assert_bg('f/3/f', 'light')
933-
assert_bg('f/4/f', 'light')
896+
assert_bg('rgb', '0000/0000/0000', 'dark')
897+
assert_bg('rgb', 'ffff/ffff/ffff', 'light')
898+
assert_bg('rgb', '000/000/000', 'dark')
899+
assert_bg('rgb', 'fff/fff/fff', 'light')
900+
assert_bg('rgb', '00/00/00', 'dark')
901+
assert_bg('rgb', 'ff/ff/ff', 'light')
902+
assert_bg('rgb', '0/0/0', 'dark')
903+
assert_bg('rgb', 'f/f/f', 'light')
904+
905+
assert_bg('rgb', 'f/0/0', 'dark')
906+
assert_bg('rgb', '0/f/0', 'light')
907+
assert_bg('rgb', '0/0/f', 'dark')
908+
909+
assert_bg('rgb', '1/1/1', 'dark')
910+
assert_bg('rgb', '2/2/2', 'dark')
911+
assert_bg('rgb', '3/3/3', 'dark')
912+
assert_bg('rgb', '4/4/4', 'dark')
913+
assert_bg('rgb', '5/5/5', 'dark')
914+
assert_bg('rgb', '6/6/6', 'dark')
915+
assert_bg('rgb', '7/7/7', 'dark')
916+
assert_bg('rgb', '8/8/8', 'light')
917+
assert_bg('rgb', '9/9/9', 'light')
918+
assert_bg('rgb', 'a/a/a', 'light')
919+
assert_bg('rgb', 'b/b/b', 'light')
920+
assert_bg('rgb', 'c/c/c', 'light')
921+
assert_bg('rgb', 'd/d/d', 'light')
922+
assert_bg('rgb', 'e/e/e', 'light')
923+
924+
assert_bg('rgb', '0/e/0', 'light')
925+
assert_bg('rgb', '0/d/0', 'light')
926+
assert_bg('rgb', '0/c/0', 'dark')
927+
assert_bg('rgb', '0/b/0', 'dark')
928+
929+
assert_bg('rgb', 'f/0/f', 'dark')
930+
assert_bg('rgb', 'f/1/f', 'dark')
931+
assert_bg('rgb', 'f/2/f', 'dark')
932+
assert_bg('rgb', 'f/3/f', 'light')
933+
assert_bg('rgb', 'f/4/f', 'light')
934+
935+
assert_bg('rgba', '0000/0000/0000/0000', 'dark')
936+
assert_bg('rgba', '0000/0000/0000/ffff', 'dark')
937+
assert_bg('rgba', 'ffff/ffff/ffff/0000', 'light')
938+
assert_bg('rgba', 'ffff/ffff/ffff/ffff', 'light')
939+
assert_bg('rgba', '000/000/000/000', 'dark')
940+
assert_bg('rgba', '000/000/000/fff', 'dark')
941+
assert_bg('rgba', 'fff/fff/fff/000', 'light')
942+
assert_bg('rgba', 'fff/fff/fff/fff', 'light')
943+
assert_bg('rgba', '00/00/00/00', 'dark')
944+
assert_bg('rgba', '00/00/00/ff', 'dark')
945+
assert_bg('rgba', 'ff/ff/ff/00', 'light')
946+
assert_bg('rgba', 'ff/ff/ff/ff', 'light')
947+
assert_bg('rgba', '0/0/0/0', 'dark')
948+
assert_bg('rgba', '0/0/0/f', 'dark')
949+
assert_bg('rgba', 'f/f/f/0', 'light')
950+
assert_bg('rgba', 'f/f/f/f', 'light')
934951
end)

0 commit comments

Comments
 (0)