Debian Codegraph @

util/change_display_profile_cal_whitepoint.py

raw
1 #!/usr/bin/env python3
2 import os
3 import sys
4
5 # Work-around 'Invalid handle' error on Windows when writing to stdout
6 sys.stdout = sys.stderr
7
8 sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
9
10 from DisplayCAL import colormath as cm
11 from DisplayCAL import config
12 from DisplayCAL import localization as lang
13 from DisplayCAL import worker
14 from DisplayCAL.icc_profile import ICCProfile, get_display_profile
15 from DisplayCAL.wx_windows import BaseApp, wx
16
17 # Environment sets defaults
18 CAL_ONLY = int(os.getenv("CHANGE_DISPLAY_PROFILE_WTPT_CAL_ONLY", 0))
19 USE_COLLINK = int(os.getenv("CHANGE_DISPLAY_PROFILE_WTPT_USE_COLLINK", 0))
20
21
22 def main(*args, **kwargs):
23 # Parse arguments
24 cal_only = kwargs.get("--cal-only", CAL_ONLY)
25 state = None
26 xy = None
27 profile = None
28 outfilename = None
29 for i, arg in enumerate(args):
30 if arg == "-t":
31 state = "COLORTEMP_DAYLIGHT"
32 elif arg == "-T":
33 state = "COLORTEMP_BLACKBODY"
34 elif (
35 arg.startswith("-t")
36 or arg.startswith("-T")
37 or state in ("COLORTEMP_DAYLIGHT", "COLORTEMP_BLACKBODY")
38 ):
39 if state in ("COLORTEMP_DAYLIGHT", "COLORTEMP_BLACKBODY"):
40 ctstr = arg
41 else:
42 ctstr = arg[2:]
43 try:
44 ct = float(ctstr)
45 except ValueError:
46 raise Invalid("Invalid color temperature %s" % ctstr)
47 if arg.startswith("-t") or state == "COLORTEMP_DAYLIGHT":
48 xy = cm.CIEDCCT2xyY(ct)
49 if not xy:
50 raise Invalid("Daylight color temperature %i out of range" % ct)
51 else:
52 xy = cm.planckianCT2xyY(ct)
53 if not xy:
54 raise Invalid("Blackbody color temperature %i out of range" % ct)
55 state = None
56 elif arg == "-w":
57 state = "CHROMATICITY"
58 elif arg.startswith("-w") or state == "CHROMATICITY":
59 if state == "CHROMATICITY":
60 xystr = arg
61 else:
62 xystr = arg[2:]
63 xy = xystr.split(",")
64 if len(xy) != 2:
65 raise Invalid("Invalid chromaticity: %s" % xystr)
66 try:
67 xy = [float(v) for v in xy]
68 except ValueError:
69 raise Invalid("Invalid chromaticity %s" % xystr)
70 state = None
71 elif os.path.isfile(arg) and i < len(args) - 1:
72 print("Reading profile:", arg)
73 profile = ICCProfile(arg)
74 else:
75 outfilename = os.path.abspath(arg)
76 if not xy or not outfilename:
77 raise Invalid(
78 "Usage: %s [-t temp | -T temp | -w x,y] [--cal-only] [inprofile] outfilename"
79 % os.path.basename(__file__)
80 )
81 if not profile:
82 print("Reading display profile")
83 profile = get_display_profile()
84 # Setup
85 config.initcfg()
86 lang.init()
87 w = worker.Worker()
88 fn = w.change_display_profile_cal_whitepoint
89 args = profile, xy[0], xy[1], outfilename, cal_only, USE_COLLINK
90 # Process
91 if CAL_ONLY:
92 fn(*args)
93 else:
94 app = BaseApp(0)
95 app.TopWindow = wx.Frame(None)
96 w.start(
97 lambda result: app.ExitMainLoop(),
98 fn,
99 wargs=args,
100 progress_msg=lang.getstr("create_profile"),
101 )
102 app.MainLoop()
103
104
105 class Invalid(ValueError):
106 pass
107
108
109 if __name__ == "__main__":
110 try:
111 main(*sys.argv[1:])
112 except Invalid as exception:
113 print(exception)