The idea of this project is simple: make the colormaps of Python's matplotlib library available in JavaScript. I came up with it because I needed a colormap in JavaScript for a side project of mine and Google did not spit out any good results, so I decided to do it myself and make it available to the public.
If you have found this repository I presume you already know what a colormap is, but just for completeness’ sake, a colormap formally is a function f: [0, 1] → [0, 255]³ that maps a number onto a color, represented by a triplet of 8-bit integers (= the red, green, blue values). Or at least that is the convention that I have adopted for this project. Of course, a lot of the time the values you want to colorcode are not between 0 and 1 — in this case, you will have to rescale them first.
Most of the magic is contained in the js-colormaps.js
file, which you need to include like:
<script src="js-colormaps.js"></script>
Once you have loaded that file, you have either use the function evaluate_cmap(x, name, reverse)
to map a value x
onto a color using the colormap specified by name
and reverse
:
>> evaluate_cmap(0.5, 'viridis', false)
Array(3) [ 33, 144, 140 ]
Alternatively, you can also directly access every colormap by its name (using the _r
suffix to reverse it):
>> RdBu_r(0.25)
Array(3) [ 106, 172, 208 ]
All of matplotlib’s colormaps are available, under the same name that you would use in Python:
(Feel free to check out the overview.html
file to see how this overview figure was created.)
Additionally, it should not be too hard to modify the code in create-colormaps.py
to also export other Python-based colormaps to JavaScript, for example, the scientific colour maps by Fabio Crameri.
I did not invent any of the colormaps here, I do not own them, and I do not claim that I do. I just tried to make them available to others. If I happen to violate any of your copyrights with this project, please inform me before you drag me to court and we can sort this thing out :-)
Regarding my own copyrights: The project is released under the MIT license (see LICENSE file), which pretty much means “Do whatever you want, but don't hold me liable!”.