Skip to main content

Custom CSS that inverts the colors of images when a dark theme is toggled.

Project description

Image Inverter

Introduction

When toggling dark mode in JupyterBook, images and figures are not inverted by default, but a white background is inserted. However, this white background might not always be desired in dark mode.

The Sphinx-Image-Inverter extension provides a solution by applying an automatic filter to images and iframes. If this filter is not desired for certain items, the Sphinx-Image-Inverter extension provides a solution by allowing selective disabling using the dark-light class.

How does it work?

This Sphinx extension applies a filter such that dark and light colors are switched, however keeps the colours recognizable. This is particularly useful for graphs in which a certain colour is mentioned in accompanying text. Items are not converted if they are marked with the dark-light class (recommended for example for photos).

In more detail, first the colors of the element are inverted, then the hue of the colors is shifted by 180 degrees, so the inverted colors change to their complementary hues. This flips the brightness and contrast, while keeping the hue somewhat recognizable (so a blue line will be a blue line in both ligth and dark mode). Black and white stay inverted (so white becomes black, and black becomes white), because they don’t have a hue. Next, the colors are slightly saturated to enforce a better contrast. After this, the element blends with the background, making similar colors appear dark and very different colors appear bright. The overall effect creates high contrast between the element and the background, depending on their colors.

Installation

To install the Sphinx-Image-Inverter, follow these steps:

Step 1: Install the Package

Install the sphinx-image-inverter package using pip:

pip install sphinx-image-inverter

Step 2: Add to requirements.txt

Make sure that the package is included in your project's requirements.txt to track the dependency:

sphinx-image-inverter

Step 3: Enable in _config.yml

In your _config.yml file, add the extension to the list of Sphinx extra extensions:

sphinx: 
    extra_extensions:
        - sphinx_image_inverter

Usage

Disable Image/Figure Inversion

By default, when dark-mode is toggled in JupyterBook, all images and figures are inverted. To prevent certain images from being inverted, apply the dark-light class. The steps for both Markdown and HTML formats are given below.

For Markdown Format

  1. Locate the markdown file that contains the image or figure you want to exclude from inversion.

  2. Add the :class: dark-light attribute to the figure directive.

    Example:

    ```{figure} example_folder/example_image.jpg
    :class: dark-light
    :width: 400```
    

For HTML Format

If your image or figure is defined using HTML, apply the dark-light class directly to the tag.

<iframe 
    src="some_figure.html" 
    width="600" 
    height="300" 
    class="dark-light">
</iframe>

Done! Now your image will not be inverted when dark mode is toggled.

Display Text According to Theme

You may want to display different text depending on whether the dark mode or light mode is enabled. To do that, you can use the following classes:

  • Light Mode only:
<span class="only-light">Text only visible in Light Mode.</span>
  • Dark Mode only:
<span class="only-dark">Text only visible in Dark Mode.</span>

These classes make sure that your text is only visible in the specified modes.

Compatible LaTeX colours

If you'd like to use LaTeX colours which invert similarly, use the approach Sphinx extension Sphinx-Named-Colors.

Contribute

This tool's repository is stored on GitHub. The README.md of the branch Manual is also part of the TeachBooks manual as a submodule. If you'd like to contribute, you can create a fork and open a pull request on the GitHub repository. To update the README.md shown in the TeachBooks manual, create a fork and open a merge request for the GitHub repository of the manual. If you intent to clone the manual including its submodules, clone using: git clone --recurse-submodulesgit@github.com:TeachBooks/manual.git.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sphinx_image_inverter-1.0.3.tar.gz (6.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

sphinx_image_inverter-1.0.3-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file sphinx_image_inverter-1.0.3.tar.gz.

File metadata

  • Download URL: sphinx_image_inverter-1.0.3.tar.gz
  • Upload date:
  • Size: 6.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for sphinx_image_inverter-1.0.3.tar.gz
Algorithm Hash digest
SHA256 ecb48fd483f4698491d7b906bb97f2ee1cbee769760aa2390faed10f683c7e88
MD5 98401b792efaf3c0456f20e8d4a4d250
BLAKE2b-256 6880773510ac4f7baca9e1874c22a22b7a1421e208221cd805200ead281bc51d

See more details on using hashes here.

Provenance

The following attestation bundles were made for sphinx_image_inverter-1.0.3.tar.gz:

Publisher: python-publish.yml on TeachBooks/Sphinx-Image-Inverter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sphinx_image_inverter-1.0.3-py3-none-any.whl.

File metadata

File hashes

Hashes for sphinx_image_inverter-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 482f8536c9e374a9ac8e131d1901960d4c3c69f79d3c8de5df8bfc4bd8b2354f
MD5 537ebde1e364461cc99aadb2078face0
BLAKE2b-256 a1bed3d4bc2ce5a6b7d2a58915f6f82a9f85a5d0cb094b725e804b99768fda32

See more details on using hashes here.

Provenance

The following attestation bundles were made for sphinx_image_inverter-1.0.3-py3-none-any.whl:

Publisher: python-publish.yml on TeachBooks/Sphinx-Image-Inverter

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page