From 16979927bacb617ef6c8cccb013ee6ff9eb93f78 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Ekl=C3=B6f?= Date: Sun, 21 Jul 2019 11:32:35 +0200 Subject: [PATCH] csi: pick 16 first 256-colors directly from the term struct The 16 first entries in the 256-color array are the regular and bright colors. However, since they can be changed (eventually) at runtime, we can't statically initialize the 256-color array. Instead, pick the regular+bright colors directly from the terminal struct. I.e. the first 16 entries of the 256-color array are now unused. --- csi.c | 21 +++++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/csi.c b/csi.c index a34f8c42..4d1921bd 100644 --- a/csi.c +++ b/csi.c @@ -23,10 +23,12 @@ static uint32_t colors256[256]; static void __attribute__((constructor)) initialize_colors256(void) { +#if 0 /* pick colors from term struct instead, since they can be changed runtime */ for (size_t i = 0; i < 8; i++) colors256[i] = colors_regular[i]; for (size_t i = 0; i < 8; i++) colors256[8 + i] = colors_bright[i]; +#endif for (size_t r = 0; r < 6; r++) { for (size_t g = 0; g < 6; g++) { @@ -132,7 +134,14 @@ csi_sgr(struct terminal *term) term->vt.params.v[i + 1].value == 5) { uint8_t idx = term->vt.params.v[i + 2].value; - term->vt.attrs.foreground = 1 << 31 | colors256[idx]; + uint32_t color; + if (idx < 8) + color = term->colors.regular[idx]; + else if (idx < 16) + color = term->colors.bright[idx - 8]; + else + color = colors256[idx]; + term->vt.attrs.foreground = 1 << 31 | color; i += 2; } @@ -194,7 +203,15 @@ csi_sgr(struct terminal *term) term->vt.params.v[i + 1].value == 5) { uint8_t idx = term->vt.params.v[i + 2].value; - term->vt.attrs.background = 1 << 31 | colors256[idx]; + uint32_t color; + + if (idx < 8) + color = term->colors.regular[idx]; + else if (idx < 16) + color = term->colors.bright[idx - 8]; + else + color = colors256[idx]; + term->vt.attrs.background = 1 << 31 | color; i += 2; }