Skip to content

Auto change color based on the theme - #25702

Merged
asmeurer merged 24 commits into
sympy:masterfrom
tjstienstra:dark-theme-svg-change
Feb 3, 2024
Merged

asmeurer merged 24 commits into
sympy:masterfrom
tjstienstra:dark-theme-svg-change

Conversation

@tjstienstra

@tjstienstra tjstienstra commented Sep 19, 2023 •

Copy link
Copy Markdown
Contributor

References to other Issues or PRs

Proposal to fix #24695

Brief description of what is fixed or changed

This PR uses the proposal from this comment, where css can be used in the svg to autochange the color, by using raw html in the rst file.

Other comments

Release Notes

NO ENTRY

@sympy-bot

sympy-bot commented Sep 19, 2023 •

Copy link
Copy Markdown

✅

Hi, I am the SymPy bot. I'm here to help you write a release notes entry. Please read the guide on how to write release notes.

  • No release notes entry will be added for this pull request.
Click here to see the pull request description that was parsed.
<!-- Your title above should be a short description of what
was changed. Do not include the issue number in the title. -->

#### References to other Issues or PRs
<!-- If this pull request fixes an issue, write "Fixes #NNNN" in that exact
format, e.g. "Fixes #1234" (see
https://tinyurl.com/auto-closing for more information). Also, please
write a comment on that issue linking back to this pull request once it is
open. -->
Proposal to fix #24695

#### Brief description of what is fixed or changed
This PR uses the proposal from [this comment](https://github.com/sympy/sympy/issues/24695#issuecomment-1723272283), where css can be used in the svg to autochange the color, by using raw html in the rst file.

#### Other comments


#### Release Notes

<!-- Write the release notes for this release below between the BEGIN and END
statements. The basic format is a bulleted list with the name of the subpackage
and the release note for this PR. For example:

* solvers
  * Added a new solver for logarithmic equations.

* functions
  * Fixed a bug with log of integers. Formerly, `log(-x)` incorrectly gave `-log(x)`.

* physics.units
  * Corrected a semantical error in the conversion between volt and statvolt which
    reported the volt as being larger than the statvolt.

or if no release note(s) should be included use:

NO ENTRY

See https://github.com/sympy/sympy/wiki/Writing-Release-Notes for more
information on how to write release notes. The bot will check your release
notes automatically to see if they are formatted correctly. -->

<!-- BEGIN RELEASE NOTES -->
NO ENTRY
<!-- END RELEASE NOTES -->

@asmeurer

Copy link
Copy Markdown
Member

.. raw:: html

<svg class="align-center" viewbox="0 0 350 175" width="350px">
<use xlink:href="../../../_static/modules/physics/vector/vec_rep.svg#vec_rep_id"></use>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How is this different from what gets generated by the .. image directive?

@tjstienstra tjstienstra Sep 19, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The .. image wraps the svg in img object, which seems to cause the css (currentColor) not to work anymore. This solution did work for me here.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can confirm your example site works. Are you using any custom CSS on that site?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, I'm just changing the #000000 color references in the image to currentColor, which is a CSS variable.

@tjstienstra tjstienstra Sep 19, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other solution with :file: does actually work here as well.

@tjstienstra

Copy link
Copy Markdown
Contributor Author

For me, there is no image that shows up, in either dark or light mode.

Hmmm, I would guess that it cannot find the image, though if I look in the CircleCI build the file is in the expected location. Bit strange that it does not work here, as it did work for me when building the documentation locally.

@tjstienstra

Copy link
Copy Markdown
Contributor Author

If we would be able by the way to get this working we could opt for creating a custom directive specifically for SVGs.

@tjstienstra

tjstienstra commented Sep 19, 2023 •

Copy link
Copy Markdown
Contributor Author

Came across the solution I just implemented here. The advantage of this solution is that it would keep the rst code clean. However, it will require one to write the scaling etc inside the SVG file itself.

@asmeurer

Copy link
Copy Markdown
Member

Nice, I can confirm this does work, with all 6 combinations of system/page light/dark/auto settings. https://output.circle-artifacts.com/output/job/77eab45b-b8fd-4228-bf13-ab66ca3ac746/artifacts/0/doc/_build/html/modules/physics/vector/vectors.html

So now we need to change all the remaining SVGs in the docs, and also document this somewhere.

We might also consider making some upstream improvements or at least open issues in Furo and/or Sphinx about this.

@tjstienstra
tjstienstra marked this pull request as ready for review November 9, 2023 15:11
@tjstienstra

Copy link
Copy Markdown
Contributor Author

Just took the time to go through all SVGs. During this process, I also cleaned up some of the SVGs in terms of code. The primary change in the commits so far is still making the images dark theme compatible by just letting colors switch from black to white. The 'kin_angvel2.svg' image has also undergone a subtle color adjustment.

It's worth noting that the SVGs referenced in the docstrings, such as the images from the joints framework, do not currently auto-adjust their colors. In my opinion, this isn't a significant concern as they remain generally readable. Nevertheless, if there is auto-changing the color is desired, it is possible. However, the disadvantage is that the entire file paths within docstrings need to be specified, which is cumbersome. Let me know if this is a required change.

@github-actions

github-actions Bot commented Nov 9, 2023 •

Copy link
Copy Markdown

Benchmark results from GitHub Actions

Lower numbers are good, higher numbers are bad. A ratio less than 1
means a speed up and greater than 1 means a slowdown. Green lines
beginning with + are slowdowns (the PR is slower then master or
master is slower than the previous release). Red lines beginning
with - are speedups.

Significantly changed benchmark results (PR vs master)

Significantly changed benchmark results (master vs previous release)

| Change   | Before [a00718ba]    | After [0b25b279]    |   Ratio | Benchmark (Parameter)                                                |
|----------|----------------------|---------------------|---------|----------------------------------------------------------------------|
| -        | 69.9±1ms             | 45.2±0.5ms          |    0.65 | integrate.TimeIntegrationRisch02.time_doit(10)                       |
| -        | 67.5±0.9ms           | 43.9±0.6ms          |    0.65 | integrate.TimeIntegrationRisch02.time_doit_risch(10)                 |
| +        | 19.1±0.1μs           | 30.7±0.3μs          |    1.61 | integrate.TimeIntegrationRisch03.time_doit(1)                        |
| -        | 5.43±0.01ms          | 2.85±0.01ms         |    0.53 | logic.LogicSuite.time_load_file                                      |
| -        | 72.4±0.4ms           | 29.0±0.1ms          |    0.4  | polys.TimeGCD_GaussInt.time_op(1, 'dense')                           |
| -        | 25.9±0.08ms          | 16.8±0.2ms          |    0.65 | polys.TimeGCD_GaussInt.time_op(1, 'expr')                            |
| -        | 72.6±0.6ms           | 29.2±0.4ms          |    0.4  | polys.TimeGCD_GaussInt.time_op(1, 'sparse')                          |
| -        | 254±1ms              | 126±0.6ms           |    0.49 | polys.TimeGCD_GaussInt.time_op(2, 'dense')                           |
| -        | 255±1ms              | 123±0.3ms           |    0.49 | polys.TimeGCD_GaussInt.time_op(2, 'sparse')                          |
| -        | 651±1ms              | 368±2ms             |    0.57 | polys.TimeGCD_GaussInt.time_op(3, 'dense')                           |
| -        | 655±1ms              | 371±2ms             |    0.57 | polys.TimeGCD_GaussInt.time_op(3, 'sparse')                          |
| -        | 499±2μs              | 290±2μs             |    0.58 | polys.TimeGCD_LinearDenseQuadraticGCD.time_op(1, 'dense')            |
| -        | 1.81±0.01ms          | 1.04±0.01ms         |    0.57 | polys.TimeGCD_LinearDenseQuadraticGCD.time_op(2, 'dense')            |
| -        | 5.88±0.01ms          | 3.12±0.02ms         |    0.53 | polys.TimeGCD_LinearDenseQuadraticGCD.time_op(3, 'dense')            |
| -        | 453±3μs              | 230±3μs             |    0.51 | polys.TimeGCD_QuadraticNonMonicGCD.time_op(1, 'dense')               |
| -        | 1.48±0ms             | 684±5μs             |    0.46 | polys.TimeGCD_QuadraticNonMonicGCD.time_op(2, 'dense')               |
| -        | 4.93±0.03ms          | 1.67±0.01ms         |    0.34 | polys.TimeGCD_QuadraticNonMonicGCD.time_op(3, 'dense')               |
| -        | 377±2μs              | 205±2μs             |    0.54 | polys.TimeGCD_SparseGCDHighDegree.time_op(1, 'dense')                |
| -        | 2.45±0.04ms          | 1.25±0.01ms         |    0.51 | polys.TimeGCD_SparseGCDHighDegree.time_op(3, 'dense')                |
| -        | 10.0±0.03ms          | 4.37±0.02ms         |    0.44 | polys.TimeGCD_SparseGCDHighDegree.time_op(5, 'dense')                |
| -        | 361±1μs              | 168±2μs             |    0.47 | polys.TimeGCD_SparseNonMonicQuadratic.time_op(1, 'dense')            |
| -        | 2.49±0.01ms          | 904±2μs             |    0.36 | polys.TimeGCD_SparseNonMonicQuadratic.time_op(3, 'dense')            |
| -        | 9.58±0.2ms           | 2.63±0.01ms         |    0.28 | polys.TimeGCD_SparseNonMonicQuadratic.time_op(5, 'dense')            |
| -        | 1.03±0.02ms          | 429±2μs             |    0.42 | polys.TimePREM_LinearDenseQuadraticGCD.time_op(3, 'dense')           |
| -        | 1.73±0.01ms          | 506±2μs             |    0.29 | polys.TimePREM_LinearDenseQuadraticGCD.time_op(3, 'sparse')          |
| -        | 5.83±0.05ms          | 1.83±0.02ms         |    0.31 | polys.TimePREM_LinearDenseQuadraticGCD.time_op(5, 'dense')           |
| -        | 8.39±0.04ms          | 1.50±0.01ms         |    0.18 | polys.TimePREM_LinearDenseQuadraticGCD.time_op(5, 'sparse')          |
| -        | 283±1μs              | 64.2±0.3μs          |    0.23 | polys.TimePREM_QuadraticNonMonicGCD.time_op(1, 'sparse')             |
| -        | 3.41±0.02ms          | 394±3μs             |    0.12 | polys.TimePREM_QuadraticNonMonicGCD.time_op(3, 'dense')              |
| -        | 4.02±0.02ms          | 282±1μs             |    0.07 | polys.TimePREM_QuadraticNonMonicGCD.time_op(3, 'sparse')             |
| -        | 7.14±0.09ms          | 1.25±0.01ms         |    0.17 | polys.TimePREM_QuadraticNonMonicGCD.time_op(5, 'dense')              |
| -        | 8.68±0.04ms          | 841±3μs             |    0.1  | polys.TimePREM_QuadraticNonMonicGCD.time_op(5, 'sparse')             |
| -        | 5.16±0.03ms          | 2.99±0.01ms         |    0.58 | polys.TimeSUBRESULTANTS_LinearDenseQuadraticGCD.time_op(2, 'sparse') |
| -        | 12.3±0.05ms          | 6.57±0.04ms         |    0.53 | polys.TimeSUBRESULTANTS_LinearDenseQuadraticGCD.time_op(3, 'dense')  |
| -        | 22.4±0.06ms          | 9.12±0.02ms         |    0.41 | polys.TimeSUBRESULTANTS_LinearDenseQuadraticGCD.time_op(3, 'sparse') |
| -        | 5.20±0.03ms          | 875±2μs             |    0.17 | polys.TimeSUBRESULTANTS_QuadraticNonMonicGCD.time_op(1, 'sparse')    |
| -        | 12.7±0.04ms          | 7.13±0.05ms         |    0.56 | polys.TimeSUBRESULTANTS_QuadraticNonMonicGCD.time_op(2, 'sparse')    |
| -        | 102±0.8ms            | 25.6±0.05ms         |    0.25 | polys.TimeSUBRESULTANTS_QuadraticNonMonicGCD.time_op(3, 'dense')     |
| -        | 167±2ms              | 54.7±0.5ms          |    0.33 | polys.TimeSUBRESULTANTS_QuadraticNonMonicGCD.time_op(3, 'sparse')    |
| -        | 173±1μs              | 111±0.6μs           |    0.64 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(1, 'dense')      |
| -        | 361±3μs              | 217±1μs             |    0.6  | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(1, 'sparse')     |
| -        | 4.30±0.03ms          | 831±5μs             |    0.19 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(3, 'dense')      |
| -        | 5.27±0.03ms          | 382±1μs             |    0.07 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(3, 'sparse')     |
| -        | 20.2±0.3ms           | 2.80±0.01ms         |    0.14 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(5, 'dense')      |
| -        | 22.8±0.2ms           | 629±2μs             |    0.03 | polys.TimeSUBRESULTANTS_SparseGCDHighDegree.time_op(5, 'sparse')     |
| -        | 482±1μs              | 134±0.6μs           |    0.28 | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(1, 'sparse') |
| -        | 4.70±0.02ms          | 614±2μs             |    0.13 | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(3, 'dense')  |
| -        | 5.28±0.07ms          | 138±1μs             |    0.03 | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(3, 'sparse') |
| -        | 13.3±0.2ms           | 1.28±0ms            |    0.1  | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(5, 'dense')  |
| -        | 13.8±0.07ms          | 141±0.9μs           |    0.01 | polys.TimeSUBRESULTANTS_SparseNonMonicQuadratic.time_op(5, 'sparse') |
| -        | 134±0.8μs            | 73.9±0.5μs          |    0.55 | solve.TimeMatrixOperations.time_rref(3, 0)                           |
| -        | 248±0.6μs            | 87.5±0.6μs          |    0.35 | solve.TimeMatrixOperations.time_rref(4, 0)                           |
| -        | 24.1±0.2ms           | 10.2±0.06ms         |    0.42 | solve.TimeSolveLinSys189x49.time_solve_lin_sys                       |
| -        | 28.6±0.2ms           | 15.4±0.1ms          |    0.54 | solve.TimeSparseSystem.time_linsolve_Aaug(20)                        |
| -        | 55.4±0.4ms           | 24.8±0.05ms         |    0.45 | solve.TimeSparseSystem.time_linsolve_Aaug(30)                        |
| -        | 28.3±0.1ms           | 15.2±0.07ms         |    0.54 | solve.TimeSparseSystem.time_linsolve_Ab(20)                          |
| -        | 54.4±0.4ms           | 24.5±0.2ms          |    0.45 | solve.TimeSparseSystem.time_linsolve_Ab(30)                          |

Full benchmark results can be found as artifacts in GitHub Actions
(click on checks at the top of the PR).

@asmeurer

asmeurer commented Nov 9, 2023

Copy link
Copy Markdown
Member

What do these docstring images look like in dark mode?

@tjstienstra

Copy link
Copy Markdown
Contributor Author

They are all on this page.

@asmeurer

asmeurer commented Nov 9, 2023

Copy link
Copy Markdown
Member

There are some examples on that page that end up with black text against the background.


.. image:: PinJoint.svg
.. raw:: html
:file: ../../../doc/src/modules/physics/mechanics/api/PinJoint.svg

@tjstienstra tjstienstra Nov 9, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed it for the PinJoint (see here). Don't really like specifying the path here, but it does fix it. Shall I also update it for the other joints?

@asmeurer

asmeurer commented Nov 9, 2023

Copy link
Copy Markdown
Member

Is there an upstream issue in Sphinx to make it so that we don't have to use these workarounds?

@tjstienstra

tjstienstra commented Nov 9, 2023 •

Copy link
Copy Markdown
Contributor Author

Is there an upstream issue in Sphinx to make it so that we don't have to use these workarounds?

I would say that it is related to this issue. One of the workarounds suggested in this issue is actually what I've done in this PR.

@tjstienstra
tjstienstra requested a review from asmeurer February 2, 2024 09:51
@asmeurer

asmeurer commented Feb 2, 2024

Copy link
Copy Markdown
Member

Looks like the diagonal lines at the top of the svg on this page aren't modified correctly https://output.circle-artifacts.com/output/job/7e1d9056-97bf-4724-9663-09d92b9894b5/artifacts/0/doc/_build/html/modules/physics/mechanics/examples/lin_pend_nonmin_example.html

@asmeurer

asmeurer commented Feb 2, 2024

Copy link
Copy Markdown
Member

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm concerned about the color contrast of this SVG, especially in dark mode. Please make sure that the text color and background colors are WGAC AA compliant. You can use https://accessible-colors.com/ to check.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome tool. Thank you for the suggestion. The contrast ratio is now 4.54 in dark theme and 4.62 in light theme. Addressed in 0b4fa71

@asmeurer

asmeurer commented Feb 3, 2024

Copy link
Copy Markdown
Member

This looks good to me now, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SVG images in the docs are not visible in dark mode

3 participants