From 07e0ce9f563ef9ae1d77ae39d14f796bfbd0936b Mon Sep 17 00:00:00 2001 From: Andrew Hamilton Date: Tue, 28 Jun 2022 23:14:49 +1000 Subject: [PATCH] editor: Fix highlighting lines on themes with bright backgrounds - For bright backgrounds brightening the line doesn't stand out, so darken the line instead. --- diff_edit/editor.py | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/diff_edit/editor.py b/diff_edit/editor.py index cfc3f25..367a16e 100755 --- a/diff_edit/editor.py +++ b/diff_edit/editor.py @@ -32,13 +32,6 @@ def highlight_str(line, bg_color, transparency=0.6): return termstr.TermStr(line).transform_style(blend_style) -def highlight_line(line): - return highlight_str(line, termstr.Color.white, 0.8) - - -NATIVE_STYLE = pygments.styles.get_style_by_name("paraiso-dark") - - @functools.lru_cache(maxsize=500) def parse_rgb(hex_rgb): if hex_rgb.startswith("#"): @@ -46,6 +39,21 @@ def parse_rgb(hex_rgb): return tuple(int("0x" + hex_rgb[index:index+2], base=16) for index in [0, 2, 4]) +@functools.cache +def is_bright_theme(theme): + return sum(parse_rgb(theme.background_color)) > (255 * 3 / 2) + + +def highlight_line(line, theme=None): + if theme is not None and is_bright_theme(theme): + return highlight_str(line, termstr.Color.black, 0.8) + else: + return highlight_str(line, termstr.Color.white, 0.8) + + +NATIVE_STYLE = pygments.styles.get_style_by_name("paraiso-dark") + + @functools.lru_cache(maxsize=500) def char_style_for_token_type(token_type, style): default_bg_color = parse_rgb(style.background_color) @@ -319,7 +327,8 @@ class Parts: appearance, coords = wrap_text(parts, width - 1, pad_char) line_num = coords[self.cursor][0] // (width - 1) if self.is_focused: - appearance[line_num] = highlight_line(appearance[line_num]) + appearance[line_num] = highlight_line(appearance[line_num], + self.editor.text_widget.theme) view_widget = fill3.View.from_widget(fill3.Fixed(appearance)) if line_num >= height: x, y = view_widget.portal.position @@ -329,7 +338,7 @@ class Parts: else: if self.is_focused: line_num = coords[self.cursor][0] // width - result[line_num] = highlight_line(result[line_num]) + result[line_num] = highlight_line(result[line_num], self.editor.text_widget.theme) fg_color = termstr.Color.grey_100 bg_color = parse_rgb(self.editor.text_widget.theme.background_color) result.append(termstr.TermStr("─").bg_color(bg_color).fg_color(fg_color) * width) @@ -440,7 +449,7 @@ class TextEditor: return appearance if self.mark is None: if 0 <= cursor_y < len(appearance): - appearance[cursor_y] = highlight_line(appearance[cursor_y]) + appearance[cursor_y] = highlight_line(appearance[cursor_y], self.text_widget.theme) else: self._highlight_selection(appearance) if self.cursor_x >= len(appearance[0]):