Skip to content

Commit

Permalink
1145 - dump termbox's 256-color support
Browse files Browse the repository at this point in the history
  • Loading branch information
akkartik committed Apr 23, 2015
1 parent 8e7827d commit 547ec78
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 118 deletions.
40 changes: 0 additions & 40 deletions cpp/termbox/output.inl
@@ -1,43 +1,3 @@
/* Sets the termbox output mode. Termbox has three output options:
* 1. TB_OUTPUT_NORMAL => [1..8]
* This mode provides 8 different colors:
* black, red, green, yellow, blue, magenta, cyan, white
* Shortcut: TB_BLACK, TB_RED, ...
* Attributes: TB_BOLD, TB_UNDERLINE, TB_REVERSE
*
* Example usage:
* tb_change_cell(x, y, '@', TB_BLACK | TB_BOLD, TB_RED);
*
* 2. TB_OUTPUT_256 => [0..256]
* In this mode you can leverage the 256 terminal mode:
* 0x00 - 0x07: the 8 colors as in TB_OUTPUT_NORMAL
* 0x08 - 0x0f: TB_* | TB_BOLD
* 0x10 - 0xe7: 216 different colors
* 0xe8 - 0xff: 24 different shades of grey
*
* Example usage:
* tb_change_cell(x, y, '@', 184, 240);
* tb_change_cell(x, y, '@', 0xb8, 0xf0);
*
* 2. TB_OUTPUT_216 => [0..216]
* This mode supports the 3rd range of the 256 mode only.
* But you don't need to provide an offset.
*
* 3. TB_OUTPUT_GRAYSCALE => [0..23]
* This mode supports the 4th range of the 256 mode only.
* But you dont need to provide an offset.
*
* Execute build/src/demo/output to see its impact on your terminal.
*
* If 'mode' is TB_OUTPUT_CURRENT, it returns the current output mode.
*/
int tb_select_output_mode(int mode);
#define TB_OUTPUT_CURRENT 0
#define TB_OUTPUT_NORMAL 1
#define TB_OUTPUT_256 2
#define TB_OUTPUT_216 3
#define TB_OUTPUT_GRAYSCALE 4

enum {
T_ENTER_CA,
T_EXIT_CA,
Expand Down
92 changes: 14 additions & 78 deletions cpp/termbox/termbox.c
Expand Up @@ -40,8 +40,6 @@ static struct bytebuffer input_buffer;
static int termw = -1;
static int termh = -1;

static int outputmode = TB_OUTPUT_NORMAL;

static int inout;
static int winch_fds[2];

Expand Down Expand Up @@ -268,13 +266,6 @@ void tb_clear(void)
cellbuf_clear(&back_buffer);
}

int tb_select_output_mode(int mode)
{
if (mode)
outputmode = mode;
return outputmode;
}

void tb_set_clear_attributes(uint16_t fg, uint16_t bg)
{
foreground = fg;
Expand Down Expand Up @@ -310,46 +301,27 @@ static void write_cursor(int x, int y) {
WRITE_LITERAL("H");
}

// can only be called in NORMAL output mode
static void write_sgr_fg(uint16_t fg) {
char buf[32];

WRITE_LITERAL("\033[3");
WRITE_INT(fg-1);
WRITE_LITERAL("m");
}

// can only be called in NORMAL output mode
static void write_sgr_bg(uint16_t bg) {
char buf[32];

WRITE_LITERAL("\033[4");
WRITE_INT(bg-1);
WRITE_LITERAL("m");
}

static void write_sgr(uint16_t fg, uint16_t bg) {
char buf[32];

switch (outputmode) {
case TB_OUTPUT_256:
case TB_OUTPUT_216:
case TB_OUTPUT_GRAYSCALE:
WRITE_LITERAL("\033[38;5;");
WRITE_INT(fg);
WRITE_LITERAL("m");
WRITE_LITERAL("\033[48;5;");
WRITE_INT(bg);
WRITE_LITERAL("m");
break;
case TB_OUTPUT_NORMAL:
default:
WRITE_LITERAL("\033[3");
WRITE_INT(fg-1);
WRITE_LITERAL(";4");
WRITE_INT(bg-1);
WRITE_LITERAL("m");
}
WRITE_LITERAL("\033[3");
WRITE_INT(fg-1);
WRITE_LITERAL(";4");
WRITE_INT(bg-1);
WRITE_LITERAL("m");
}

static void cellbuf_init(struct cellbuf *buf, int width, int height)
Expand Down Expand Up @@ -431,34 +403,8 @@ static void send_attr(uint16_t fg, uint16_t bg)
if (fg != lastfg || bg != lastbg) {
bytebuffer_puts(&output_buffer, funcs[T_SGR0]);

uint16_t fgcol;
uint16_t bgcol;

switch (outputmode) {
case TB_OUTPUT_256:
fgcol = fg & 0xFF;
bgcol = bg & 0xFF;
break;

case TB_OUTPUT_216:
fgcol = fg & 0xFF; if (fgcol > 215) fgcol = 7;
bgcol = bg & 0xFF; if (bgcol > 215) bgcol = 0;
fgcol += 0x10;
bgcol += 0x10;
break;

case TB_OUTPUT_GRAYSCALE:
fgcol = fg & 0xFF; if (fgcol > 23) fgcol = 23;
bgcol = bg & 0xFF; if (bgcol > 23) bgcol = 0;
fgcol += 0xe8;
bgcol += 0xe8;
break;

case TB_OUTPUT_NORMAL:
default:
fgcol = fg & 0x0F;
bgcol = bg & 0x0F;
}
uint16_t fgcol = fg & 0x0F;
uint16_t bgcol = bg & 0x0F;

if (fg & TB_BOLD)
bytebuffer_puts(&output_buffer, funcs[T_BOLD]);
Expand All @@ -469,23 +415,13 @@ static void send_attr(uint16_t fg, uint16_t bg)
if ((fg & TB_REVERSE) || (bg & TB_REVERSE))
bytebuffer_puts(&output_buffer, funcs[T_REVERSE]);

switch (outputmode) {
case TB_OUTPUT_256:
case TB_OUTPUT_216:
case TB_OUTPUT_GRAYSCALE:
write_sgr(fgcol, bgcol);
break;

case TB_OUTPUT_NORMAL:
default:
if (fgcol != TB_DEFAULT) {
if (bgcol != TB_DEFAULT)
write_sgr(fgcol, bgcol);
else
write_sgr_fg(fgcol);
} else if (bgcol != TB_DEFAULT) {
write_sgr_bg(bgcol);
}
if (fgcol != TB_DEFAULT) {
if (bgcol != TB_DEFAULT)
write_sgr(fgcol, bgcol);
else
write_sgr_fg(fgcol);
} else if (bgcol != TB_DEFAULT) {
write_sgr_bg(bgcol);
}

lastfg = fg;
Expand Down

0 comments on commit 547ec78

Please sign in to comment.