Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix lines in kitty terminal with text_fg_override_threshold set #197

Merged
merged 3 commits into from
Nov 2, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions src/cli/hdcanvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,13 @@ impl Canvas {
}
}

// The kitty terminal has a feature text_fg_override_threshold that
// checks the difference in luminosity between text and background and
// changes the text to black or white to make it readable if the
// luminosity difference percentage is below the specified threshold.
// Using block characters for graphics display can trigger this, causing
// black or white lines or blocks, if the color is the same or too close.
// The checkerboard should be ok unless the theshold is set fairly high.
pub fn print(&self, out: &mut dyn Write) -> Result<()> {
for i_div_2 in 0..self.height / 2 {
for j in 0..self.width {
Expand All @@ -85,11 +92,21 @@ impl Canvas {
let p_bottom = self.pixel(2 * i_div_2 + 1, j);

match (p_top, p_bottom) {
(Some(top), Some(bottom)) => write!(
out,
"{}",
self.brush.paint("▀", top.ansi_style().on(bottom))
)?,
(Some(top), Some(bottom)) => {
if top == bottom {
write!(
out,
"{}",
self.brush.paint(" ", top.ansi_style().on(bottom))
)?
} else {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what about the else path? Isn't that still potentially causing problems if kitty changes colors?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Potentially, however I think it is only used in the checkerboard, which has a fairly high luminosity difference. I think kitty uses a simple percentage luminosity difference (I want to work on the compare functionality from #21 next and would like to add the value that kitty uses). I set it to 40 which seems high enough to avoid issues with text and the checkerboard still works. Maybe a comment about this potential issue would be appropriate to avoid it being used for something else in the future.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I just checked and 40 is the highest value of text_fg_override_threshold that the current checkerboard works with, so I guess that would be a potential reason to use the graphics protocol, although I'm not sure if anyone will want to set it higher than that. Does look a bit odd when it triggers.

write!(
out,
"{}",
self.brush.paint("▀", top.ansi_style().on(bottom))
)?
}
}
(Some(top), None) => write!(out, "{}", self.brush.paint("▀", top))?,
(None, Some(bottom)) => write!(out, "{}", self.brush.paint("▄", bottom))?,
(None, None) => write!(out, " ")?,
Expand Down