Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
need to fix is_default_color() or possibly color_rgb()
  • Loading branch information
rnpnr committed Oct 21, 2023
1 parent 932788d commit cbea2a9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 35 deletions.
9 changes: 6 additions & 3 deletions ui-terminal-curses.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@

#define MAX_COLOR_CLOBBER 240

static short color_clobber_idx = 0;
static uint32_t clobbering_colors[MAX_COLOR_CLOBBER];
static int change_colors = -1;

/* Calculate r,g,b components of one of the standard upper 240 colors */
Expand Down Expand Up @@ -82,10 +80,13 @@ static void undo_palette(void)
/* Work out the nearest color from the 256 color set, or perhaps exactly. */
static CellColor color_rgb(UiTerm *ui, uint8_t r, uint8_t g, uint8_t b)
{
static short color_clobber_idx = 0;
static uint32_t clobbering_colors[MAX_COLOR_CLOBBER];

if (change_colors == -1)
change_colors = ui->vis->change_colors && can_change_color() && COLORS >= 256;
if (change_colors) {
uint32_t hexrep = ((r << 16) | (g << 8) | b) + 1;
uint32_t hexrep = (r << 16) | (g << 8) | b;
for (short i = 0; i < MAX_COLOR_CLOBBER; ++i) {
if (clobbering_colors[i] == hexrep)
return i + 16;
Expand Down Expand Up @@ -295,5 +296,7 @@ static void ui_curses_free(UiTerm *term) {
}

bool is_default_color(CellColor c) {
if (change_colors == 1)
return c == 16;
return c == CELL_COLOR_DEFAULT;
}
51 changes: 19 additions & 32 deletions vis.c
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,18 @@ static void window_free(Win *win) {
free(win);
}

static void cell_apply_style(Cell *cell, CellStyle *style, bool cursel) {
CellStyle set = { .fg = style->fg, .bg = style->bg, .attr = style->attr };
if (cursel) {
if (is_default_color(style->fg))
set.fg = cell->style.fg;
if (is_default_color(style->bg))
set.bg = cell->style.bg;
set.attr |= cell->style.attr;
}
memcpy(&cell->style, &set, sizeof(CellStyle));
}

static void window_draw_colorcolumn(Win *win) {
View *view = win->view;
int cc = view_colorcolumn_get(view);
Expand All @@ -315,10 +327,7 @@ static void window_draw_colorcolumn(Win *win) {

/* This screen line contains the cell we want to highlight */
if (cc <= line_cols + width) {
CellStyle *orig = &l->cells[cc - 1 - line_cols].style;
orig->attr = style.attr;
orig->fg = is_default_color(style.fg) ? orig->fg : style.fg;
orig->bg = is_default_color(style.bg) ? orig->bg : style.bg;
cell_apply_style(&l->cells[cc - 1 - line_cols], &style, true);
line_cc_set = true;
} else {
line_cols += width;
Expand All @@ -343,10 +352,8 @@ static void window_draw_cursorline(Win *win) {
size_t lineno = view_cursors_line_get(sel)->lineno;
for (Line *l = view_lines_first(view); l; l = l->next) {
if (l->lineno == lineno) {
for (int x = 0; x < width; x++) {
l->cells[x].style.attr |= style.attr;
l->cells[x].style.bg = style.bg;
}
for (int x = 0; x < width; x++)
cell_apply_style(&l->cells[x], &style, true);
} else if (l->lineno > lineno) {
break;
}
Expand Down Expand Up @@ -376,21 +383,8 @@ static void window_draw_selection(View *view, Selection *cur, CellStyle *style)
for (Line *l = start_line; l != end_line->next; l = l->next) {
int col = (l == start_line) ? start_col : 0;
int end = (l == end_line) ? end_col : l->width;
while (col < end) {
if (cell_color_equal(l->cells[col].style.fg, style->bg)) {
CellStyle old = l->cells[col].style;
if (!cell_color_equal(old.fg, old.bg)) {
l->cells[col].style.fg = old.bg;
l->cells[col].style.bg = old.fg;
} else {
l->cells[col].style.attr = style->attr;
}
} else {
l->cells[col].style.bg = style->bg;
l->cells[col].style.fg = style->fg;
}
col++;
}
while (col < end)
cell_apply_style(&l->cells[col++], style, true);
}
}

Expand All @@ -405,14 +399,7 @@ static void window_draw_cursor_matching(Win *win, Selection *cur, CellStyle *sty
return;
if (!view_coord_get(win->view, pos_match, &line_match, NULL, &col_match))
return;
if (cell_color_equal(line_match->cells[col_match].style.fg, style->fg)) {
CellStyle old = line_match->cells[col_match].style;
line_match->cells[col_match].style.fg = old.bg;
line_match->cells[col_match].style.bg = old.fg;
} else {
line_match->cells[col_match].style.bg = style->bg;
line_match->cells[col_match].style.fg = style->fg;
}
cell_apply_style(&line_match->cells[col_match], style, true);
}

static void window_draw_cursor(Win *win, Selection *cur, CellStyle *style, CellStyle *sel_style) {
Expand All @@ -422,7 +409,7 @@ static void window_draw_cursor(Win *win, Selection *cur, CellStyle *style, CellS
int col = view_cursors_cell_get(cur);
if (!line || col == -1)
return;
line->cells[col].style = *style;
cell_apply_style(&line->cells[col], style, true);
}

static void window_draw_selections(Win *win) {
Expand Down

0 comments on commit cbea2a9

Please sign in to comment.