cancel
Showing results forย 
Search instead forย 
Did you mean:ย 
Administration & Architecture
Explore discussions on Databricks administration, deployment strategies, and architectural best practices. Connect with administrators and architects to optimize your Databricks environment for performance, scalability, and security.
cancel
Showing results forย 
Search instead forย 
Did you mean:ย 

rich colors affected by recent dark mode update

mksmith521
New Contributor II

Hello,

I use rich Consoles to print DataFrame modification statistics, and I use dark mode. Up until a few days ago, Databricks handled dark mode by inverting rich's colors. That was fine, I just inverted the colors I wanted before passing them into rich. But Databricks recently changed their dark mode handling such that the colors aren't inverted but they are de-saturated or something. I've tried the recommended class="nodarkmode", but that doesn't completely fix it. For example, in the snippet below, the color bright_red is supposed to "#FF0000", but it is coming out something like "#C56E71". Is there anything else I can try?

from rich.console import Console
import os
import rich
from rich.table import Table


table = Table(style='bright_white on grey0')
table.add_column('ID')
table.add_column('age')

row = ['001', '30']
table.add_row(*row, style='bright_white on bright_red')

record_console = Console(record=True, file=open(os.devnull, 'w'), force_jupyter=False,)
record_console.print(table)
html = record_console.export_html(clear=False)
counter_inverted_html = f"""
<div class="nodarkmode">
    {html}
</div>
"""
displayHTML(counter_inverted_html)

 

1 ACCEPTED SOLUTION

Accepted Solutions

Louis_Frolio
Databricks Employee
Databricks Employee

Hello @mksmith521, I took a look at both internal and external documentation and here is what I found.

You're not imagining the change. The notebook appearance docs were updated on October 2 and now describe the new behavior: in dark mode, Databricks flips the light and dark tones of displayHTML output while keeping the original hues. DavidLin (Databricks) confirmed the same on an older Community thread that day. The old behavior was a plain inversion, which is its own inverse, so passing the complement color got you back where you wanted. A tone flip that preserves hue isn't reversible that way, so there's no color you can hand to rich that comes out as #FF0000. Pure red is a fairly dark tone in luminance terms, so the flip lifts and mutes it, and that's roughly what you're seeing. The shift itself is the feature working as designed.

Why nodarkmode isn't fully fixing it is the real question, and I can't see the implementation, so here's how I'd narrow it down. On that older thread, valentin9 found the old dark mode was a CSS filter (invert(1) saturate(0.5)) applied to the output iframe itself. If the new version works the same way, nodarkmode can only counter-correct inside an already-filtered frame, and a close-but-muted red is exactly what an imperfect counter-correction looks like. The other possibility is structural: rich's export_html() defaults to inline_styles=False, so your colors live in a <style> block with generated classes (.r1, .r2) rather than on the spans, and you're nesting a full HTML document inside your div. Either could keep the class from reaching the elements that carry the color.

A two-minute test separates the two. Run this with no rich involved:

displayHTML('<div class="nodarkmode" style="color:#FF0000;background:#000000">bright red test</div>')

If that comes out muted too, nothing you change in rich will fix it. I'd report it through support with this snippet, your cloud, browser and version, and whether the sun icon (below) restores the color. The docs say nodarkmode preserves original colors, so a muted result is a gap between the docs and the behavior.

If the bare div renders true red, the problem is in rich's HTML structure. Inline the styles and emit a fragment instead of a full document:

fragment_template = """<pre class="nodarkmode" style="font-family:Menlo,'DejaVu Sans Mono',monospace;color:{foreground};background-color:{background}"><code style="font-family:inherit">{code}</code></pre>"""

html = record_console.export_html(inline_styles=True, code_format=fragment_template)
displayHTML(html)

Keep the <pre> wrapper so the table's newlines and alignment survive. If the colors still shift, move the class onto the spans instead of the <pre> with html.replace('<span style=', '<span class="nodarkmode" style=').

Two stopgaps while you sort it out. Hover over the output and click the sun icon in the upper-right corner to see that one output in its original colors; it's per output and resets on reload. valentin9 also posted a script that re-inverts the frame when prefers-color-scheme: dark is set, but it was tuned to the old invert(1) saturate(0.5) filter and keys off your OS theme rather than the workspace theme, so I wouldn't expect it to line up with the October change.

References:

Regards, Louis.

View solution in original post

3 REPLIES 3

Louis_Frolio
Databricks Employee
Databricks Employee

Hello @mksmith521, I took a look at both internal and external documentation and here is what I found.

You're not imagining the change. The notebook appearance docs were updated on October 2 and now describe the new behavior: in dark mode, Databricks flips the light and dark tones of displayHTML output while keeping the original hues. DavidLin (Databricks) confirmed the same on an older Community thread that day. The old behavior was a plain inversion, which is its own inverse, so passing the complement color got you back where you wanted. A tone flip that preserves hue isn't reversible that way, so there's no color you can hand to rich that comes out as #FF0000. Pure red is a fairly dark tone in luminance terms, so the flip lifts and mutes it, and that's roughly what you're seeing. The shift itself is the feature working as designed.

Why nodarkmode isn't fully fixing it is the real question, and I can't see the implementation, so here's how I'd narrow it down. On that older thread, valentin9 found the old dark mode was a CSS filter (invert(1) saturate(0.5)) applied to the output iframe itself. If the new version works the same way, nodarkmode can only counter-correct inside an already-filtered frame, and a close-but-muted red is exactly what an imperfect counter-correction looks like. The other possibility is structural: rich's export_html() defaults to inline_styles=False, so your colors live in a <style> block with generated classes (.r1, .r2) rather than on the spans, and you're nesting a full HTML document inside your div. Either could keep the class from reaching the elements that carry the color.

A two-minute test separates the two. Run this with no rich involved:

displayHTML('<div class="nodarkmode" style="color:#FF0000;background:#000000">bright red test</div>')

If that comes out muted too, nothing you change in rich will fix it. I'd report it through support with this snippet, your cloud, browser and version, and whether the sun icon (below) restores the color. The docs say nodarkmode preserves original colors, so a muted result is a gap between the docs and the behavior.

If the bare div renders true red, the problem is in rich's HTML structure. Inline the styles and emit a fragment instead of a full document:

fragment_template = """<pre class="nodarkmode" style="font-family:Menlo,'DejaVu Sans Mono',monospace;color:{foreground};background-color:{background}"><code style="font-family:inherit">{code}</code></pre>"""

html = record_console.export_html(inline_styles=True, code_format=fragment_template)
displayHTML(html)

Keep the <pre> wrapper so the table's newlines and alignment survive. If the colors still shift, move the class onto the spans instead of the <pre> with html.replace('<span style=', '<span class="nodarkmode" style=').

Two stopgaps while you sort it out. Hover over the output and click the sun icon in the upper-right corner to see that one output in its original colors; it's per output and resets on reload. valentin9 also posted a script that re-inverts the frame when prefers-color-scheme: dark is set, but it was tuned to the old invert(1) saturate(0.5) filter and keys off your OS theme rather than the workspace theme, so I wouldn't expect it to line up with the October change.

References:

Regards, Louis.

Thank you so much @Louis_Frolio! I ran your first code snippet, and the bright red test came out muted red on a black background. Clicking the sun icon restored it to bright red on a black background. paint.net's color picker confirmed the muted color was real, not just a perception issue. I'll see if I can find where to report this to support. Thank you again!

Andreas_DBR
Databricks Employee
Databricks Employee

 

Thanks for the careful report, and for measuring the color with a picker. You're right: bright_red inside a
class="nodarkmode"
wrapper does not come back as FF0000. What you're seeing is a limitation of the CSS filter capabilities that browsers offer today.

Clamping in hue-rotate, a CSS limitation, mutes very saturated colors

In dark mode, notebook output is rendered through a filter chain of roughly
invert(1) hue-rotate(180deg)
plus a gamma step that lifts dark tones. The invert flips light and dark, the hue rotation turns the colors back to their original hue. CSS gives an element no way to opt out of a filter applied to its container, so nodarkmode works by applying the inverse filter and letting the two cancel out.

 

For most colors they do cancel out. For very saturated colors they can't, because the browser clamps every channel to the 0โ€“1 range after each filter step. This happens for strong color after the hue rotation. Pure red, once inverted to cyan and rotated back, would need a red channel of about 1.57. It gets clipped to 1, and that information is lost before the inverse filter runs. The gamma correction that follows can be reversed to within a single color channel step (1/255) of accuracy but the hue-rotate step that precedes it cannot.
 
This results in the C56E71 you measured. Strong blues and greens behave the same way. Mid-tones and pale colors round-trip fine, which works great for photos, as photographers intentionally stay away from saturating color channels in order to retain detail which is lost when large areas of a photo hit or exceed the maximum allowable color channel value.

To support users of both light and dark mode, we traded perfect accuracy for colors that are right in spirit

Before this change, dark mode simply inverted everything, so colors were negated: gold turned blue, red turned cyan, green turned magenta. For output where color carries meaning, such as charts, diagrams, highlighted rows, or an instructor in a training using a notebook and illustrating a point by saying "look at the gold box", that was the more serious problem for our users. The same instructor or presenter has users of both light and dark mode in their class, so both need to reproduce hues that correspond to similar color words. The same goes for conversations among colleagues about what is seen in a notebook, as they would in a meeting - if it a remote call and they are not looking at the same screen, they would misunderstand each other. This means we had to prioritize reproducing color in both modes simultaneously by intent over the ability to reproduce graphics with perfect color accuracy in only one of two modes.
 
With the filters available today, we had to weigh a slight shift in tone for extremely saturated colors protected with
class="nodarkmode"
against colors that are wrong outright. As described above, for mixed groups of users we believe the benefit of colors that are right in spirit (red stays red, green stays green) outweighs the benefit of colors that are bit-exact but false. I understand that this is not what you set out to achieve by measuring exact colors for your use case, but it was a deliberate decision, and it's the behavior we plan to keep until CSS is extended with filter functions that can either rotate hues reversibly or invert only luminance by itself without creating color negatives.

nodarkmode is intended for photographic images

nodarkmode is intended for photos and similar content that should look as close to the original as possible. Photos rarely contain extreme hues, so the clipping is almost never perceptible. For graphical output like tables, status colors, and diagrams, we recommend using colors authored for light mode without nodarkmode: you get consistent behavior in light and dark mode, including the contrast boost that should result in the right relationship between colors signaling specific messages (such as "green is good, red is rotten").

A suggestion for your table: author colors for light mode and let dark mode transform them

Instead of trying to pin exact colors, you might consider designing for light mode with pale tones and let dark mode turn them into stronger ones. A light background inverts into a dark one, and a pale color becomes a rich, saturated version of the same hue:
import os
import rich


table = rich.table.Table(style='black on #ffffff')
table.add_column('ID')
table.add_column('age')

row = ['001', '30']
table.add_row(*row, style='black on light_coral')

record_console = rich.console.Console(record=True, file=open(os.devnull, 'w'), force_jupyter=False,)
record_console.print(table)
html = record_console.export_html(clear=False)
displayHTML(html)
In light mode you get black text on a soft coral row, which the human eye can distinguish very well. In dark mode, the white table becomes the dark notebook background, the text turns light, and light_coral (FF8787) becomes a clear, strong red (about BD4545 before the gamma step).