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

Cursor Color Contrast Logic is Too Sensitive #4564

Closed
Neightro opened this issue Dec 12, 2020 · 6 comments · Fixed by #5556
Closed

Cursor Color Contrast Logic is Too Sensitive #4564

Neightro opened this issue Dec 12, 2020 · 6 comments · Fixed by #5556

Comments

@Neightro
Copy link

I have a color specified for my cursor. When hovering over text that has the 'reverse' style, the cursor switches to be the inverse of the color underneath it. My understanding, as informed by @kchibisov in #2398, is that this is caused by a feature intended to keep the cursor itself from blending in with text that is very similar in color. However, this functionality kicks in in some situations where it likely shouldn't.

My alacritty.yml

alacritty.yml.txt

To Reproduce

I've found two ways of reproducing this. The first is, while running my config and any program that doesn't change the background color, use the mouse to select over the cursor, which should turn black despite not being similar in color to the highlighted text.

image

Alternatively, should that approach not work for some reason:

  1. Run vim -u NONE test.c
  2. Type the text int test
  3. Within Vim, run the command syn on | hi Type cterm=reverse gui=reverse
  4. Hover over the keyword int, which should be highlighted in lime green. The cursor will turn black, and the character underneath green. (Image is not very accurate, but I can't stop the window from unfocusing when taking a screenshot.)

image

Current Behavior

In many cases, the cursor changes color when hovering over text with the reverse style even if the color of the text is not similar to that of the cursor. My cursor is orange (#e19b14), though it will turn black over text that is green or teal, for instance.

This behavior has the side effect of making the cursor hard to see in many cases, such as when hovering over matching delimiters in Vim with 'matchparen'. This doesn't affect every color scheme, though; only ones using the 'reverse' style with a bright enough color.

Expected Behavior

The cursor only changes color when the background is very similar in color to the cursor itself, such as white on very light gray. Ideally this behavior would never happen with Vim's 'matchparen' highlighting or other one-character highlights, though that's likely a different issue.

System

Manjaro and Arch Linux
alacritty 0.6.0 (04cbf76)
X11 (xorg-server 1.20.10-0)

Please let me know if you need any more information, and I will be happy to oblige.

@chrisduerr
Copy link
Member

I mean #E19B14 will have extremely bad contrast with almost everything else out there, so I'm not sure what to do here since it seems to be doing its job. At least the contrast of that green to your cursor's yellow is extremely low.

What exactly do you want to change, just to lower the threshold? The current threshold is a contrast of 1.5, with your yellow on green being 1.3. The lowest possible value is 1.0, the highest is 21, while I think the lowest acceptable contrast for text according to the W3C is 3 with large text.

So I have very little doubt a contrast of 1.3 justifies switching out the colors. Even if we were to reduce it even more we'd have to use at most 1.2 for your example to work. That would accept #ff0000 and #dd0000 as having enough contrast, which I don't think I agree with.

@kchibisov what do you think?

@Neightro
Copy link
Author

Out of interest, what algorithm does Alacritty use to calculate contrast, and how much does it take hue into consideration? I agree that #ff0000 and #dd0000 should always invert the cursor, but I can't help wondering, should for example #e19b14 (orange) and #56b6c2 (teal), which are near complements, realistically do the same? It's possible that I'm overestimating the actual visibility between these two colors; just food for thought.

Truthfully, the reason I created this issue is that it's been noticed the cursor is sometimes hard to see when Vim highlights matching parens (#2398), and @kchibisov and I narrowed down the root cause to this contrast logic. Perhaps there's a better way to approach the problem than this.

@chrisduerr
Copy link
Member

Alacritty uses the W3C standard for color contrast: https://www.w3.org/TR/WCAG20/#contrast-ratiodef

@chrisduerr
Copy link
Member

Truthfully, the reason I created this issue is that it's been noticed the cursor is sometimes hard to see when Vim highlights matching parens (#2398)

If the contrast is too low, Alacritty will use inversion though. If that doesn't provide sufficient contrast, there's something wrong with your colors.

@AckslD
Copy link

AckslD commented Jul 6, 2021

@Neightro @chrisduerr I experimented a bit since I think I'm having the same issue (see also my comment here). It seems that whenever you set both text and cursor in colors: cursor then the cursor becomes invisible within search matches in vim. It doesn't matter what color you set them to. I try many different, having them different, the same etc. If you only set one of them then the cursor will be visible in some way but not the way, I want to fit the colorscheme.

Edit: After trying a bit more I can only say that I have no idea what's going on, at least the gruvbox scheme from here does not work:
https://github.com/alacritty/alacritty/wiki/Color-schemes

Edit: Changing to these colors seem to work:

colors:
  primary:
    # hard contrast background - '#1d2021'
    background:        &gruvbox_dark_bg '#282828'
    # soft contrast background - '#32302f'
    foreground:        '#ebdbb2'
    bright_foreground: '#fbf1c7'
    dim_foreground:    '#a89984'
  cursor:
    text: '#282828'
    cursor: '#ebdbb2'
  vi_mode_cursor:
    text: '#282828'
    cursor: '#ebdbb2'
  search:
    matches:
      foreground: '#282828'
      background: '#fabd2f'
    focused_match:
      foreground: '#282828'
      background: '#b57614'
    bar:
      foreground: '#282828'
      background: '#ebdbb2'
  line_indicator:
    foreground: '#282828'
    background: '#ebdbb2'
  selection:
    text:       CellForeground
    background: '#ebdbb2'
  bright:
    black:   '#928374'
    red:     '#fb4934'
    green:   '#b8bb26'
    yellow:  '#fabd2f'
    blue:    '#83a598'
    magenta: '#d3869b'
    cyan:    '#8ec07c'
    white:   '#ebdbb2'
  normal:
    black:   *gruvbox_dark_bg
    red:     '#cc241d'
    green:   '#98971a'
    yellow:  '#d79921'
    blue:    '#458588'
    magenta: '#b16286'
    cyan:    '#689d6a'
    white:   '#a89984'
  dim:
    black:   '#32302f'
    red:     '#9d0006'
    green:   '#79740e'
    yellow:  '#b57614'
    blue:    '#076678'
    magenta: '#8f3f71'
    cyan:    '#427b58'
    white:   '#928374'

@bagnaram
Copy link

bagnaram commented Jul 23, 2021

I am able to replicate this issue with the following snippet:

on=$(tput smso)"no color, standout on"`fb=3;r=255;g=85;b=85;printf '\e[0;%s8;2;%s;%s;%sm colored, standout on ' "$fb" "$r" "$g" "$b"`$(tput rmso)$(tput sgr0)
off="no color, standout off"`fb=3;r=255;g=85;b=85;printf '\e[0;%s8;2;%s;%s;%sm colored, standout off ' "$fb" "$r" "$g" "$b"`$(tput sgr0)
echo $off"\n"$on

It appears to be a problem when using standout under certain color combinations. This especially becomes an issue when using cursors with style standout, they disappear in certain colors.

chrisduerr added a commit to chrisduerr/alacritty that referenced this issue Oct 22, 2021
The existing cursor inversion logic was causing more problems than it
solved, without solving the problem of invisible cursor when inverting a
cell with matching foreground and background colors.

This patch reworks this logic and only inverts the cursor when the
foreground and background colors of the cursor are similar and the
cursor colors aren't set to fixed RGB values.

Fixes alacritty#4564.
Fixes alacritty#5550.
chrisduerr added a commit that referenced this issue Oct 22, 2021
The existing cursor inversion logic was causing more problems than it
solved, without solving the problem of invisible cursor when inverting a
cell with matching foreground and background colors.

This patch reworks this logic and only inverts the cursor when the
foreground and background colors of the cursor are similar and the
cursor colors aren't set to fixed RGB values.

Fixes #4564.
Fixes #5550.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging a pull request may close this issue.

5 participants
@chrisduerr @bagnaram @Neightro @AckslD and others