diff options
author | Raheman Vaiya <r.vaiya@gmail.com> | 2021-12-26 18:57:04 +0100 |
---|---|---|
committer | sinanmohd <pcmsinan@gmail.com> | 2023-01-12 08:21:55 +0530 |
commit | bb45edc83d12396ff754d4687904c65bf93ec3f0 (patch) | |
tree | b496a5accd5510c1fa35d93f979bf53456e63549 | |
parent | f3d1a0e667934251184a8f41bd465045f9f68816 (diff) |
Add support for OSC color sequences
-rw-r--r-- | config.def.h | 49 | ||||
-rw-r--r-- | st.c | 80 | ||||
-rw-r--r-- | st.h | 3 | ||||
-rw-r--r-- | x.c | 13 |
4 files changed, 121 insertions, 24 deletions
diff --git a/config.def.h b/config.def.h index be1b2e3..7c68baa 100644 --- a/config.def.h +++ b/config.def.h @@ -105,29 +105,32 @@ float alpha = 0.8; /* Terminal colors (16 first used in escape sequence) */ static const char *colorname[] = { - /* 8 normal colors */ - [0] = "#22212f", /* black */ - [1] = "#ff5555", /* red */ - [2] = "#50fa7b", /* green */ - [3] = "#f1fa8c", /* yellow */ - [4] = "#6071a9", /* blue */ - [5] = "#ff79c6", /* magenta */ - [6] = "#8cd5df", /* cyan */ - [7] = "#ced3e2", /* white */ - - /* 8 bright colors */ - [8] = "#6071a9", /* black */ - [9] = "#ff5555", /* red */ - [10] = "#50fa7b", /* green */ - [11] = "#f1fa8c", /* yellow */ - [12] = "#6071a9", /* blue */ - [13] = "#ff79c6", /* magenta */ - [14] = "#8cd5df", /* cyan */ - [15] = "#ced3e2", /* white */ - - /* special colors */ - [256] = "#22212f", /* background */ - [257] = "#ced3e2", /* foreground */ + /* 8 normal colors */ + [0] = "#22212f", /* black */ + [1] = "#ff5555", /* red */ + [2] = "#50fa7b", /* green */ + [3] = "#f1fa8c", /* yellow */ + [4] = "#6071a9", /* blue */ + [5] = "#ff79c6", /* magenta */ + [6] = "#8cd5df", /* cyan */ + [7] = "#ced3e2", /* white */ + + /* 8 bright colors */ + [8] = "#6071a9", /* black */ + [9] = "#ff5555", /* red */ + [10] = "#50fa7b", /* green */ + [11] = "#f1fa8c", /* yellow */ + [12] = "#6071a9", /* blue */ + [13] = "#ff79c6", /* magenta */ + [14] = "#8cd5df", /* cyan */ + [15] = "#ced3e2", /* white */ + + /* more colors can be added after 255 to use with DefaultXX */ + /* special colors */ + [256] = "#22212f", /* background */ + [257] = "#ced3e2", /* foreground */ + "gray90", /* default foreground colour */ + "black", /* default background colour */ }; /* @@ -1905,6 +1905,42 @@ csireset(void) } void +osc4_color_response(int num) +{ + int n; + char buf[32]; + unsigned char r, g, b; + + if (xgetcolor(num, &r, &g, &b)) { + fprintf(stderr, "erresc: failed to fetch osc4 color %d\n", num); + return; + } + + n = snprintf(buf, sizeof buf, "\033]4;%d;rgb:%02x%02x/%02x%02x/%02x%02x\007", + num, r, r, g, g, b, b); + + ttywrite(buf, n, 1); +} + +void +osc_color_response(int index, int num) +{ + int n; + char buf[32]; + unsigned char r, g, b; + + if (xgetcolor(index, &r, &g, &b)) { + fprintf(stderr, "erresc: failed to fetch osc color %d\n", index); + return; + } + + n = snprintf(buf, sizeof buf, "\033]%d;rgb:%02x%02x/%02x%02x/%02x%02x\007", + num, r, r, g, g, b, b); + + ttywrite(buf, n, 1); +} + +void strhandle(void) { char *p = NULL, *dec; @@ -1942,6 +1978,45 @@ strhandle(void) } } return; + case 10: + if (narg < 2) + break; + + p = strescseq.args[1]; + + if (!strcmp(p, "?")) + osc_color_response(defaultfg, 10); + else if (xsetcolorname(defaultfg, p)) + fprintf(stderr, "erresc: invalid foreground color: %s\n", p); + else + redraw(); + break; + case 11: + if (narg < 2) + break; + + p = strescseq.args[1]; + + if (!strcmp(p, "?")) + osc_color_response(defaultbg, 11); + else if (xsetcolorname(defaultbg, p)) + fprintf(stderr, "erresc: invalid background color: %s\n", p); + else + redraw(); + break; + case 12: + if (narg < 2) + break; + + p = strescseq.args[1]; + + if (!strcmp(p, "?")) + osc_color_response(defaultcs, 12); + else if (xsetcolorname(defaultcs, p)) + fprintf(stderr, "erresc: invalid cursor color: %s\n", p); + else + redraw(); + break; case 4: /* color set */ if (narg < 3) break; @@ -1949,7 +2024,10 @@ strhandle(void) /* FALLTHROUGH */ case 104: /* color reset, here p = NULL */ j = (narg > 1) ? atoi(strescseq.args[1]) : -1; - if (xsetcolorname(j, p)) { + + if (!strcmp(p, "?")) + osc4_color_response(j); + else if (xsetcolorname(j, p)) { if (par == 104 && narg <= 1) return; /* color reset without parameter */ fprintf(stderr, "erresc: invalid color j=%d, p=%s\n", @@ -113,6 +113,8 @@ void *xmalloc(size_t); void *xrealloc(void *, size_t); char *xstrdup(const char *); +int xgetcolor(int x, unsigned char *r, unsigned char *g, unsigned char *b); + /* config.h globals */ extern char *utmp; extern char *scroll; @@ -126,3 +128,4 @@ extern unsigned int tabspaces; extern unsigned int defaultfg; extern unsigned int defaultbg; extern float alpha; +extern unsigned int defaultcs; @@ -830,6 +830,19 @@ xloadcols(void) } int +xgetcolor(int x, unsigned char *r, unsigned char *g, unsigned char *b) +{ + if (!BETWEEN(x, 0, dc.collen)) + return 1; + + *r = dc.col[x].color.red >> 8; + *g = dc.col[x].color.green >> 8; + *b = dc.col[x].color.blue >> 8; + + return 0; +} + +int xsetcolorname(int x, const char *name) { Color ncolor; |