Dynamic images in Hugo

As you may have noticed, this website can be displayed in either light or dark mode (toggle with sun/moon top left of page or with Alt+T ). An issue is that images and diagrams are perfectly visible in light mode, but when flipping over to dark mode they either are surrounded by a big white box or lost all contrast if the background is transparent, or vice versa.