Instead of representing colours in several different forms with various

cell flags, convert to use an int with flags marking 256 or RGB colours
in the top byte (except in cells, which we don't want to make any
bigger). From Brad Town.
This commit is contained in:
nicm 2016-07-15 00:42:56 +00:00
parent fc118e13a9
commit 1fd6ca2260
5 changed files with 69 additions and 132 deletions

View File

@ -67,7 +67,7 @@ colour_find_rgb(u_char r, u_char g, u_char b)
/* If we have hit the colour exactly, return early. */
if (cr == r && cg == g && cb == b)
return (16 + (36 * qr) + (6 * qg) + qb);
return ((16 + (36 * qr) + (6 * qg) + qb) | COLOUR_FLAG_256);
/* Work out the closest grey (average of RGB). */
grey_avg = (r + g + b) / 3;
@ -83,25 +83,25 @@ colour_find_rgb(u_char r, u_char g, u_char b)
idx = 232 + grey_idx;
else
idx = 16 + (36 * qr) + (6 * qg) + qb;
return (idx);
return (idx | COLOUR_FLAG_256);
}
/* Set grid cell foreground colour. */
void
colour_set_fg(struct grid_cell *gc, int c)
/* Join RGB into a colour. */
int
colour_join_rgb(u_char r, u_char g, u_char b)
{
if (c & 0x100)
gc->flags |= GRID_FLAG_FG256;
gc->fg = c;
return ((((int)((r) & 0xff)) << 16) |
(((int)((g) & 0xff)) << 8) |
(((int)((b) & 0xff))) | COLOUR_FLAG_RGB);
}
/* Set grid cell background colour. */
/* Split colour into RGB. */
void
colour_set_bg(struct grid_cell *gc, int c)
colour_split_rgb(int c, u_char *r, u_char *g, u_char *b)
{
if (c & 0x100)
gc->flags |= GRID_FLAG_BG256;
gc->bg = c;
*r = (c >> 16) & 0xff;
*g = (c >> 8) & 0xff;
*b = c & 0xff;
}
/* Convert colour to a string. */
@ -109,9 +109,16 @@ const char *
colour_tostring(int c)
{
static char s[32];
u_char r, g, b;
if (c & 0x100) {
xsnprintf(s, sizeof s, "colour%d", c & ~0x100);
if (c & COLOUR_FLAG_RGB) {
colour_split_rgb(c, &r, &g, &b);
xsnprintf(s, sizeof s, "#%02x%02x%02x", r, g, b);
return (s);
}
if (c & COLOUR_FLAG_256) {
xsnprintf(s, sizeof s, "colour%u", c & 0xff);
return (s);
}
@ -171,14 +178,14 @@ colour_fromstring(const char *s)
n = sscanf(s + 1, "%2hhx%2hhx%2hhx", &r, &g, &b);
if (n != 3)
return (-1);
return (colour_find_rgb(r, g, b) | 0x100);
return (colour_join_rgb(r, g, b));
}
if (strncasecmp(s, "colour", (sizeof "colour") - 1) == 0) {
n = strtonum(s + (sizeof "colour") - 1, 0, 255, &errstr);
if (errstr != NULL)
return (-1);
return (n | 0x100);
return (n | COLOUR_FLAG_256);
}
if (strcasecmp(s, "black") == 0 || strcmp(s, "0") == 0)

43
input.c
View File

@ -1628,23 +1628,15 @@ input_csi_dispatch_sgr_256(struct input_ctx *ictx, int fgbg, u_int *i)
(*i)++;
c = input_get(ictx, *i, 0, -1);
if (c == -1) {
if (fgbg == 38) {
gc->flags &= ~(GRID_FLAG_FG256|GRID_FLAG_FGRGB);
if (fgbg == 38)
gc->fg = 8;
} else if (fgbg == 48) {
gc->flags &= ~(GRID_FLAG_BG256|GRID_FLAG_BGRGB);
else if (fgbg == 48)
gc->bg = 8;
}
} else {
if (fgbg == 38) {
gc->flags |= GRID_FLAG_FG256;
gc->flags &= ~GRID_FLAG_FGRGB;
gc->fg = c;
} else if (fgbg == 48) {
gc->flags |= GRID_FLAG_BG256;
gc->flags &= ~GRID_FLAG_BGRGB;
gc->bg = c;
}
if (fgbg == 38)
gc->fg = c | COLOUR_FLAG_256;
else if (fgbg == 48)
gc->bg = c | COLOUR_FLAG_256;
}
}
@ -1668,19 +1660,10 @@ input_csi_dispatch_sgr_rgb(struct input_ctx *ictx, int fgbg, u_int *i)
if (b == -1 || b > 255)
return;
if (fgbg == 38) {
gc->flags &= ~GRID_FLAG_FG256;
gc->flags |= GRID_FLAG_FGRGB;
gc->fg_rgb.r = r;
gc->fg_rgb.g = g;
gc->fg_rgb.b = b;
} else if (fgbg == 48) {
gc->flags &= ~GRID_FLAG_BG256;
gc->flags |= GRID_FLAG_BGRGB;
gc->bg_rgb.r = r;
gc->bg_rgb.g = g;
gc->bg_rgb.b = b;
}
if (fgbg == 38)
gc->fg = colour_join_rgb(r, g, b);
else if (fgbg == 48)
gc->bg = colour_join_rgb(r, g, b);
}
/* Handle CSI SGR. */
@ -1761,11 +1744,9 @@ input_csi_dispatch_sgr(struct input_ctx *ictx)
case 35:
case 36:
case 37:
gc->flags &= ~(GRID_FLAG_FG256|GRID_FLAG_FGRGB);
gc->fg = n - 30;
break;
case 39:
gc->flags &= ~(GRID_FLAG_FG256|GRID_FLAG_FGRGB);
gc->fg = 8;
break;
case 40:
@ -1776,11 +1757,9 @@ input_csi_dispatch_sgr(struct input_ctx *ictx)
case 45:
case 46:
case 47:
gc->flags &= ~(GRID_FLAG_BG256|GRID_FLAG_BGRGB);
gc->bg = n - 40;
break;
case 49:
gc->flags &= ~(GRID_FLAG_BG256|GRID_FLAG_BGRGB);
gc->bg = 8;
break;
case 90:
@ -1791,7 +1770,6 @@ input_csi_dispatch_sgr(struct input_ctx *ictx)
case 95:
case 96:
case 97:
gc->flags &= ~(GRID_FLAG_FG256|GRID_FLAG_FGRGB);
gc->fg = n;
break;
case 100:
@ -1802,7 +1780,6 @@ input_csi_dispatch_sgr(struct input_ctx *ictx)
case 105:
case 106:
case 107:
gc->flags &= ~(GRID_FLAG_BG256|GRID_FLAG_BGRGB);
gc->bg = n - 10;
break;
}

View File

@ -552,9 +552,9 @@ screen_redraw_draw_number(struct client *c, struct window_pane *wp, u_int top)
memcpy(&gc, &grid_default_cell, sizeof gc);
if (w->active == wp)
colour_set_bg(&gc, active_colour);
gc.bg = active_colour;
else
colour_set_bg(&gc, colour);
gc.bg = colour;
tty_attributes(tty, &gc, wp);
for (ptr = buf; *ptr != '\0'; ptr++) {
if (*ptr < '0' || *ptr > '9')
@ -579,9 +579,9 @@ screen_redraw_draw_number(struct client *c, struct window_pane *wp, u_int top)
draw_text:
memcpy(&gc, &grid_default_cell, sizeof gc);
if (w->active == wp)
colour_set_fg(&gc, active_colour);
gc.fg = active_colour;
else
colour_set_fg(&gc, colour);
gc.fg = colour;
tty_attributes(tty, &gc, wp);
tty_puts(tty, buf);

105
style.c
View File

@ -33,7 +33,8 @@ style_parse(const struct grid_cell *defgc, struct grid_cell *gc,
char tmp[32];
int val;
size_t end;
u_char fg, bg, attr, flags;
int fg, bg;
u_char attr, flags;
if (*in == '\0')
return (0);
@ -56,38 +57,20 @@ style_parse(const struct grid_cell *defgc, struct grid_cell *gc,
fg = defgc->fg;
bg = defgc->bg;
attr = defgc->attr;
flags &= ~(GRID_FLAG_FG256|GRID_FLAG_BG256);
flags |=
defgc->flags & (GRID_FLAG_FG256|GRID_FLAG_BG256);
flags = defgc->flags;
} else if (end > 3 && strncasecmp(tmp + 1, "g=", 2) == 0) {
if ((val = colour_fromstring(tmp + 3)) == -1)
goto error;
if (*in == 'f' || *in == 'F') {
if (val != 8) {
if (val & 0x100) {
flags |= GRID_FLAG_FG256;
val &= ~0x100;
} else
flags &= ~GRID_FLAG_FG256;
if (val != 8)
fg = val;
} else {
else
fg = defgc->fg;
flags &= ~GRID_FLAG_FG256;
flags |= defgc->flags & GRID_FLAG_FG256;
}
} else if (*in == 'b' || *in == 'B') {
if (val != 8) {
if (val & 0x100) {
flags |= GRID_FLAG_BG256;
val &= ~0x100;
} else
flags &= ~GRID_FLAG_BG256;
if (val != 8)
bg = val;
} else {
else
bg = defgc->bg;
flags &= ~GRID_FLAG_BG256;
flags |= defgc->flags & GRID_FLAG_BG256;
}
} else
goto error;
} else if (strcasecmp(tmp, "none") == 0)
@ -120,27 +103,19 @@ error:
const char *
style_tostring(struct grid_cell *gc)
{
int c, off = 0, comma = 0;
int off = 0, comma = 0;
static char s[256];
*s = '\0';
if (gc->fg != 8 || gc->flags & GRID_FLAG_FG256) {
if (gc->flags & GRID_FLAG_FG256)
c = gc->fg | 0x100;
else
c = gc->fg;
off += xsnprintf(s, sizeof s, "fg=%s", colour_tostring(c));
if (gc->fg != 8) {
off += xsnprintf(s, sizeof s, "fg=%s", colour_tostring(gc->fg));
comma = 1;
}
if (gc->bg != 8 || gc->flags & GRID_FLAG_BG256) {
if (gc->flags & GRID_FLAG_BG256)
c = gc->bg | 0x100;
else
c = gc->bg;
if (gc->bg != 8) {
off += xsnprintf(s + off, sizeof s - off, "%sbg=%s",
comma ? "," : "", colour_tostring(c));
comma ? "," : "", colour_tostring(gc->bg));
comma = 1;
}
@ -177,9 +152,9 @@ style_update_new(struct options *oo, const char *name, const char *newname)
value = o->num;
if (strstr(name, "-bg") != NULL)
colour_set_bg(gc, value);
gc->bg = value;
else if (strstr(name, "-fg") != NULL)
colour_set_fg(gc, value);
gc->fg = value;
else if (strstr(name, "-attr") != NULL)
gc->attr = value;
}
@ -189,23 +164,15 @@ void
style_update_old(struct options *oo, const char *name, struct grid_cell *gc)
{
char newname[128];
int c, size;
int size;
size = strrchr(name, '-') - name;
if (gc->flags & GRID_FLAG_BG256)
c = gc->bg | 0x100;
else
c = gc->bg;
xsnprintf(newname, sizeof newname, "%.*s-bg", size, name);
options_set_number(oo, newname, c);
options_set_number(oo, newname, gc->bg);
if (gc->flags & GRID_FLAG_FG256)
c = gc->fg | 0x100;
else
c = gc->fg;
xsnprintf(newname, sizeof newname, "%.*s-fg", size, name);
options_set_number(oo, newname, c);
options_set_number(oo, newname, gc->fg);
xsnprintf(newname, sizeof newname, "%.*s-attr", size, name);
options_set_number(oo, newname, gc->attr);
@ -219,14 +186,8 @@ style_apply(struct grid_cell *gc, struct options *oo, const char *name)
memcpy(gc, &grid_default_cell, sizeof *gc);
gcp = options_get_style(oo, name);
if (gcp->flags & GRID_FLAG_FG256)
colour_set_fg(gc, gcp->fg | 0x100);
else
colour_set_fg(gc, gcp->fg);
if (gcp->flags & GRID_FLAG_BG256)
colour_set_bg(gc, gcp->bg | 0x100);
else
colour_set_bg(gc, gcp->bg);
gc->fg = gcp->fg;
gc->bg = gcp->bg;
gc->attr |= gcp->attr;
}
@ -237,18 +198,10 @@ style_apply_update(struct grid_cell *gc, struct options *oo, const char *name)
struct grid_cell *gcp;
gcp = options_get_style(oo, name);
if (gcp->fg != 8 || gcp->flags & GRID_FLAG_FG256) {
if (gcp->flags & GRID_FLAG_FG256)
colour_set_fg(gc, gcp->fg | 0x100);
else
colour_set_fg(gc, gcp->fg);
}
if (gcp->bg != 8 || gcp->flags & GRID_FLAG_BG256) {
if (gcp->flags & GRID_FLAG_BG256)
colour_set_bg(gc, gcp->bg | 0x100);
else
colour_set_bg(gc, gcp->bg);
}
if (gcp->fg != 8)
gc->fg = gcp->fg;
if (gcp->bg != 8)
gc->bg = gcp->bg;
if (gcp->attr != 0)
gc->attr |= gcp->attr;
}
@ -257,10 +210,10 @@ style_apply_update(struct grid_cell *gc, struct options *oo, const char *name)
int
style_equal(const struct grid_cell *gc1, const struct grid_cell *gc2)
{
return gc1->fg == gc2->fg &&
gc1->bg == gc2->bg &&
(gc1->flags & ~GRID_FLAG_PADDING) ==
(gc2->flags & ~GRID_FLAG_PADDING) &&
(gc1->attr & ~GRID_ATTR_CHARSET) ==
(gc2->attr & ~GRID_ATTR_CHARSET);
return (gc1->fg == gc2->fg &&
gc1->bg == gc2->bg &&
(gc1->flags & ~GRID_FLAG_PADDING) ==
(gc2->flags & ~GRID_FLAG_PADDING) &&
(gc1->attr & ~GRID_ATTR_CHARSET) ==
(gc2->attr & ~GRID_ATTR_CHARSET));
}

View File

@ -230,7 +230,7 @@ window_clock_draw_screen(struct window_pane *wp)
screen_write_cursormove(&ctx, x, y);
memcpy(&gc, &grid_default_cell, sizeof gc);
colour_set_fg(&gc, colour);
gc.fg = colour;
screen_write_puts(&ctx, &gc, "%s", tim);
}
@ -242,7 +242,7 @@ window_clock_draw_screen(struct window_pane *wp)
y = (screen_size_y(s) / 2) - 3;
memcpy(&gc, &grid_default_cell, sizeof gc);
colour_set_bg(&gc, colour);
gc.bg = colour;
for (ptr = tim; *ptr != '\0'; ptr++) {
if (*ptr >= '0' && *ptr <= '9')
idx = *ptr - '0';