Skip to content

Commit 6e484e3

Browse files
committed
Make the command line viscm interface much fancier
1 parent a08fdbe commit 6e484e3

1 file changed

Lines changed: 74 additions & 15 deletions

File tree

pycam02ucs/cm/viscm.py

Lines changed: 74 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
# Simple script using CIECAM02 and CAM02-UCS to visualize properties of a
66
# matplotlib colormap
77

8+
import sys
89
import os.path
910

1011
import numpy as np
@@ -195,8 +196,8 @@ def __init__(self, cm, name=None, N=256, N_dots=50, show_gamut=False):
195196
if name is None:
196197
name = cm.name
197198

198-
fig = plt.figure()
199-
fig.suptitle("Colormap evaluation: %s" % (name,), fontsize=24)
199+
self.fig = plt.figure()
200+
self.fig.suptitle("Colormap evaluation: %s" % (name,), fontsize=24)
200201
axes = _vis_axes()
201202

202203
x = np.linspace(0, 1, N)
@@ -806,19 +807,77 @@ def _refresh_point(self):
806807
self.ax.figure.canvas.draw()
807808

808809

809-
if __name__ == "__main__":
810-
import sys
811-
import os
812-
813-
ns = {'__name__': ''}
814-
815-
if len(sys.argv) > 1:
816-
cmap_params = sys.argv[1]
817-
if os.path.isfile(cmap_params):
818-
with open(cmap_params) as f:
819-
code = compile(f.read(), cmap_params, 'exec')
810+
def main(argv):
811+
import argparse
812+
813+
# Usage:
814+
# python -m pycam02ucs.cm.viscm
815+
# python -m pycam02ucs.cm.viscm edit
816+
# python -m pycam02ucs.cm.viscm edit <file.py>
817+
# (file.py must define some appropriate globals)
818+
# python -m pycam02ucs.cm.viscm view <file.py>
819+
# (file.py must define a global named "test_cm")
820+
# python -m pycam02ucs.cm.viscm view "matplotlib builtin colormap"
821+
# python -m pycam02ucs.cm.viscm view --save=foo.png ...
822+
823+
parser = argparse.ArgumentParser(
824+
prog="python -m {}".format(__file__),
825+
description="A colormap tool.",
826+
)
827+
parser.add_argument("action", metavar="ACTION",
828+
help="'edit' or 'view'",
829+
choices=["edit", "view"],
830+
default="edit",
831+
nargs="?")
832+
parser.add_argument("colormap", metavar="COLORMAP",
833+
default=None,
834+
help="A .py file saved from the editor, or "
835+
"the name of a matplotlib builtin colormap",
836+
nargs="?")
837+
parser.add_argument("--save", metavar="FILE",
838+
default=None,
839+
help="Immediately save visualization to a file (view-mode only).")
840+
parser.add_argument("--quit", default=False, action="store_true",
841+
help="Quit immediately after starting (useful with --save).")
842+
args = parser.parse_args(argv)
843+
844+
params = {}
845+
cmap = None
846+
if args.colormap:
847+
if os.path.isfile(args.colormap):
848+
ns = {'__name__': '',
849+
'__file__': os.path.basename(args.colormap),
850+
}
851+
852+
with open(args.colormap) as f:
853+
code = compile(f.read(),
854+
os.path.basename(args.colormap),
855+
'exec')
820856
exec(code, globals(), ns)
821857

822-
params = ns.get('parameters', {})
823-
viscm_editor(**params)
858+
params = ns.get('parameters', {})
859+
cmap = ns.get("test_cm", None)
860+
else:
861+
cmap = plt.get_cmap(args.colormap)
862+
863+
if args.action == "view":
864+
if cmap is None:
865+
sys.exit("Please specify a colormap")
866+
v = viscm(cmap)
867+
if args.save is not None:
868+
v.fig.set_size_inches(20, 12)
869+
v.fig.savefig(args.save)
870+
elif args.action == "edit":
871+
if params is None:
872+
sys.exit("Sorry, I don't know how to edit the specified colormap")
873+
viscm_editor(**params)
874+
else:
875+
raise RuntimeError("can't happen")
876+
877+
if args.quit:
878+
sys.exit()
879+
824880
plt.show()
881+
882+
if __name__ == "__main__":
883+
main(sys.argv[1:])

0 commit comments

Comments
 (0)