public
Last active

tmux 24 bit colour support

  • Download Gist
tmux_24_bit_colours.c
C
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432
From 920989223fda9fec3f178f4a772eca920dcb8515 Mon Sep 17 00:00:00 2001
From: Arnis Lapsa <arnis.lapsa@gmail.com>
Date: Fri, 2 Aug 2013 16:15:46 +0300
Subject: [PATCH] 24bit colour support
 
---
colour.c | 6 ---
input.c | 30 +++++++++++++-
screen-write.c | 2 +
tmux.h | 11 +++++
tty.c | 124 +++++++++++++++++++++++++++++++++++++++++----------------
5 files changed, 132 insertions(+), 41 deletions(-)
 
diff --git a/colour.c b/colour.c
index da1cb42..09a969d 100644
--- a/colour.c
+++ b/colour.c
@@ -29,12 +29,6 @@
* of the 256 colour palette.
*/
-/* An RGB colour. */
-struct colour_rgb {
- u_char r;
- u_char g;
- u_char b;
-};
/* 256 colour RGB table, generated on first use. */
struct colour_rgb *colour_rgb_256;
diff --git a/input.c b/input.c
index 30d3bb9..7c3ed44 100644
--- a/input.c
+++ b/input.c
@@ -1404,7 +1404,26 @@ input_csi_dispatch_sgr(struct input_ctx *ictx)
if (n == 38 || n == 48) {
i++;
- if (input_get(ictx, i, 0, -1) != 5)
+ m=input_get(ictx, i, 0, -1);
+ if (m == 2){ // 24bit?
+ u_char r, g, b;
+ r = input_get(ictx, i+1, 0, -1);
+ g = input_get(ictx, i+2, 0, -1);
+ b = input_get(ictx, i+3, 0, -1);
+ struct colour_rgb rgb = {.r=r, .g=g, .b=b};
+ if (n == 38){
+ gc->flags &= ~GRID_FLAG_FG256;
+ gc->flags |= GRID_FLAG_FG24;
+ gc->fg_rgb = rgb;
+ } else if (n == 48){
+ gc->flags &= ~GRID_FLAG_BG256;
+ gc->flags |= GRID_FLAG_BG24;
+ gc->bg_rgb = rgb;
+ }
+ break;
+ }
+
+ if (m != 5)
continue;
i++;
@@ -1412,18 +1431,22 @@ input_csi_dispatch_sgr(struct input_ctx *ictx)
if (m == -1) {
if (n == 38) {
gc->flags &= ~GRID_FLAG_FG256;
+ gc->flags &= ~GRID_FLAG_FG24;
gc->fg = 8;
} else if (n == 48) {
gc->flags &= ~GRID_FLAG_BG256;
+ gc->flags &= ~GRID_FLAG_BG24;
gc->bg = 8;
}
} else {
if (n == 38) {
gc->flags |= GRID_FLAG_FG256;
+ gc->flags &= ~GRID_FLAG_FG24;
gc->fg = m;
} else if (n == 48) {
gc->flags |= GRID_FLAG_BG256;
+ gc->flags &= ~GRID_FLAG_BG24;
gc->bg = m;
}
}
@@ -1482,10 +1505,12 @@ input_csi_dispatch_sgr(struct input_ctx *ictx)
case 36:
case 37:
gc->flags &= ~GRID_FLAG_FG256;
+ gc->flags &= ~GRID_FLAG_FG24;
gc->fg = n - 30;
break;
case 39:
gc->flags &= ~GRID_FLAG_FG256;
+ gc->flags &= ~GRID_FLAG_FG24;
gc->fg = 8;
break;
case 40:
@@ -1497,10 +1522,12 @@ input_csi_dispatch_sgr(struct input_ctx *ictx)
case 46:
case 47:
gc->flags &= ~GRID_FLAG_BG256;
+ gc->flags &= ~GRID_FLAG_BG24;
gc->bg = n - 40;
break;
case 49:
gc->flags &= ~GRID_FLAG_BG256;
+ gc->flags &= ~GRID_FLAG_BG24;
gc->bg = 8;
break;
case 90:
@@ -1523,6 +1550,7 @@ input_csi_dispatch_sgr(struct input_ctx *ictx)
case 106:
case 107:
gc->flags &= ~GRID_FLAG_BG256;
+ gc->flags &= ~GRID_FLAG_BG24;
gc->bg = n - 10;
break;
}
diff --git a/screen-write.c b/screen-write.c
index ae89293..e426f2b 100644
--- a/screen-write.c
+++ b/screen-write.c
@@ -289,6 +289,7 @@ screen_write_cnputs(struct screen_write_ctx *ctx,
}
/* Parse an embedded style of the form "fg=colour,bg=colour,bright,...". */
+//TODO
void
screen_write_parsestyle(
struct grid_cell *defgc, struct grid_cell *gc, const char *in)
@@ -1072,6 +1073,7 @@ screen_write_cell(struct screen_write_ctx *ctx, const struct grid_cell *gc)
memcpy(&tmp_gc, &s->sel.cell, sizeof tmp_gc);
grid_cell_get(gc, &ud);
grid_cell_set(&tmp_gc, &ud);
+ //TODO
tmp_gc.flags = gc->flags & ~(GRID_FLAG_FG256|GRID_FLAG_BG256);
tmp_gc.flags |= s->sel.cell.flags &
(GRID_FLAG_FG256|GRID_FLAG_BG256);
diff --git a/tmux.h b/tmux.h
index cc1c79e..1fc6043 100644
--- a/tmux.h
+++ b/tmux.h
@@ -713,10 +713,19 @@ struct utf8_data {
#define GRID_FLAG_FG256 0x1
#define GRID_FLAG_BG256 0x2
#define GRID_FLAG_PADDING 0x4
+#define GRID_FLAG_FG24 0x8
+#define GRID_FLAG_BG24 0x10
/* Grid line flags. */
#define GRID_LINE_WRAPPED 0x1
+/* An RGB colour. */
+struct colour_rgb {
+ u_char r;
+ u_char g;
+ u_char b;
+};
+
/* Grid cell data. */
struct grid_cell {
u_char attr;
@@ -726,6 +735,8 @@ struct grid_cell {
u_char xstate; /* top 4 bits width, bottom 4 bits size */
u_char xdata[UTF8_SIZE];
+ struct colour_rgb fg_rgb;
+ struct colour_rgb bg_rgb;
} __packed;
/* Grid line. */
diff --git a/tty.c b/tty.c
index d5b1aec..61b65b1 100644
--- a/tty.c
+++ b/tty.c
@@ -35,6 +35,7 @@ void tty_read_callback(struct bufferevent *, void *);
void tty_error_callback(struct bufferevent *, short, void *);
int tty_try_256(struct tty *, u_char, const char *);
+int tty_try_24(struct tty *, struct colour_rgb, const char *);
void tty_colours(struct tty *, const struct grid_cell *);
void tty_check_fg(struct tty *, struct grid_cell *);
@@ -1380,14 +1381,23 @@ tty_attributes(struct tty *tty, const struct grid_cell *gc)
void
tty_colours(struct tty *tty, const struct grid_cell *gc)
-{
+{
struct grid_cell *tc = &tty->cell;
u_char fg = gc->fg, bg = gc->bg, flags = gc->flags;
int have_ax, fg_default, bg_default;
/* No changes? Nothing is necessary. */
if (fg == tc->fg && bg == tc->bg &&
- ((flags ^ tc->flags) & (GRID_FLAG_FG256|GRID_FLAG_BG256)) == 0)
+ tc->fg_rgb.r == gc->fg_rgb.r &&
+ tc->fg_rgb.g == gc->fg_rgb.g &&
+ tc->fg_rgb.b == gc->fg_rgb.b &&
+
+ tc->bg_rgb.r == gc->bg_rgb.r &&
+ tc->bg_rgb.g == gc->bg_rgb.g &&
+ tc->bg_rgb.b == gc->bg_rgb.b &&
+ ((flags ^ tc->flags) & (GRID_FLAG_FG256|GRID_FLAG_BG256|GRID_FLAG_FG24|GRID_FLAG_BG24)) == 0
+
+ )
return;
/*
@@ -1396,8 +1406,8 @@ tty_colours(struct tty *tty, const struct grid_cell *gc)
* case if only one is default need to fall onward to set the other
* colour.
*/
- fg_default = (fg == 8 && !(flags & GRID_FLAG_FG256));
- bg_default = (bg == 8 && !(flags & GRID_FLAG_BG256));
+ fg_default = (fg == 8 && !(flags & GRID_FLAG_FG256) && !(flags & GRID_FLAG_FG24));
+ bg_default = (bg == 8 && !(flags & GRID_FLAG_BG256) && !(flags & GRID_FLAG_BG24));
if (fg_default || bg_default) {
/*
* If don't have AX but do have op, send sgr0 (op can't
@@ -1411,39 +1421,49 @@ tty_colours(struct tty *tty, const struct grid_cell *gc)
tty_reset(tty);
else {
if (fg_default &&
- (tc->fg != 8 || tc->flags & GRID_FLAG_FG256)) {
+ (tc->fg != 8 || tc->flags & GRID_FLAG_FG256 || tc->flags & GRID_FLAG_FG24)) {
if (have_ax)
tty_puts(tty, "\033[39m");
else if (tc->fg != 7 ||
- tc->flags & GRID_FLAG_FG256)
+ tc->flags & GRID_FLAG_FG256 ||
+ tc->flags & GRID_FLAG_FG24)
tty_putcode1(tty, TTYC_SETAF, 7);
tc->fg = 8;
tc->flags &= ~GRID_FLAG_FG256;
+ tc->flags &= ~GRID_FLAG_FG24;
}
if (bg_default &&
- (tc->bg != 8 || tc->flags & GRID_FLAG_BG256)) {
+ (tc->bg != 8 || tc->flags & GRID_FLAG_BG256 || tc->flags & GRID_FLAG_BG24)) {
if (have_ax)
tty_puts(tty, "\033[49m");
else if (tc->bg != 0 ||
- tc->flags & GRID_FLAG_BG256)
+ tc->flags & GRID_FLAG_BG256 ||
+ tc->flags & GRID_FLAG_BG24)
tty_putcode1(tty, TTYC_SETAB, 0);
tc->bg = 8;
tc->flags &= ~GRID_FLAG_BG256;
+ tc->flags &= ~GRID_FLAG_BG24;
}
}
}
/* Set the foreground colour. */
- if (!fg_default && (fg != tc->fg ||
- ((flags & GRID_FLAG_FG256) != (tc->flags & GRID_FLAG_FG256))))
+ if (!fg_default && (fg != tc->fg || ((flags & GRID_FLAG_FG256) != (tc->flags & GRID_FLAG_FG256)) ||
+ (
+ ( tc->fg_rgb.r!=gc->fg_rgb.r || tc->fg_rgb.g!=gc->fg_rgb.g || tc->fg_rgb.b!=gc->fg_rgb.b ) ||
+ ((flags & GRID_FLAG_FG24) != (tc->flags & GRID_FLAG_FG24))
+ )))
tty_colours_fg(tty, gc);
/*
* Set the background colour. This must come after the foreground as
* tty_colour_fg() can call tty_reset().
*/
- if (!bg_default && (bg != tc->bg ||
- ((flags & GRID_FLAG_BG256) != (tc->flags & GRID_FLAG_BG256))))
+ if (!bg_default && (bg != tc->bg || ((flags & GRID_FLAG_BG256) != (tc->flags & GRID_FLAG_BG256)) ||
+ (
+ ( tc->bg_rgb.r!=gc->bg_rgb.r || tc->bg_rgb.g!=gc->bg_rgb.g || tc->bg_rgb.b!=gc->bg_rgb.b ) ||
+ ((flags & GRID_FLAG_BG24) != (tc->flags & GRID_FLAG_BG24))
+ )))
tty_colours_bg(tty, gc);
}
@@ -1453,7 +1473,7 @@ tty_check_fg(struct tty *tty, struct grid_cell *gc)
u_int colours;
/* Is this a 256-colour colour? */
- if (gc->flags & GRID_FLAG_FG256) {
+ if (gc->flags & GRID_FLAG_FG256 && !(gc->flags & GRID_FLAG_BG24)) {
/* And not a 256 colour mode? */
if (!(tty->term->flags & TERM_256COLOURS) &&
!(tty->term_flags & TERM_256COLOURS)) {
@@ -1482,7 +1502,7 @@ tty_check_bg(struct tty *tty, struct grid_cell *gc)
u_int colours;
/* Is this a 256-colour colour? */
- if (gc->flags & GRID_FLAG_BG256) {
+ if (gc->flags & GRID_FLAG_BG256 && !(gc->flags & GRID_FLAG_BG24)) {
/*
* And not a 256 colour mode? Translate to 16-colour
* palette. Bold background doesn't exist portably, so just
@@ -1511,15 +1531,29 @@ void
tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
{
struct grid_cell *tc = &tty->cell;
+ struct colour_rgb rgb= gc->fg_rgb;
u_char fg = gc->fg;
char s[32];
+ tc->flags &= ~GRID_FLAG_FG256;
+ tc->flags &= ~GRID_FLAG_FG24;
+
+ /* Is this a 24-colour colour? */
+ if (gc->flags & GRID_FLAG_FG24) {
+//log_debug("trying to output 24bit fg");
+ if (tty_try_24(tty, rgb, "38") == 0){
+ tc->fg_rgb = rgb;
+ tc->flags |= gc->flags & GRID_FLAG_FG24;
+ }
+ return;
+ }
+
/* Is this a 256-colour colour? */
if (gc->flags & GRID_FLAG_FG256) {
- /* Try as 256 colours. */
- if (tty_try_256(tty, fg, "38") == 0)
- goto save_fg;
- /* Else already handled by tty_check_fg. */
+ if (tty_try_256(tty, fg, "38") == 0){
+ tc->fg = fg;
+ tc->flags |= gc->flags & GRID_FLAG_FG256;
+ }
return;
}
@@ -1527,32 +1561,41 @@ tty_colours_fg(struct tty *tty, const struct grid_cell *gc)
if (fg >= 90 && fg <= 97) {
xsnprintf(s, sizeof s, "\033[%dm", fg);
tty_puts(tty, s);
- goto save_fg;
+ tc->fg = fg;
+ return;
}
/* Otherwise set the foreground colour. */
tty_putcode1(tty, TTYC_SETAF, fg);
-
-save_fg:
- /* Save the new values in the terminal current cell. */
tc->fg = fg;
- tc->flags &= ~GRID_FLAG_FG256;
- tc->flags |= gc->flags & GRID_FLAG_FG256;
}
void
tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
{
struct grid_cell *tc = &tty->cell;
+ struct colour_rgb rgb= gc->bg_rgb;
u_char bg = gc->bg;
char s[32];
+ tc->flags &= ~GRID_FLAG_BG256;
+ tc->flags &= ~GRID_FLAG_BG24;
+
+ /* Is this a 24-colour colour? */
+ if (gc->flags & GRID_FLAG_BG24) {
+ if (tty_try_24(tty, rgb, "48") == 0){
+ tc->bg_rgb = rgb;
+ tc->flags |= gc->flags & GRID_FLAG_BG24;
+ }
+ return;
+ }
+
/* Is this a 256-colour colour? */
if (gc->flags & GRID_FLAG_BG256) {
- /* Try as 256 colours. */
- if (tty_try_256(tty, bg, "48") == 0)
- goto save_bg;
- /* Else already handled by tty_check_bg. */
+ if (tty_try_256(tty, bg, "48") == 0){
+ tc->bg = bg;
+ tc->flags |= gc->flags & GRID_FLAG_BG256;
+ }
return;
}
@@ -1562,20 +1605,16 @@ tty_colours_bg(struct tty *tty, const struct grid_cell *gc)
if (tty_term_number(tty->term, TTYC_COLORS) >= 16) {
xsnprintf(s, sizeof s, "\033[%dm", bg + 10);
tty_puts(tty, s);
- goto save_bg;
+ tc->bg = bg;
}
bg -= 90;
+ return;
/* no such thing as a bold background */
}
/* Otherwise set the background colour. */
tty_putcode1(tty, TTYC_SETAB, bg);
-
-save_bg:
- /* Save the new values in the terminal current cell. */
tc->bg = bg;
- tc->flags &= ~GRID_FLAG_BG256;
- tc->flags |= gc->flags & GRID_FLAG_BG256;
}
int
@@ -1592,6 +1631,23 @@ tty_try_256(struct tty *tty, u_char colour, const char *type)
return (0);
}
+
+int
+tty_try_24(struct tty *tty, struct colour_rgb rgb, const char *type)
+{
+ char s[32];
+
+ //if (!(tty->term->flags & TERM_256COLOURS) &&
+ // !(tty->term_flags & TERM_256COLOURS))
+ // return (-1);
+
+ //xsnprintf(s, sizeof s, "\033[%s;5;%hhum", type, colour);
+ xsnprintf(s, sizeof s, "\033[%s;2;%hhu;%hhu;%hhum", type, rgb.r, rgb.g, rgb.b);
+//log_debug("24bit output: %s",s);
+ tty_puts(tty, s);
+ return (0);
+}
+
void
tty_bell(struct tty *tty)
{
--
1.8.1.2

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.