Skip to content

[FEATURE]: Add automatic contrast for secondary hover labels #8149

Open

Description

@Lexachoc

Description

Secondary hover labels inherit their text color from the trace color. However, light or low contrast trace colors can make the text difficult to read against the label background.

I propose using Color.wcagContrast() with a 4.5:1 threshold to automatically select a more readable text color.

Related to #2342 and #8115 (see the review comment: 1926941#r4383215739).

Before

Image

After

Image

Activity

  1. camdecoster commented on Oct 27, 2026

    @camdecoster
    Contributor

    Thanks for opening this. @KoolADE85 will follow up with you on this.

  2. KoolADE85 commented on Oct 27, 2026

    @KoolADE85
    Contributor

    I've been thinking about this one and how to avoid making the label just "black". It would be nice to have both the colour coding AND the contrast requirements.
    So, could we compare the label colour against the background colour and darken/lighten the label until it contrasts enough? In your screenshot, I envision the "Yellow Trace" label being a darker shade of yellow rather than black.
    I'd also want to ensure that we respect a user-supplied namecolor - even if it doesn't have good contrast.

  3. Lexachoc commented on Oct 28, 2026

    @Lexachoc
    ContributorAuthor

    So, could we compare the label colour against the background colour and darken/lighten the label until it contrasts enough? In your screenshot, I envision the "Yellow Trace" label being a darker shade of yellow rather than black.

    I think this is feasible. We could maybe use a fixed-iteration binary search (perhaps just <= 5 iterations) to find a lighter or darker shade that meets the contrast while preserving the original hue.

    for(var i = 0; i < 5; i++) {
        // Binary search
    }

    My only concern is the potential performance impact, since this calculation would run during hover rendering.

    I'd also want to ensure that we respect a user-supplied namecolor - even if it doesn't have good contrast.

    Agreed! This was already the behavior in my original PR #8115. The automatic contrast adjustment only applies when namecolor is not explicitly specified, so user-supplied color is always respected.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions