Nice work @vmagnin!
I first learned a few things about colormaps from the works of Kenneth Moreland and the choice of the default colormap in Paraview. Here are a few related blog posts:
I also like the colormaps in Matplotlib (Choosing Colormaps in Matplotlib ā Matplotlib 3.8.0 documentation) and the ones used in ColorBrewer. The latter are also available for gnuplot here: GitHub - aschn/gnuplot-colorbrewer: ColorBrewer color schemes for gnuplot
Here is a short Python script to export colormaps from Matplotlib. I didnāt do any tests yet with ForColormap, so apologies if any bugs remain. The usage should be self-explanatory:
$ ./export_mpl_cmap.py -h
usage: export_mpl_cmap [-h] [--list] [-t TABSIZE] [-o OUTPUT] [colormap]
Helper script to export matplotlib colormaps as Fortran arrays for use with ForColormap.
positional arguments:
colormap A valid Matplotlib colormap name.
optional arguments:
-h, --help show this help message and exit
--list List the available colormaps in Matplotlib and quit.
-t TABSIZE, --tabsize TABSIZE
Size of the tab; default is 4.
-o OUTPUT, --output OUTPUT
A destination file; defaults to stdout.
#!/usr/bin/env python
"""
export_mpl_cmap.py --
Script for exporting Matplotlib colormaps as Fortran include blocks
Copyright (c) 2023 Ivan Pribec. All rights reserved.
This work is licensed under the terms of the MIT license.
For a copy, see <https://opensource.org/licenses/MIT>.
"""
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl
def fortran_cmap(name,tabsize=4):
"""Exports a Matplotlib colormap as a Fortran block"""
cmap = mpl.colormaps[name]
header = "\tinteger, dimension(0:{N}, 1:3) :: {name} = reshape([ &\n"
footer = "\t], shape({name}), order=[2,1] )\n"
triplet = "{:3d},{:3d},{:3d}"
block = header.format(N=cmap.N-1,name=name)
for i in range(0,cmap.N,4):
left = cmap.N - i
line = ""
if (left > 4):
for k in range(4):
(r,g,b,_) = cmap(i + k,bytes=True)
line += "\t" + triplet.format(r,g,b) + ','
line += " &\n"
else:
for k in range(left):
(r,g,b,_) = cmap(i + k,bytes=True)
sep = ',' if k < left-1 else ''
line += "\t" + triplet.format(r,g,b) + sep
line += " &\n"
block += line
block += footer.format(name=name)
return block
if __name__ == '__main__':
import argparse
import sys
from textwrap import wrap
parser = argparse.ArgumentParser(
prog='export_mpl_cmap',
description="""Helper script to export matplotlib colormaps
as Fortran arrays for use with ForColormap.""")
parser.add_argument('--list',action='store_true',
help="List the available colormaps in Matplotlib and quit.")
parser.add_argument('-t', '--tabsize', default=4,
help="Size of the tab; default is 4.")
parser.add_argument('-o', '--output',
type=argparse.FileType('w'),
default=sys.stdout,
help="A destination file; defaults to stdout.")
parser.add_argument('colormap', nargs='?',
help="A valid Matplotlib colormap name.")
args = parser.parse_args()
if args.list:
print("The colormaps available are:\n")
cmaps = list(filter(lambda x: not x.endswith('_r'),
mpl.pyplot.colormaps()))
# Column width
cw = len(max(cmaps,key=len)) + 3
for a, b, c, d in zip(cmaps[0::4],cmaps[1::4],
cmaps[2::4],cmaps[3::4]):
print(" {:{cw}}{:{cw}}{:{cw}}{:{cw}}".format(a,b,c,d,cw=cw))
print("\nAppend '_r' to the end of a colormap to obtain the reverse version.")
print("\nFor more information visit:\n\n\t"
"https://matplotlib.org/stable/users/explain/colors/colormaps.html\n")
else:
color_block = fortran_cmap(args.colormap,args.tabsize)
with args.output as f:
f.write(color_block)