From 1dabaf88ef69d2f68edb3d23d19f1d88a8c3cb5f Mon Sep 17 00:00:00 2001 From: powellnorma <101364699+powellnorma@users.noreply.github.com> Date: Wed, 30 Sep 2026 09:04:10 +0200 Subject: [PATCH 1/2] Decode the color type byte for reverse-video extra cursors --- docs/changelog.rst | 3 +++ kitty/shaders/background.slang | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index a8c76765b..32190b879 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -202,6 +202,9 @@ Detailed list of changes 0.49.2 [future] ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +- Fix reverse-video extra cursors to use the color type encoded by the + multiple-cursors protocol. + - A new option :opt:`detect_url_regex` to detect arbitrary text, such as file references in compiler output, as clickable URLs under the mouse (:iss:`10407`) diff --git a/kitty/shaders/background.slang b/kitty/shaders/background.slang index c2e5d675e..f7c62018e 100644 --- a/kitty/shaders/background.slang +++ b/kitty/shaders/background.slang @@ -189,7 +189,7 @@ resolve_extra_cursor_colors(float3 cell_bg, float3 cell_fg, ColorPair main_curso resolve_dynamic_color(crd.extra_cursor_bg, crd.extra_cursor_bg_lin.rgb, main_cursor.bg, main_cursor.bg), resolve_dynamic_color(crd.extra_cursor_fg, crd.extra_cursor_fg_lin.rgb, cell_bg, main_cursor.fg)); ColorPair special = resolve_extra_cursor_colors_for_special_cursor(cell_bg, cell_fg); - return if_one_then_pair(zero_or_one(abs(float(crd.extra_cursor_bg & BYTE_MASK) - float(COLOR_IS_SPECIAL))), ans, special); + return if_one_then_pair(zero_or_one(abs(float((crd.extra_cursor_bg >> 24) & BYTE_MASK) - float(COLOR_IS_SPECIAL))), ans, special); } uint From 37e22f63000f77ff0c0860c6b0be7fd0f904352e Mon Sep 17 00:00:00 2001 From: powellnorma <101364699+powellnorma@users.noreply.github.com> Date: Wed, 30 Sep 2026 09:04:10 +0200 Subject: [PATCH 2/2] Make reverse-video cursors follow effective DIM colors --- docs/changelog.rst | 3 ++ kitty/options/definition.py | 9 +++--- kitty/shaders.c | 52 ++++++++++++++++++++++++---------- kitty/shaders/background.slang | 15 ++++++++-- kitty/shaders/cell.slang | 4 ++- 5 files changed, 61 insertions(+), 22 deletions(-) diff --git a/docs/changelog.rst b/docs/changelog.rst index 32190b879..e722c60a3 100644 --- a/docs/changelog.rst +++ b/docs/changelog.rst @@ -205,6 +205,9 @@ Detailed list of changes - Fix reverse-video extra cursors to use the color type encoded by the multiple-cursors protocol. +- Make reverse-video cursors follow dimmed text colors while keeping text under + block cursors legible. + - A new option :opt:`detect_url_regex` to detect arbitrary text, such as file references in compiler output, as clickable URLs under the mouse (:iss:`10407`) diff --git a/kitty/options/definition.py b/kitty/options/definition.py index 5f3e6c5a0..4ca42da34 100644 --- a/kitty/options/definition.py +++ b/kitty/options/definition.py @@ -357,10 +357,11 @@ opt( Default text cursor color. If set to the special value :code:`none` the cursor will be rendered with a "reverse video" effect. Its color will be the color of the text in the cell it is over and the text will be rendered with the background -color of the cell. Note that if the program running in the terminal sets a -cursor color, this takes precedence. Also, the cursor colors are modified if -the cell background and foreground colors have very low contrast. Note that some -themes set this value, so if you want to override it, place your value after +color of the cell, including its effective DIM/FAINT foreground color. Note +that if the program running in the terminal sets a cursor color, this takes +precedence. Also, the cursor colors are modified if the cell background and +foreground colors have very low contrast. Note that some themes set this value, so if you want to +override it, place your value after the lines where the theme file is included. """, ) diff --git a/kitty/shaders.c b/kitty/shaders.c index f38d81549..d80c0987d 100644 --- a/kitty/shaders.c +++ b/kitty/shaders.c @@ -801,24 +801,47 @@ create_cell_vao(void) { (screen->color_profile->overridden.name.type == COLOR_IS_SPECIAL || \ (screen->color_profile->overridden.name.type == COLOR_NOT_SET && screen->color_profile->configured.name.type == COLOR_IS_SPECIAL)) +static float +cursor_color_luminance(color_type color) { + // Match color_to_vec() and contrast_ratio() in background.slang. + return 0.2126f * srgb_lut[(color >> 16) & 0xff] + 0.7152f * srgb_lut[(color >> 8) & 0xff] + 0.0722f * srgb_lut[color & 0xff]; +} + +static float +cursor_color_contrast(color_type a, color_type b) { + const float la = cursor_color_luminance(a), lb = cursor_color_luminance(b); + return (MAX(la, lb) + 0.05f) / (MIN(la, lb) + 0.05f); +} + static void pick_cursor_color(color_type cell_fg, color_type cell_bg, color_type *cursor_fg, color_type *cursor_bg, color_type default_fg, color_type default_bg) { - ARGB32 fg, bg, dfg, dbg; - fg.rgb = cell_fg; - bg.rgb = cell_bg; *cursor_fg = cell_bg; *cursor_bg = cell_fg; - double cell_contrast = rgb_contrast(fg, bg); + const float cell_contrast = cursor_color_contrast(cell_fg, cell_bg); if (cell_contrast < 2.5) { - dfg.rgb = default_fg; - dbg.rgb = default_bg; - if (rgb_contrast(dfg, dbg) > cell_contrast) { + if (cursor_color_contrast(default_fg, default_bg) > cell_contrast) { *cursor_fg = default_bg; *cursor_bg = default_fg; } } } +static uint8_t +dimmed_color_component(uint8_t fg, uint8_t bg, float opacity) { + // DIM blends text with the cell background in linear color space. + const float linear = (1.f - opacity) * srgb_lut[bg] + opacity * srgb_lut[fg]; + const float srgb = linear <= 0.0031308f ? 12.92f * linear : 1.055f * powf(linear, 1.f / 2.4f) - 0.055f; + return (uint8_t)lroundf(255.f * srgb); +} + +static color_type +dimmed_cursor_color(color_type fg, color_type bg, float opacity) { + if (opacity >= 1.f) return fg; + return ((color_type)dimmed_color_component((fg >> 16) & 0xff, (bg >> 16) & 0xff, opacity) << 16) | + ((color_type)dimmed_color_component((fg >> 8) & 0xff, (bg >> 8) & 0xff, opacity) << 8) | + (color_type)dimmed_color_component(fg & 0xff, bg & 0xff, opacity); +} + static bool has_bgimage(OSWindow *w) { return background_image_for_os_window(w) != NULL; @@ -855,7 +878,7 @@ cell_update_uniform_block( GLfloat use_cell_bg_for_selection_fg, use_cell_fg_for_selection_color, use_cell_for_selection_bg; - GLuint default_fg, url_style, inverted, extra_cursor_fg, extra_cursor_bg; + GLuint default_fg, url_style, inverted, main_cursor_is_special, extra_cursor_fg, extra_cursor_bg; GLuint columns, lines, sprites_xnum, sprites_ynum, cursor_shape, cell_width, cell_height; GLuint cursor_x1, cursor_x2, cursor_y1, cursor_y2; @@ -893,6 +916,7 @@ cell_update_uniform_block( rd->default_fg = COLOR(default_fg); rd->extra_cursor_fg = screen->extra_cursors.color.text.val; rd->extra_cursor_bg = screen->extra_cursors.color.cursor.val; + rd->main_cursor_is_special = IS_SPECIAL_COLOR(cursor_color); rd->bg_colors0 = COLOR(default_bg); rd->bg_opacities0 = bg_alpha; rd->fg_override_threshold = OPT(text_fg_override_threshold); @@ -972,13 +996,11 @@ cell_update_uniform_block( } } // If you change the following algorithm remember to change it in the cell shader for extra cursors too - if (IS_SPECIAL_COLOR(cursor_color)) { - if (line_for_cursor) pick_cursor_color(cell_fg, cell_bg, &main_cursor_fg, &main_cursor_bg, rd->default_fg, rd->bg_colors0); - else { - main_cursor_fg = rd->bg_colors0; - main_cursor_bg = rd->default_fg; - } - if (cell_bg == cell_fg) { + if (rd->main_cursor_is_special) { + if (line_for_cursor && line_for_cursor->gpu_cells[cell_color_x].attrs.dim) { + cell_fg = dimmed_cursor_color(cell_fg, cell_bg, OPT(dim_opacity)); + pick_cursor_color(cell_fg, cell_bg, &main_cursor_fg, &main_cursor_bg, rd->default_fg, rd->bg_colors0); + } else if (cell_bg == cell_fg) { main_cursor_fg = rd->bg_colors0; main_cursor_bg = rd->default_fg; } else { diff --git a/kitty/shaders/background.slang b/kitty/shaders/background.slang index f7c62018e..cdd598be1 100644 --- a/kitty/shaders/background.slang +++ b/kitty/shaders/background.slang @@ -30,7 +30,7 @@ public struct CellRenderData { // default_fg and bg_colors0 are also needed as integers, to resolve cell // colors and to detect cells using the default background. - public uint default_fg, url_style, inverted, extra_cursor_fg, extra_cursor_bg; + public uint default_fg, url_style, inverted, main_cursor_is_special, extra_cursor_fg, extra_cursor_bg; public uint columns, lines, sprites_xnum, sprites_ynum, cursor_shape, cell_width, cell_height; public uint cursor_x1, cursor_x2, cursor_y1, cursor_y2; @@ -218,6 +218,7 @@ calc_background_opacity(uint bg) { public struct CellData { public float has_cursor; public float has_block_cursor; + public float reverse_video_cursor; public uint2 pos; public uint cursor_fg_sprite_idx; public ColorPair cursor; @@ -296,12 +297,22 @@ compute_background(uint3 colors, uint2 sprite_idx, uint is_selected, uint instan main_cursor.bg = crd.main_cursor_bg_lin.rgb; main_cursor.fg = crd.main_cursor_fg_lin.rgb; - ColorPair extra_cursor = resolve_extra_cursor_colors(cell_bg, foreground, main_cursor); + float has_dim = float((text_attrs >> DIM_SHIFT) & BIT_MASK); + float3 displayed_foreground = if_one_then(has_dim, lerp(cell_bg, foreground, crd.dim_opacity), foreground); + ColorPair extra_cursor = resolve_extra_cursor_colors(cell_bg, displayed_foreground, main_cursor); ColorPair cursor = if_one_then_pair(has_main_cursor, main_cursor, extra_cursor); + uint extra_cursor_bg_type = (crd.extra_cursor_bg >> 24) & BYTE_MASK; + uint extra_cursor_fg_type = (crd.extra_cursor_fg >> 24) & BYTE_MASK; + float explicit_extra_reverse_video = float(one_if_equal_zero_otherwise(extra_cursor_bg_type, COLOR_IS_SPECIAL)); + float inherited_reverse_video = float(crd.main_cursor_is_special) * float(one_if_equal_zero_otherwise(extra_cursor_bg_type, COLOR_NOT_SET)) * + float(one_if_equal_zero_otherwise(extra_cursor_fg_type, COLOR_NOT_SET)); + CellData cell_data; cell_data.has_cursor = has_cursor; cell_data.has_block_cursor = is_block_cursor; + cell_data.reverse_video_cursor = + if_one_then(has_main_cursor, float(crd.main_cursor_is_special), max(explicit_extra_reverse_video, inherited_reverse_video)); cell_data.pos = pos; cell_data.cursor_fg_sprite_idx = cursor_shape_map[int(final_cursor_shape)]; cell_data.cursor = cursor; diff --git a/kitty/shaders/cell.slang b/kitty/shaders/cell.slang index 4cb89deae..967033025 100644 --- a/kitty/shaders/cell.slang +++ b/kitty/shaders/cell.slang @@ -161,7 +161,9 @@ vertex_main( vo.colored_sprite = float((sprite_idx[0] & SPRITE_COLORED_MASK) >> SPRITE_COLORED_SHIFT); float has_dim = float((text_attrs >> DIM_SHIFT) & BIT_MASK), has_blink = float((text_attrs >> BLINK_SHIFT) & BIT_MASK); - vo.effective_text_alpha = crd.inactive_text_alpha * if_one_then(has_dim, crd.dim_opacity, 1.0) * if_one_then(has_blink, crd.blink_opacity, 1.0); + float dim_under_reverse_cursor = has_dim * (1.0 - cell_data.has_block_cursor * cell_data.reverse_video_cursor); + vo.effective_text_alpha = + crd.inactive_text_alpha * if_one_then(dim_under_reverse_cursor, crd.dim_opacity, 1.0) * if_one_then(has_blink, crd.blink_opacity, 1.0); float in_url = float((is_selected >> 1) & BIT_MASK); vo.decoration_fg = if_one_then(in_url, crd.url_color_lin.rgb, to_color(colors[2], fg_as_uint)); // Selection