Skip to content

SVG output needs to be compatible with "dark mode", e.g. in docs #1150

Open

Description

@hyanwong

With the ability to switch from dark to light mode, we need the SVGs to work both ways too.

The shaded background in a tree seq viz is easy: we can just make it a mid grey with a suitable alpha value.

The difficulty is going to be the switching the lines from black to white. I guess this can be done using css somehow. At the moment it's hard-coded into the CSS embedded in the SVG. Perhaps we could override that somehow, but only for dark mode.

Edit: looks like currentColor might do it: https://hiddedevries.nl/en/blog/2018-12-24-making-single-color-svg-icons-work-in-dark-mode

Activity

  1. added
    enhancement
    Python API
    Visualisation
    on Jan 28, 2021
  2. added this to the Python upcoming milestone on Jan 29, 2021
  3. hyanwong commented on Jan 29, 2021

    @hyanwong
    MemberAuthor

    I tested currentColor. I can't get it to to work. Damn. @benjeffery suggested for ease of use we should simply pick a colour scheme for trees that works OK in both dark and light. Not sure how easy that's going to be. The other option is simply to put a white background behind all the SVGs, but that's a bit horrible. I don't know how one would normally detect being in dark mode anyway. So I'm a bit stuck here.

    Here's what I was hoping would work, but the lines are still black in Chrome dark mode on my Mac (chrome v 88.0.4324.96)

    <html><body>Here you go:
    <svg>
      <defs>
        <style type="text/css"><![CDATA[ path {stroke: currentColor; stroke-width: 4px; fill: none}]]>
        </style>
      </defs>
      <path d="M 10 10 V 80.0 H 31.875"/>
      <text transform="translate(15 15)">hello</text>
    </svg></body></html>
    
  4. hyanwong commented on Jan 29, 2021

    @hyanwong
    MemberAuthor

    Ha, so actually this does work, just not with the "force dark mode" in chrome (looks like that's being deprecated anyway, sorry @benjeffery ). If you switch the mode using the OS settings, it should work fine, I think. Unfortunately AFAIK not yet available in chromium/linux

    <!DOCTYPE html>
    <html>
    <head>
    <style type="text/css">
    
    @media (prefers-color-scheme: dark) {
      body {
        color: #eff;
        background: #121212;
      }
    
      body a {
        color: #809fff;
      }
    }
    
    </style>
    </head><body>
    <h1>SVG below: lines take on the current colour</h1>
    The line and "hello" text should be white when in dark mode, and black when in light mode
    
    <svg>
      <defs>
        <style type="text/css"><![CDATA[ path {stroke: currentColor; stroke-width: 4px; fill: none} text {fill: currentColor}]]>
        </style>
      </defs>
      <path d="M 10 10 V 80.0 H 31.875"/>
      <text transform="translate(15 15)">hello</text>
    </svg></body>
    </html>
    
  5. benjeffery commented on Feb 1, 2021

    @benjeffery
    Member

    Gah, what will I do!
    Thanks for looking into this - happy to have this solution.

  6. benjeffery commented on Jun 6, 2025

    @benjeffery
    Member

    So currentColor works but isn't in the code currently?

  7. hyanwong commented on Jun 6, 2025

    @hyanwong
    MemberAuthor

    Yes - it's not in there, I just tried swapping currentColor in, and it seems to work OK on my browser. Maybe we should swap all the "black" for "currentColor" in the stylesheet. I guess everything else can be left the same?

  8. benjeffery commented on Jun 8, 2025

    @benjeffery
    Member

    Looks like currentColor is well supported, can this easily be tweaked by the person generating the SVG?

  9. hyanwong commented on Jun 9, 2025

    @hyanwong
    MemberAuthor

    Well, they can override anything in the stylesheet, but maybe it's not obvious what that is. We should probably provide user access to an array of the styles, so it can be inspected?

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Python APIVisualisationenhancement

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions