Neural Sketch

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):

first image
second image

Usage

Activating dark mode in neural-sketch is straightforward—just include the dark-mode option when loading the package in your preamble:

\usepackage[
  dark-mode = true 
]{neural-sketch}

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:

<base-color>!<percentage>[!<other-color>]

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.

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:

fill = nskRed!30, # Implicitly mixes with white

explicitly specify the background color to ensure predictable behavior:

outer-border-color = nskRed!100!nskBg,

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 \boldsymbol{\ell} and the dark background color d\mathbf{d}, based on the brightness of the original color c\mathbf{c}.

Precisely, let:

  • Original color c=(cr,cg,cb)\mathbf{c} = (c_r, c_g, c_b), where each channel cx[0,1]c_x \in [0,1].
  • Light background =(r,g,b)\boldsymbol{\ell} = (\ell_r, \ell_g, \ell_b).
  • Dark background d=(dr,dg,db)\mathbf{d} = (d_r, d_g, d_b).

Then the recolored output c\mathbf{c}' is calculated as:

c=+(1c)(d),\mathbf{c}' = \boldsymbol{\ell} + \bigl(1 - \mathbf{c}\bigr) \circ\bigl(\mathbf{d} - \boldsymbol{\ell}\bigr),

where ()(\circ) represents the Hadamard product (component-wise multiplication). Explicitly, for each color channel x{r,g,b}x \in \{r,g,b\}:

cx=x+(1cx)(dxx).c_x' = \ell_x + (1 - c_x)\,(d_x - \ell_x).

Intuition Behind the Formula

This formula ensures a smooth transition from a lighter to a darker context:

  • Brighter colors (values near 11) remain close to the defined light background, preserving their visual prominence.
  • Darker colors (values near 00) 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.

Neural-Sketch Dark Mode Recoloring

first image
second image

On this page