@@ -948,117 +948,13 @@ def load(self, path):
948
948
self .name = path
949
949
950
950
951
- def main ():
952
- import argparse
953
- argv = sys .argv [1 :]
954
-
955
- # Usage:
956
- # python -m viscm
957
- # python -m viscm edit
958
- # python -m viscm edit <file.py>
959
- # (file.py must define some appropriate globals)
960
- # python -m viscm view <file.py>
961
- # (file.py must define a global named "test_cm")
962
- # python -m viscm view "matplotlib builtin colormap"
963
- # python -m viscm view --save=foo.png ...
964
-
965
- parser = argparse .ArgumentParser (
966
- prog = "python -m viscm" ,
967
- description = "A colormap tool." ,
968
- )
969
- parser .add_argument ("action" , metavar = "ACTION" ,
970
- help = "'edit' or 'view' (or 'show', same as 'view')" ,
971
- choices = ["edit" , "view" , "show" ],
972
- default = "edit" ,
973
- nargs = "?" )
974
- parser .add_argument ("colormap" , metavar = "COLORMAP" ,
975
- default = None ,
976
- help = "A .json file saved from the editor, or "
977
- "the name of a matplotlib builtin colormap" ,
978
- nargs = "?" )
979
- parser .add_argument ("--uniform-space" , metavar = "SPACE" ,
980
- default = "CAM02-UCS" ,
981
- dest = "uniform_space" ,
982
- help = "The perceptually uniform space to use. Usually "
983
- "you should leave this alone. You can pass 'CIELab' "
984
- "if you're curious how uniform some colormap is in "
985
- "CIELab space. You can pass 'buggy-CAM02-UCS' if "
986
- "you're trying to reproduce the matplotlib colormaps "
987
- "(which turn out to have had a small bug in the "
988
- "assumed sRGB viewing conditions) from their bezier "
989
- "curves." )
990
- parser .add_argument ("-t" , "--type" , type = str ,
991
- default = "linear" , choices = ["linear" , "diverging" , "diverging-continuous" ],
992
- help = "Choose a colormap type. Supported options are 'linear', 'diverging', and 'diverging-continuous" )
993
- parser .add_argument ("-m" , "--method" , type = str ,
994
- default = "CatmulClark" , choices = ["Bezier" , "CatmulClark" ],
995
- help = "Choose a spline construction method. 'CatmulClark' is the default, but you may choose the legacy option 'Bezier'" )
996
- parser .add_argument ("--save" , metavar = "FILE" ,
997
- default = None ,
998
- help = "Immediately save visualization to a file "
999
- "(view-mode only)." )
1000
- parser .add_argument ("--quit" , default = False , action = "store_true" ,
1001
- help = "Quit immediately after starting "
1002
- "(useful with --save)." )
1003
- args = parser .parse_args (argv )
1004
-
1005
- cm = Colormap (args .type , args .method , args .uniform_space )
1006
- app = QtWidgets .QApplication ([])
1007
-
1008
- if args .colormap :
1009
- cm .load (args .colormap )
1010
-
1011
-
1012
- # Easter egg! I keep typing 'show' instead of 'view' so accept both
1013
- if args .action in ("view" , "show" ):
1014
- if cm is None :
1015
- sys .exit ("Please specify a colormap" )
1016
- fig = plt .figure ()
1017
- figureCanvas = FigureCanvas (fig )
1018
- v = viscm (cm .cmap , name = cm .name , figure = fig , uniform_space = cm .uniform_space )
1019
- mainwindow = ViewerWindow (figureCanvas , v , cm .name )
1020
- if args .save is not None :
1021
- v .figure .set_size_inches (20 , 12 )
1022
- v .figure .savefig (args .save )
1023
- elif args .action == "edit" :
1024
- if not cm .can_edit :
1025
- sys .exit ("Sorry, I don't know how to edit the specified colormap" )
1026
- # Hold a reference so it doesn't get GC'ed
1027
- fig = plt .figure ()
1028
- figureCanvas = FigureCanvas (fig )
1029
- v = viscm_editor (figure = fig , uniform_space = cm .uniform_space , cmtype = cm .cmtype , method = cm .method , ** cm .params )
1030
- mainwindow = EditorWindow (figureCanvas , v )
1031
- else :
1032
- raise RuntimeError ("can't happen" )
1033
-
1034
- if args .quit :
1035
- sys .exit ()
1036
-
1037
- figureCanvas .setSizePolicy (QtWidgets .QSizePolicy .Expanding ,
1038
- QtWidgets .QSizePolicy .Expanding )
1039
- figureCanvas .updateGeometry ()
1040
-
1041
- mainwindow .resize (800 , 600 )
1042
- mainwindow .show ()
1043
-
1044
- # PyQt messes up signal handling by default. Python signal handlers (e.g.,
1045
- # the default handler for SIGINT that raises KeyboardInterrupt) can only
1046
- # run when we enter the Python interpreter, which doesn't happen while
1047
- # idling in the Qt mainloop. (Unless we register a timer to poll
1048
- # explicitly.) So here we unregister Python's default signal handler and
1049
- # replace it with... the *operating system's* default signal handler, so
1050
- # instead of a KeyboardInterrupt our process just exits.
1051
- import signal
1052
- signal .signal (signal .SIGINT , signal .SIG_DFL )
1053
-
1054
- app .exec_ ()
1055
-
1056
951
def about ():
1057
952
QtWidgets .QMessageBox .about (None , "VISCM" ,
1058
953
"Copyright (C) 2015-2016 Nathaniel Smith\n " +
1059
954
"Copyright (C) 2015-2016 Stéfan van der Walt\n "
1060
955
"Copyright (C) 2016 Hankun Zhao" )
1061
956
957
+
1062
958
class ViewerWindow (QtWidgets .QMainWindow ):
1063
959
def __init__ (self , figurecanvas , viscm , cmapname , parent = None ):
1064
960
QtWidgets .QMainWindow .__init__ (self , parent )
0 commit comments