diff options
author | Christian Duerr <contact@christianduerr.com> | 2020-07-26 01:04:39 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-26 01:04:39 +0000 |
commit | bedf5f3004e8f33011925ca471be02ead96f4581 (patch) | |
tree | 0512bbd51514e5bca8be3b1f11cadffaa2eae113 /alacritty_terminal/src/term/mod.rs | |
parent | 9a4d847d897016ac3f1662fec45d372b879072cd (diff) | |
download | alacritty-bedf5f3004e8f33011925ca471be02ead96f4581.tar.gz alacritty-bedf5f3004e8f33011925ca471be02ead96f4581.zip |
Invert fixed color cursor if it's close to cell bg
This should reduce the number of times people with fixed cursor colors
run into troubles when existing text is already colored.
Using just the background color as a metric instead of both background
and foreground color should ensure that the cursor still has a clear
shape, since just changing the foreground color for a cursor might be
difficult to see. Always inverting the entire cursor instead of keeping
the fixed foreground color is important to make sure the contrast isn't
messed up.
Fixes #4016.
Diffstat (limited to 'alacritty_terminal/src/term/mod.rs')
-rw-r--r-- | alacritty_terminal/src/term/mod.rs | 21 |
1 files changed, 18 insertions, 3 deletions
diff --git a/alacritty_terminal/src/term/mod.rs b/alacritty_terminal/src/term/mod.rs index f27852de..7d00961e 100644 --- a/alacritty_terminal/src/term/mod.rs +++ b/alacritty_terminal/src/term/mod.rs @@ -31,6 +31,9 @@ mod search; /// Max size of the window title stack. const TITLE_STACK_MAX_DEPTH: usize = 4096; +/// Minimum contrast between a fixed cursor color and the cell's background. +const MIN_CURSOR_CONTRAST: f64 = 1.5; + /// Maximum number of linewraps followed outside of the viewport during search highlighting. const MAX_SEARCH_LINES: usize = 100; @@ -389,13 +392,19 @@ impl<'a, C> Iterator for RenderableCellsIter<'a, C> { if self.cursor.point.line == self.inner.line() && self.cursor.point.col == self.inner.column() { - // Handle cell below cursor. if self.cursor.rendered { + // Handle cell below cursor. let cell = self.inner.next()?; let mut cell = RenderableCell::new(self, cell); if self.cursor.key.style == CursorStyle::Block { - cell.fg = self.cursor.text_color.color(cell.fg, cell.bg); + // Invert cursor if static background is close to the cell's background. + match self.cursor.cursor_color { + CellRgb::Rgb(col) if col.contrast(cell.bg) >= MIN_CURSOR_CONTRAST => { + cell.fg = self.cursor.text_color.color(cell.fg, cell.bg); + }, + _ => cell.fg = cell.bg, + } } return Some(cell); @@ -412,7 +421,13 @@ impl<'a, C> Iterator for RenderableCellsIter<'a, C> { let mut cell = RenderableCell::new(self, cell); cell.inner = RenderableCellContent::Cursor(self.cursor.key); - cell.fg = self.cursor.cursor_color.color(cell.fg, cell.bg); + + // Only apply static color if it isn't close to the cell's current background. + if let CellRgb::Rgb(color) = self.cursor.cursor_color { + if color.contrast(cell.bg) >= MIN_CURSOR_CONTRAST { + cell.fg = self.cursor.cursor_color.color(cell.fg, cell.bg); + } + } return Some(cell); } |