summaryrefslogtreecommitdiff
path: root/usr.bin/tmux
diff options
context:
space:
mode:
authorNicholas Marriott <nicm@cvs.openbsd.org>2015-06-05 22:33:40 +0000
committerNicholas Marriott <nicm@cvs.openbsd.org>2015-06-05 22:33:40 +0000
commitcb7451af2d1c7a38c406d9ba6af36a7733727112 (patch)
treed1e4f4e95fd89f6e032a3ad157c85fbc9c4909d4 /usr.bin/tmux
parent1664eb7966294c5c322e613de54f00881451307e (diff)
Handle the RGB colour escape sequence (\033[38;2;<r>;<g>;<b>m and 48;2)
like xterm(1) does, by mapping to the nearest in the 256 colour palette.
Diffstat (limited to 'usr.bin/tmux')
-rw-r--r--usr.bin/tmux/colour.c36
-rw-r--r--usr.bin/tmux/input.c93
-rw-r--r--usr.bin/tmux/tmux.h3
3 files changed, 88 insertions, 44 deletions
diff --git a/usr.bin/tmux/colour.c b/usr.bin/tmux/colour.c
index 6e10a2fdc96..8dc65e02e8e 100644
--- a/usr.bin/tmux/colour.c
+++ b/usr.bin/tmux/colour.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: colour.c,v 1.9 2015/06/05 22:01:17 nicm Exp $ */
+/* $OpenBSD: colour.c,v 1.10 2015/06/05 22:33:39 nicm Exp $ */
/*
* Copyright (c) 2008 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -281,12 +281,11 @@ const struct colour_rgb colour_to_256[] = {
{ 214, 0xff, 0xff, 0xd7 }, { 215, 0xff, 0xff, 0xff },
};
-int colour_rgb_cmp(const void *, const void *);
-int colour_rgb_find(struct colour_rgb *);
+int colour_cmp_rgb(const void *, const void *);
/* Compare function for bsearch(). */
int
-colour_rgb_cmp(const void *lhs0, const void *rhs0)
+colour_cmp_rgb(const void *lhs0, const void *rhs0)
{
const struct colour_rgb *lhs = lhs0, *rhs = rhs0;
@@ -310,23 +309,22 @@ colour_rgb_cmp(const void *lhs0, const void *rhs0)
/* Work out the nearest colour from the 256 colour set. */
int
-colour_rgb_find(struct colour_rgb *rgb)
+colour_find_rgb(u_char r, u_char g, u_char b)
{
- struct colour_rgb *found;
- u_int distance, lowest, colour, i;
- int r, g, b;
+ struct colour_rgb rgb = { .r = r, .g = g, .b = b }, *found;
+ u_int distance, lowest, colour, i;
- found = bsearch(rgb, colour_to_256, nitems(colour_to_256),
- sizeof colour_to_256[0], colour_rgb_cmp);
+ found = bsearch(&rgb, colour_to_256, nitems(colour_to_256),
+ sizeof colour_to_256[0], colour_cmp_rgb);
if (found != NULL)
return (16 + found->i);
colour = 16;
lowest = UINT_MAX;
for (i = 0; i < 240; i++) {
- r = colour_from_256[i].r - rgb->r;
- g = colour_from_256[i].g - rgb->g;
- b = colour_from_256[i].b - rgb->b;
+ r = colour_from_256[i].r - rgb.r;
+ g = colour_from_256[i].g - rgb.g;
+ b = colour_from_256[i].b - rgb.b;
distance = r * r + g * g + b * b;
if (distance < lowest) {
@@ -409,20 +407,20 @@ colour_tostring(int c)
int
colour_fromstring(const char *s)
{
- const char *errstr;
- const char *cp;
- struct colour_rgb rgb;
- int n;
+ const char *errstr;
+ const char *cp;
+ int n;
+ u_char r, g, b;
if (*s == '#' && strlen(s) == 7) {
for (cp = s + 1; isxdigit((u_char) *cp); cp++)
;
if (*cp != '\0')
return (-1);
- n = sscanf(s + 1, "%2hhx%2hhx%2hhx", &rgb.r, &rgb.g, &rgb.b);
+ n = sscanf(s + 1, "%2hhx%2hhx%2hhx", &r, &g, &b);
if (n != 3)
return (-1);
- return (colour_rgb_find(&rgb) | 0x100);
+ return (colour_find_rgb(r, g, b) | 0x100);
}
if (strncasecmp(s, "colour", (sizeof "colour") - 1) == 0) {
diff --git a/usr.bin/tmux/input.c b/usr.bin/tmux/input.c
index c7c5b7c3f1a..e73119fd41b 100644
--- a/usr.bin/tmux/input.c
+++ b/usr.bin/tmux/input.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: input.c,v 1.77 2015/06/04 09:42:29 nicm Exp $ */
+/* $OpenBSD: input.c,v 1.78 2015/06/05 22:33:39 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -126,6 +126,8 @@ void input_csi_dispatch_rm_private(struct input_ctx *);
void input_csi_dispatch_sm(struct input_ctx *);
void input_csi_dispatch_sm_private(struct input_ctx *);
void input_csi_dispatch_winops(struct input_ctx *);
+void input_csi_dispatch_sgr_256(struct input_ctx *, int, u_int *);
+void input_csi_dispatch_sgr_rgb(struct input_ctx *, int, u_int *);
void input_csi_dispatch_sgr(struct input_ctx *);
int input_dcs_dispatch(struct input_ctx *);
int input_utf8_open(struct input_ctx *);
@@ -1609,13 +1611,71 @@ input_csi_dispatch_winops(struct input_ctx *ictx)
}
}
+/* Handle CSI SGR for 256 colours. */
+void
+input_csi_dispatch_sgr_256(struct input_ctx *ictx, int fgbg, u_int *i)
+{
+ struct grid_cell *gc = &ictx->cell.cell;
+ int c;
+
+ (*i)++;
+ c = input_get(ictx, *i, 0, -1);
+ if (c == -1) {
+ if (fgbg == 38) {
+ gc->flags &= ~GRID_FLAG_FG256;
+ gc->fg = 8;
+ } else if (fgbg == 48) {
+ gc->flags &= ~GRID_FLAG_BG256;
+ gc->bg = 8;
+ }
+ } else {
+ if (fgbg == 38) {
+ gc->flags |= GRID_FLAG_FG256;
+ gc->fg = c;
+ } else if (fgbg == 48) {
+ gc->flags |= GRID_FLAG_BG256;
+ gc->bg = c;
+ }
+ }
+}
+
+/* Handle CSI SGR for RGB colours. */
+void
+input_csi_dispatch_sgr_rgb(struct input_ctx *ictx, int fgbg, u_int *i)
+{
+ struct grid_cell *gc = &ictx->cell.cell;
+ int c, r, g, b;
+
+ (*i)++;
+ r = input_get(ictx, *i, 0, -1);
+ if (r == -1 || r > 255)
+ return;
+ (*i)++;
+ g = input_get(ictx, *i, 0, -1);
+ if (g == -1 || g > 255)
+ return;
+ (*i)++;
+ b = input_get(ictx, *i, 0, -1);
+ if (b == -1 || b > 255)
+ return;
+
+ c = colour_find_rgb(r, g, b);
+ if (fgbg == 38) {
+ gc->flags |= GRID_FLAG_FG256;
+ gc->fg = c;
+ } else if (fgbg == 48) {
+ gc->flags |= GRID_FLAG_BG256;
+ gc->bg = c;
+ }
+}
+
/* Handle CSI SGR. */
void
input_csi_dispatch_sgr(struct input_ctx *ictx)
{
struct grid_cell *gc = &ictx->cell.cell;
u_int i;
- int n, m;
+ int n;
if (ictx->param_list_len == 0) {
memcpy(gc, &grid_default_cell, sizeof *gc);
@@ -1627,28 +1687,13 @@ input_csi_dispatch_sgr(struct input_ctx *ictx)
if (n == 38 || n == 48) {
i++;
- if (input_get(ictx, i, 0, -1) != 5)
- continue;
-
- i++;
- m = input_get(ictx, i, 0, -1);
- if (m == -1) {
- if (n == 38) {
- gc->flags &= ~GRID_FLAG_FG256;
- gc->fg = 8;
- } else if (n == 48) {
- gc->flags &= ~GRID_FLAG_BG256;
- gc->bg = 8;
- }
-
- } else {
- if (n == 38) {
- gc->flags |= GRID_FLAG_FG256;
- gc->fg = m;
- } else if (n == 48) {
- gc->flags |= GRID_FLAG_BG256;
- gc->bg = m;
- }
+ switch (input_get(ictx, i, 0, -1)) {
+ case 2:
+ input_csi_dispatch_sgr_rgb(ictx, n, &i);
+ break;
+ case 5:
+ input_csi_dispatch_sgr_256(ictx, n, &i);
+ break;
}
continue;
}
diff --git a/usr.bin/tmux/tmux.h b/usr.bin/tmux/tmux.h
index be9c2f87fde..086fbad7b57 100644
--- a/usr.bin/tmux/tmux.h
+++ b/usr.bin/tmux/tmux.h
@@ -1,4 +1,4 @@
-/* $OpenBSD: tmux.h,v 1.520 2015/06/05 18:18:32 nicm Exp $ */
+/* $OpenBSD: tmux.h,v 1.521 2015/06/05 22:33:39 nicm Exp $ */
/*
* Copyright (c) 2007 Nicholas Marriott <nicm@users.sourceforge.net>
@@ -1953,6 +1953,7 @@ char *xterm_keys_lookup(int);
int xterm_keys_find(const char *, size_t, size_t *, int *);
/* colour.c */
+int colour_find_rgb(u_char, u_char, u_char);
void colour_set_fg(struct grid_cell *, int);
void colour_set_bg(struct grid_cell *, int);
const char *colour_tostring(int);