From fa2b08846bcdd93e1eea2cd3e3c0aac397452fdc Mon Sep 17 00:00:00 2001 From: Piotr Kocia Date: Tue, 17 Jun 2025 01:47:42 +0200 Subject: [PATCH] add custom highlight colors --- config.c | 16 ++-------------- config.h | 8 ++------ render.c | 22 ++++++++++++++++++---- terminal.c | 2 ++ terminal.h | 2 ++ tests/test-config.c | 6 ++---- 6 files changed, 28 insertions(+), 28 deletions(-) diff --git a/config.c b/config.c index be911adb..b0dda403 100644 --- a/config.c +++ b/config.c @@ -1434,6 +1434,8 @@ parse_color_theme(struct context *ctx, struct color_theme *theme) else if (streq(key, "background")) color = &theme->bg; else if (streq(key, "selection-foreground")) color = &theme->selection_fg; else if (streq(key, "selection-background")) color = &theme->selection_bg; + else if (streq(key, "highlight-foreground")) color = &theme->highlight_fg; + else if (streq(key, "highlight-background")) color = &theme->highlight_bg; else if (streq(key, "jump-labels")) { if (!value_to_two_colors( @@ -1505,20 +1507,6 @@ parse_color_theme(struct context *ctx, struct color_theme *theme) return true; } - else if (streq(key, "highlights")) { - if (!value_to_two_colors( - ctx, - &theme->highlights.fg, - &theme->highlights.bg, - false)) - { - return false; - } - - theme->use_custom.highlights = true; - return true; - } - else if (streq(key, "urls")) { if (!value_to_color(ctx, &theme->url, false)) return false; diff --git a/config.h b/config.h index 2c78aad0..0ba50651 100644 --- a/config.h +++ b/config.h @@ -140,6 +140,8 @@ struct color_theme { uint16_t alpha; uint32_t selection_fg; uint32_t selection_bg; + uint32_t highlight_fg; + uint32_t highlight_bg; uint32_t url; uint32_t dim[8]; @@ -183,11 +185,6 @@ struct color_theme { } match; } search_box; - struct { - uint32_t fg; - uint32_t bg; - } highlights; - struct { bool cursor:1; bool jump_label:1; @@ -195,7 +192,6 @@ struct color_theme { bool url:1; bool search_box_no_match:1; bool search_box_match:1; - bool highlights:1; uint8_t dim; } use_custom; }; diff --git a/render.c b/render.c index c41171fe..67f7d064 100644 --- a/render.c +++ b/render.c @@ -824,10 +824,24 @@ render_cell(struct terminal *term, pixman_image_t *pix, const bool is_highlighted = is_cell_highlighted(term, (struct coord){.row = row_no, .col = col}); if(is_highlighted) { - // TODO (kociap): Do the same thing as for selection colors. - if(term->conf->colors.use_custom.highlights) { - _fg = term->conf->colors.highlights.fg; - _bg = term->conf->colors.highlights.bg; + const uint32_t cell_fg = _fg; + const uint32_t cell_bg = _bg; + + const uint32_t hl_fg = term->colors.highlight_fg; + const uint32_t hl_bg = term->colors.highlight_bg; + const bool custom_fg = hl_fg >> 24 == 0; + const bool custom_bg = hl_bg >> 24 == 0; + const bool custom_both = custom_fg && custom_bg; + + if (custom_both) { + _fg = hl_fg; + _bg = hl_bg; + } else if (custom_bg) { + _bg = hl_bg; + _fg = cell->attrs.reverse ? cell_bg : cell_fg; + } else if (custom_fg) { + _fg = hl_fg; + _bg = cell->attrs.reverse ? cell_fg : cell_bg; } else { _fg = term->colors.table[0]; _bg = term->colors.table[1]; diff --git a/terminal.c b/terminal.c index a75c3593..7c039c8a 100644 --- a/terminal.c +++ b/terminal.c @@ -1323,6 +1323,8 @@ term_init(const struct config *conf, struct fdm *fdm, struct reaper *reaper, .cursor_bg = (theme->use_custom.cursor ? 1u << 31 : 0) | theme->cursor.cursor, .selection_fg = theme->selection_fg, .selection_bg = theme->selection_bg, + .highlight_fg = theme->highlight_fg, + .highlight_bg = theme->highlight_bg, .active_theme = conf->initial_color_theme, }, .color_stack = { diff --git a/terminal.h b/terminal.h index 1258ced3..8f0ad542 100644 --- a/terminal.h +++ b/terminal.h @@ -409,6 +409,8 @@ struct colors { uint32_t cursor_bg; /* cursor color */ uint32_t selection_fg; uint32_t selection_bg; + uint32_t highlight_fg; + uint32_t highlight_bg; enum which_color_theme active_theme; }; diff --git a/tests/test-config.c b/tests/test-config.c index 7147f668..6ade8f53 100644 --- a/tests/test-config.c +++ b/tests/test-config.c @@ -731,6 +731,8 @@ test_section_colors(void) test_color(&ctx, &parse_section_colors, "dim7", false, &conf.colors.dim[7]); test_color(&ctx, &parse_section_colors, "selection-foreground", false, &conf.colors.selection_fg); test_color(&ctx, &parse_section_colors, "selection-background", false, &conf.colors.selection_bg); + test_color(&ctx, &parse_section_colors, "highlight-foreground", false, &conf.colors.highlight_fg); + test_color(&ctx, &parse_section_colors, "highlight-background", false, &conf.colors.highlight_bg); test_color(&ctx, &parse_section_colors, "urls", false, &conf.colors.url); test_two_colors(&ctx, &parse_section_colors, "jump-labels", false, &conf.colors.jump_label.fg, @@ -744,10 +746,6 @@ test_section_colors(void) test_two_colors(&ctx, &parse_section_colors, "search-box-match", false, &conf.colors.search_box.match.fg, &conf.colors.search_box.match.bg); - test_two_colors(&ctx, &parse_section_colors, "highlights", false, - &conf.colors.highlights.fg, - &conf.colors.highlights.bg); - test_two_colors(&ctx, &parse_section_colors, "cursor", false, &conf.colors.cursor.text, &conf.colors.cursor.cursor);