From 50868caece49559ed9d2b7684bf4d96ebe0a0e91 Mon Sep 17 00:00:00 2001 From: Joschua Kesper Date: Sun, 26 May 2024 21:37:57 +0200 Subject: [PATCH] config: add colors.alpha-non-whitespace option --- CHANGELOG.md | 2 ++ config.c | 23 +++++++++++++++++++++++ config.h | 1 + doc/foot.ini.5.scd | 6 ++++++ foot.ini | 1 + render.c | 5 ++++- terminal.c | 1 + terminal.h | 1 + 8 files changed, 39 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 10ed8876..c1446904 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -60,6 +60,8 @@ * Support for `wp_single_pixel_buffer_v1`; certain overlay surfaces will now utilize the new single-pixel buffer protocol. This mainly reduces the memory usage, but should also be slightly faster. +* `alpha-non-whitespace` option, allowing for adjustment of + transparency only for non whitespace characters. [1707]: https://codeberg.org/dnkl/foot/issues/1707 diff --git a/config.c b/config.c index b7bc1f09..dff0f549 100644 --- a/config.c +++ b/config.c @@ -1340,6 +1340,20 @@ parse_section_colors(struct context *ctx) return true; } + else if (streq(key, "alpha-non-whitespace")) { + float alpha; + if (!value_to_float(ctx, &alpha)) + return false; + + if (alpha < 0. || alpha > 1.) { + LOG_CONTEXTUAL_ERR("not in range 0.0-1.0"); + return false; + } + + conf->colors.alpha_non_whitespace = alpha * 65535.; + return true; + } + else if (streq(key, "flash-alpha")) { float alpha; if (!value_to_float(ctx, &alpha)) @@ -2854,6 +2868,9 @@ parse_config_file(FILE *f, struct config *conf, const char *path, bool errors_ar errno = 0; } + /* Set `alpha_non_whitespace` to atleast `alpha`. */ + conf->colors.alpha_non_whitespace = max(conf->colors.alpha_non_whitespace, conf->colors.alpha); + if (errno != 0) { LOG_AND_NOTIFY_ERRNO("failed to read from configuration"); if (errors_are_fatal) @@ -3092,6 +3109,12 @@ config_load(struct config *conf, const char *conf_path, .flash = 0x7f7f00, .flash_alpha = 0x7fff, .alpha = 0xffff, + /* HACK: + * when `alpha_non_whitespace` is not set its + * default value will be `alpha`. This is achived via + * restricting it to be at least the value of `alpha` + */ + .alpha_non_whitespace = 0x0000, .selection_fg = 0x80000000, /* Use default bg */ .selection_bg = 0x80000000, /* Use default fg */ .use_custom = { diff --git a/config.h b/config.h index e2d94507..12333e58 100644 --- a/config.h +++ b/config.h @@ -220,6 +220,7 @@ struct config { uint32_t flash_alpha; uint32_t table[256]; uint16_t alpha; + uint16_t alpha_non_whitespace; uint32_t selection_fg; uint32_t selection_bg; uint32_t url; diff --git a/doc/foot.ini.5.scd b/doc/foot.ini.5.scd index 93a3120c..6e77900b 100644 --- a/doc/foot.ini.5.scd +++ b/doc/foot.ini.5.scd @@ -647,6 +647,12 @@ can configure the background transparency with the _alpha_ option. Background translucency. A value in the range 0.0-1.0, where 0.0 means completely transparent, and 1.0 is opaque. Default: _1.0_. +*alpha-non-whitespace* + Background translucency for non whitespace characters. + A value in the range 0.0-1.0, where 0.0 means completely + transparent, and 1.0 is opaque. Default: The value of 0.0. + If this value is lower than _alpha_, _alpha_ will be used instead. + *selection-foreground*, *selection-background* Foreground (text) and background color to use in selected text. Note that *both* options must be set, or the default will be diff --git a/foot.ini b/foot.ini index f4f104ae..79180ec2 100644 --- a/foot.ini +++ b/foot.ini @@ -77,6 +77,7 @@ [colors] # alpha=1.0 +# alpha-non-whitespace=1.0 # background=242424 # foreground=ffffff # flash=7f7f00 diff --git a/render.c b/render.c index fd7e743a..91431c71 100644 --- a/render.c +++ b/render.c @@ -2,6 +2,7 @@ #include #include +#include #include #include @@ -572,8 +573,10 @@ render_cell(struct terminal *term, pixman_image_t *pix, pixman_region32_t *damag * NOTE: if changing this, also update render_margin() */ xassert(alpha == 0xffff); - } else { + } else if (isspace(cell->wc) || cell->wc == 0) { alpha = term->colors.alpha; + } else { + alpha = term->colors.alpha_non_whitespace; } } } diff --git a/terminal.c b/terminal.c index 55c387c1..826371c1 100644 --- a/terminal.c +++ b/terminal.c @@ -1221,6 +1221,7 @@ term_init(const struct config *conf, struct fdm *fdm, struct reaper *reaper, .fg = conf->colors.fg, .bg = conf->colors.bg, .alpha = conf->colors.alpha, + .alpha_non_whitespace = conf->colors.alpha_non_whitespace, .selection_fg = conf->colors.selection_fg, .selection_bg = conf->colors.selection_bg, .use_custom_selection = conf->colors.use_custom.selection, diff --git a/terminal.h b/terminal.h index 5033c550..3378cf06 100644 --- a/terminal.h +++ b/terminal.h @@ -516,6 +516,7 @@ struct terminal { uint32_t bg; uint32_t table[256]; uint16_t alpha; + uint16_t alpha_non_whitespace; uint32_t selection_fg; uint32_t selection_bg; bool use_custom_selection;