Skip to main content

A Sphinx extension to add support for colorschemed images.

Project description

Color schemed images for Sphinx tests

An extension for Sphinx that adds support for color-scheme aware images. This README.md is also used for the NodeJS package. The NodeJS package contains only the JavaScript module. See below the paragraph about the JavaScript plugin.

Tested

Description

Sphinx Color Schemed Images is an extension that makes available two new image directives to your project:

  • cs_image extends the image directive, and
  • cs_figure extends the figure directive.

These new directives add data attributes to the HTML <img> tag to help web browsers to automatically transition between light and dark color schemes. The extension adds a bit of JavaScript code to trigger an image update when the user switches the color scheme.

Usage

Install the package:

pip install sphinx-colorschemed-images

Add sphinx_colorschemed_images to the extensions setting, in your project's conf.py file.

As an example, download the following files and place them in the source directory of your Sphinx project, inside the image directory. I assume the sources are in the docs/ directory, and the image directory is docs/img/:

Now edit your index.rst or index.md file, and add a cs_image directive. For index.rst, add:

.. cs_image:: img/balloon.png
   :alt: A balloon icon
   :align: center
   :width: 200

If it is an index.md, add the following instead:

```{cs_image} img/balloon.png
:alt: A balloon icon
:align: center
:width: 200
```

Build your Sphinx project and serve it. You should see either the image for the light color scheme, balloon.light.png, or the image for the dark color scheme, balloon.dark.png. Switch your operating system settings and the image should update automatically.

JavaScript plugin

When using the extension Sphinx adds a script to your HTML output, sphinx-colorschemed-images.js, that listens for changes on the user's preferred color-scheme and switches between the images accordingly. It works in all Sphinx themes regardless of whether they have support for light/dark color schemes.

If you have your own theme and it offers the user control over the color-scheme, you can use the NPM package sphinx-colorschemed-images and its class SphinxColorschemeImageHandler when building your own plugig.

The source code is rather small, so it is better to look into it than to explain it here. If your theme already listens for changes in prefers-color-scheme, pass {auto: false} to the constructor, to avoid adding the listeners again. To switch between light and dark images, call the activate method with either light or dark.

Contributing

The Makefile is the lead for all development tasks. Mind the tests in Python and JavaScript.

Appropriate documentation will come soon.

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_colorschemed_images-0.1.1.tar.gz (15.0 kB view details)

Uploaded Source

Built Distribution

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

sphinx_colorschemed_images-0.1.1-py3-none-any.whl (11.9 kB view details)

Uploaded Python 3

File details

Details for the file sphinx_colorschemed_images-0.1.1.tar.gz.

File metadata

File hashes

Hashes for sphinx_colorschemed_images-0.1.1.tar.gz
Algorithm Hash digest
SHA256 f99ca3e492f074fd3509fb82024ea187b6af3990ba21c74e880d968c9583a087
MD5 bdb4ba0fea1fdcfc090bc570dae375b8
BLAKE2b-256 0ab823d2dc476e1c2c1d5f5a6154473fca7e56c2eb890019f6ef65cb890725b6

See more details on using hashes here.

File details

Details for the file sphinx_colorschemed_images-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for sphinx_colorschemed_images-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 b81967267baeee7f7c4543d030484fd6dbe6964f3ecec779635035aae64469a2
MD5 80738b70665dae5ada3e9a8645a05e31
BLAKE2b-256 d0d47ce6ed2bc4bf1a62d46f5bfff006d5cd36c3a5e7df8b761ac19b414c16e2

See more details on using hashes here.

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