Dark Mode
Seamless dark-mode support with automatic color inversion for diagrams
Overview
neural-sketch
provides built-in support for a seamless dark-mode experience, automatically adjusting colors to remain legible, balanced, and aesthetically harmonious on dark backgrounds. By simply enabling the package option dark-mode=true
, all predefined colors are intelligently re-mapped, ensuring your diagrams remain clear and visually appealing in dark-themed PDFs, slides, or web content.
Quick Example
Below, hover over (or tap) the comparison to experience the automatic transformation from a standard diagram (light mode) to its optimized counterpart (dark mode):
Usage
Activating dark mode in neural-sketch
is straightforward—just include the dark-mode
option when loading the package in your preamble:
That’s it! From this point forward, neural-sketch
automatically handles color transformations, ensuring your diagrams effortlessly adapt to dark-themed presentations, websites, or PDFs without any additional configuration.
The dark-mode
global option is specifically intended for standalone or
externalized figures. It adjusts the page background color directly before
compilation, which may interfere with the overall document style if used
globally. Consider isolating this setting to figure-only compilations.
How It Works
Neural-Sketch implements a two-point color interpolation method, automatically adapting each color between predefined light and dark backgrounds. While some colors rely on this interpolation by default, both endpoints—as well as individually inverted color versions—can be completely customized to fit your specific design requirements.
For a full overview of customizable colors and their inverse versions, refer to the Color Reference.
Customizing Dark Mode Colors
You retain complete control over both the standard and inverted colors. Specifically, each color c__nsk_<*>_clr
and its inverse counterpart c__nsk_<*>_inv_clr
can be individually adjusted, ensuring precise control over your color scheme under both light and dark modes.
Moreover, the interpolation process itself can be customized by simply specifying two reference points:
- Light background (
light_bg
): Typically white or a very pale color. - Dark background (
dark_bg
): Typically a very dark grey or near-black tone.
By defining these endpoints, the package ensures colors interpolate gracefully and predictably, preserving visual coherence.
Color Mixing in Dark Mode
When mixing colors (see Color Mixing and Blending), recall that the default syntax:
implicitly mixes the <base-color>
with white, creating a pastel, opacity-like effect. While effective in light-themed diagrams, this approach can yield unexpected results after automatic dark-mode recoloring, as colors no longer correctly match the altered background.
Recommended Mixing Strategy
For robust diagrams that consistently look correct across both light and dark modes, explicitly use special placeholder colors provided by neural-sketch:
nskBg
: Represents the current background color (typically page background).nskFg
: Represents the current foreground color (typically text color).
Example
Instead of relying on default mixing:
explicitly specify the background color to ensure predictable behavior:
This ensures the mixed color dynamically adapts, maintaining consistent appearance across both themes.
In the experimental
branch, correct background-based mixing is applied
automatically unless explicitly specified otherwise.
Recoloring Step
The recoloring procedure is defined mathematically through linear interpolation between the light background color and the dark background color , based on the brightness of the original color .
Precisely, let:
- Original color , where each channel .
- Light background .
- Dark background .
Then the recolored output is calculated as:
where represents the Hadamard product (component-wise multiplication). Explicitly, for each color channel :
Intuition Behind the Formula
This formula ensures a smooth transition from a lighter to a darker context:
- Brighter colors (values near ) remain close to the defined light background, preserving their visual prominence.
- Darker colors (values near ) smoothly shift towards the dark background, ensuring they remain clearly visible against a darker canvas.
The result is visually intuitive and maintains excellent readability across theme variations.