diff options
author | Nicholas Marriott <nicm@cvs.openbsd.org> | 2015-06-05 22:33:40 +0000 |
---|---|---|
committer | Nicholas Marriott <nicm@cvs.openbsd.org> | 2015-06-05 22:33:40 +0000 |
commit | cb7451af2d1c7a38c406d9ba6af36a7733727112 (patch) | |
tree | d1e4f4e95fd89f6e032a3ad157c85fbc9c4909d4 /usr.bin/tmux/colour.c | |
parent | 1664eb7966294c5c322e613de54f00881451307e (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/colour.c')
-rw-r--r-- | usr.bin/tmux/colour.c | 36 |
1 files changed, 17 insertions, 19 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) { |