File: wxLUTViewer.py

package info (click to toggle)
displaycal-py3 3.9.16-1
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 29,120 kB
  • sloc: python: 115,777; javascript: 11,540; xml: 598; sh: 257; makefile: 173
file content (2542 lines) | stat: -rw-r--r-- 100,077 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
# -*- coding: utf-8 -*-

import math
import os
import re
import sys

import numpy

from DisplayCAL import (
    colormath,
    config,
    localization as lang,
    wxenhancedplot as plot,
)
from DisplayCAL.argyll import make_argyll_compatible_path
from DisplayCAL.argyll_cgats import cal_to_fake_profile, vcgt_to_cal
from DisplayCAL.config import (
    fs_enc,
    get_argyll_display_number,
    get_data_path,
    get_display_profile,
    get_display_rects,
    get_verified_path,
    getcfg,
    geticon,
    setcfg,
)
from DisplayCAL.icc_profile import (
    CurveType,
    ICCProfile,
    ICCProfileInvalidError,
    LUT16Type,
    ParametricCurveType,
    TextDescriptionType,
    VideoCardGammaTableType,
    VideoCardGammaType,
    WcsProfilesTagType,
)
from DisplayCAL.meta import name as appname
from DisplayCAL.options import debug
from DisplayCAL.util_decimal import float2dec
from DisplayCAL.util_os import waccess
from DisplayCAL.worker import (
    Error,
    UnloggedError,
    Worker,
    show_result_dialog,
)
from DisplayCAL.wxMeasureFrame import MeasureFrame
from DisplayCAL.wxaddons import get_platform_window_decoration_size, wx
from DisplayCAL.wxfixes import (
    GenBitmapButton as BitmapButton,
    wx_Panel,
)
from DisplayCAL.wxwindows import (
    BaseApp,
    BaseFrame,
    BitmapBackgroundPanelText,
    BitmapBackgroundPanelTextGamut,
    CustomCheckBox,
    FileDrop,
    InfoDialog,
    TooltipWindow,
)

BGCOLOUR = "#333333"
FGCOLOUR = "#999999"
GRIDCOLOUR = "#444444"

if sys.platform == "darwin":
    FONTSIZE_LARGE = 11
    FONTSIZE_SMALL = 10
else:
    FONTSIZE_LARGE = 9
    FONTSIZE_SMALL = 8


class CoordinateType(list):
    """List of coordinates.

    [(Y, x)] where Y is in the range 0..100 and x in the range 0..255

    """

    def __init__(self, profile=None):
        super(CoordinateType, self).__init__()
        self.profile = profile
        self._transfer_function = {}

    def get_gamma(
        self, use_vmin_vmax=False, average=True, least_squares=False, slice=(0.01, 0.99)
    ):
        """Return average or least squares gamma or a list of gamma values"""
        if len(self):
            start = slice[0] * 100
            end = slice[1] * 100
            values = []
            for i, (y, x) in enumerate(self):
                n = colormath.XYZ2Lab(0, y, 0)[0]
                if start <= n <= end:
                    values.append((x / 255.0 * 100, y))
        else:
            # Identity
            if average or least_squares:
                return 1.0
            return [1.0]
        vmin = 0
        vmax = 100.0
        if use_vmin_vmax:
            if len(self) > 2:
                vmin = self[0][0]
                vmax = self[-1][0]
        return colormath.get_gamma(values, 100.0, vmin, vmax, average, least_squares)

    def get_transfer_function(self, best=True, slice=(0.05, 0.95), outoffset=None):
        """Return transfer function name, exponent and match percentage"""
        transfer_function = self._transfer_function.get((best, slice))
        if transfer_function:
            return transfer_function
        xp = []
        fp = []
        for y, x in self:
            xp.append(x)
            fp.append(y)
        interp = colormath.Interp(xp, fp, use_numpy=True)
        otrc = CurveType(profile=self.profile)
        for i in range(len(self)):
            otrc.append(interp(i / (len(self) - 1.0) * 255) / 100 * 65535)
        match = otrc.get_transfer_function(best, slice, outoffset=outoffset)
        self._transfer_function[(best, slice)] = match
        return match

    def set_trc(self, power=2.2, values=(), vmin=0, vmax=100):
        """Set the response to a certain function.

        Positive power, or -2.4 = sRGB, -3.0 = L*, -240 = SMPTE 240M,
        -601 = Rec. 601, -709 = Rec. 709 (Rec. 601 and 709 transfer functions are
        identical)

        """
        self[:] = [[y, x] for y, x in values]
        for i in range(len(self)):
            self[i][0] = vmin + colormath.specialpow(self[i][1] / 255.0, power) * (
                vmax - vmin
            )


class PolyBox(plot.PolyLine):
    def __init__(self, x, y, w, h, **attr):
        plot.PolyLine.__init__(
            self, [(x, y), (x + w, y), (x + w, y + h), (x, y + h), (x, y)], **attr
        )


class LUTCanvas(plot.PlotCanvas):
    def __init__(self, *args, **kwargs):
        self.colors = {
            "RGB_R": "red",
            "RGB_G": "#00FF00",
            "RGB_B": "#0080FF",
            "CMYK_C": "cyan",
            "CMYK_M": "magenta",
            "CMYK_Y": "yellow",
            "CMYK_K": "black",
            "nCLR_0": "cyan",
            "nCLR_1": "magenta",
            "nCLR_2": "yellow",
            "nCLR_3": "black",
            "nCLR_4": "red",
            "nCLR_5": "#00FF00",
            "nCLR_6": "blue",
            "nCLR_7": "#FF8000",
            "nCLR_8": "#80FF00",
            "nCLR_9": "#00FF80",
            "XYZ_X": "red",
            "XYZ_Y": "#00FF00",
            "XYZ_Z": "#0080FF",
            "Lab_L*": "#CCCCCC",
            "Lab_a*": "#FF0080",
            "Lab_a*-": "#00CC80",
            "Lab_b*": "#FFEE00",
            "Lab_b*-": "#0080FF",
        }
        plot.PlotCanvas.__init__(self, *args, **kwargs)
        self.Unbind(wx.EVT_SCROLL_THUMBTRACK)
        self.Unbind(wx.EVT_SCROLL_PAGEUP)
        self.Unbind(wx.EVT_SCROLL_PAGEDOWN)
        self.Unbind(wx.EVT_SCROLL_LINEUP)
        self.Unbind(wx.EVT_SCROLL_LINEDOWN)
        self.HandCursor = wx.StockCursor(wx.CURSOR_CROSS)
        self.GrabHandCursor = wx.StockCursor(wx.CURSOR_SIZING)
        self.SetBackgroundColour(BGCOLOUR)
        self.SetEnableAntiAliasing(True)
        self.SetEnableHiRes(True)
        self.SetEnableCenterLines(True)
        self.SetEnableDiagonals("Bottomleft-Topright")
        self.SetEnableDrag(True)
        self.SetEnableGrid(False)
        self.SetEnablePointLabel(True)
        self.SetEnableTitle(False)
        self.SetForegroundColour(FGCOLOUR)
        self.SetFontSizeAxis(FONTSIZE_SMALL)
        self.SetFontSizeLegend(FONTSIZE_SMALL)
        self.SetFontSizeTitle(FONTSIZE_LARGE)
        self.SetGridColour(GRIDCOLOUR)
        self.setLogScale((False, False))
        self.SetPointLabelFunc(self.DrawPointLabel)
        self.canvas.BackgroundColour = BGCOLOUR
        if sys.platform == "win32" and sys.getwindowsversion() >= (6,):
            # Enable/disable double buffering on-the-fly to make pointlabel work
            self.canvas.Bind(wx.EVT_ENTER_WINDOW, self._disabledoublebuffer)
            self.canvas.Bind(wx.EVT_LEAVE_WINDOW, self._enabledoublebuffer)
        self.worker = Worker(self.TopLevelParent)
        self.errors = []
        self.resetzoom()

    def DrawLUT(
        self,
        vcgt=None,
        title=None,
        xLabel=None,
        yLabel=None,
        channels=None,
        colorspace=b"RGB",
        connection_colorspace=b"RGB",
    ):
        if not title:
            title = ""
        if not xLabel:
            xLabel = ""
        if not yLabel:
            yLabel = ""

        detect_increments = False
        Plot = plot.PolyLine
        Plot._attributes["width"] = 1

        maxv = 4095
        linear_points = []

        if colorspace in (b"YCbr", b"RGB", b"GRAY", b"HSV", b"HLS"):
            axis_y = 255.0
            if connection_colorspace in (b"Lab", b"XYZ"):
                axis_x = 100.0
            else:
                axis_x = 255.0
        else:
            axis_y = 100.0
            axis_x = 100.0
        if getattr(self, "axis_x", None) != (0, axis_x) or getattr(
            self, "axis_y", None
        ) != (0, axis_y):
            self.resetzoom()
            wx.CallAfter(self.center)
        self.axis_x, self.axis_y = (0, axis_x), (0, axis_y)
        if not self.last_draw:
            self.center_x = axis_x / 2.0
            self.center_y = axis_y / 2.0
        self.proportional = False
        self.spec_x = self.spec_y = 5

        lines = [PolyBox(0, 0, axis_x, axis_y, colour=GRIDCOLOUR, width=1)]

        # Use a point grid, so we can get the whole set of output values for
        # each channel for any single given input value.
        # The point grid keys are quantized to 12 bits to avoid floating point
        # inaccuracy
        self.point_grid = {}
        points = {}
        if not channels:
            channels = {}
        for channel in channels:
            channel_name = channels[channel]
            if channel_name:
                self.point_grid[channel] = {}
                points[channel] = []

        if not vcgt:
            irange = list(range(0, 256))
        elif "data" in vcgt:  # table
            data = list(vcgt["data"])
            while len(data) < len(channels):
                data.append(data[0])
            if not isinstance(vcgt, VideoCardGammaTableType) and not isinstance(
                data[0], CurveType
            ):
                # Coordinate list
                irange = list(range(0, len(data[0])))
                set_linear_points = True
                for channel in range(len(data)):
                    if channel in points:
                        for n, y in data[channel]:
                            if not detect_increments and set_linear_points:
                                linear_points.append([n, n])
                            if connection_colorspace == b"Lab":
                                n = colormath.XYZ2Lab(0, n / axis_x * 100, 0)[0] * (
                                    axis_x / 100.0
                                )
                            points[channel].append([n, y])
                            idx = int(round(n / 255.0 * 4095))
                            self.point_grid[channel][idx] = y
                        set_linear_points = False
            else:
                irange = list(range(0, vcgt["entryCount"]))
                maxv = math.pow(256, vcgt["entrySize"]) - 1
                for i in irange:
                    j = i * (axis_y / (vcgt["entryCount"] - 1))
                    if not detect_increments:
                        linear_points.append([j, j])
                    for channel in points:
                        values = points[channel]
                        n = float(data[channel][i]) / maxv * axis_x
                        if (
                            not detect_increments
                            or not values
                            or i == vcgt["entryCount"] - 1
                            or n != i
                        ):
                            if (
                                detect_increments
                                and n != i
                                and len(values) == 1
                                and i > 1
                                and values[-1][0] == values[-1][1]
                            ):
                                values.append([i - 1, i - 1])
                            if connection_colorspace == b"Lab":
                                n = colormath.XYZ2Lab(0, n / axis_x * 100, 0)[0] * (
                                    axis_x / 100.0
                                )
                            if connection_colorspace in (b"Lab", b"XYZ"):
                                values.append([n, j])
                                idx = int(round(n / 255.0 * 4095))
                                self.point_grid[channel][idx] = j
                            else:
                                values.append([j, n])
                                idx = int(round(j / 255.0 * 4095))
                                self.point_grid[channel][idx] = n
        else:  # formula
            irange = list(range(0, 256))
            step = 100.0 / axis_y
            for i in irange:
                # float2dec(v) fixes miniscule deviations in the calculated gamma
                # n = float2dec(n, 8)
                if not detect_increments:
                    linear_points.append([i, i])
                for channel, color in enumerate(("red", "green", "blue")):
                    if channel not in points:
                        continue
                    vmin = float2dec(vcgt[f"{color}Min"] * axis_x)
                    v = float2dec(math.pow(step * i / 100.0, vcgt[f"{color}Gamma"]))
                    vmax = float2dec(vcgt[f"{color}Max"] * axis_x)
                    n = vmin + v * (vmax - vmin)
                    if connection_colorspace == b"Lab":
                        n = colormath.XYZ2Lab(0, float(n) / axis_x * 100, 0)[0] * (
                            axis_x / 100.0
                        )
                    if connection_colorspace in (b"Lab", b"XYZ"):
                        points[channel].append([n, i])
                        idx = int(round(float(n) / 255.0 * 4095))
                        self.point_grid[channel][idx] = i
                    else:
                        points[channel].append([i, n])
                        idx = int(round(i / 255.0 * 4095))
                        self.point_grid[channel][idx] = float(n)

        # for n in sorted(self.point_grid[0].keys()):
        # print n, self.point_grid[0].get(n), self.point_grid[1].get(n), self.point_grid[2].get(n)

        self.entryCount = irange[-1] + 1

        linear = [[0, 0], [irange[-1], irange[-1]]]

        if not vcgt:
            for values in points.values():
                if detect_increments:
                    values[:] = linear
                else:
                    values[:] = linear_points

        # Note: We need to make sure each point is a float because it
        # might be a decimal.Decimal, which can't be divided by floats!
        self.unique = {}
        for channel in points:
            values = points[channel]
            self.unique[channel] = len(
                set(round(float(y) / axis_y * irange[-1]) for x, y in values)
            )

        legend = []
        color = "white"

        if len(points) > 1:
            values0 = list(points.values())[0]
            # identical = all(values == values0
            # for values in points.itervalues())
            identical = all(
                all(
                    x == values0[i][0] and abs(y - values0[i][1]) < 0.005
                    for i, (x, y) in enumerate(values)
                )
                for values in points.values()
            )
        else:
            identical = False

        if identical:
            channels_label = "".join(list(channels.values()))
            color = self.get_color(channels_label, color)
        for channel_name in list(channels.values()):
            if channel_name:
                legend.append(channel_name)
        linear_points = [(i, int(round(v / axis_y * maxv))) for i, v in linear_points]
        seen_values = []
        seen_labels = []
        for channel in points:
            values = points[channel]
            channel_label = channels[channel]
            if not identical:
                color = self.colors.get(
                    f"{colorspace.decode('utf-8')}_{channel_label}", "white"
                )
            # Note: We need to make sure each point is a float because it
            # might be a decimal.Decimal, which can't be divided by floats!
            points_quantized = [
                (i, int(round(float(v) / axis_y * maxv))) for i, v in values
            ]
            line2 = None
            if identical:
                label = "=".join(legend)
                suffix = (
                    (", {}".format(lang.getstr("linear").capitalize()))
                    if points_quantized
                    == (linear if detect_increments else linear_points)
                    else ""
                )
            else:
                label = channel_label
                suffix = ""
                if channel_label in ("a*", "b*") and len(values) > 1:
                    half = len(values) / 2
                    values2 = values[:half]
                    values = values[half:]
                    center_x = (values[0][0] + values2[-1][0]) / 2.0
                    center_y = (values[0][1] + values2[-1][1]) / 2.0
                    center = [center_x, center_y]
                    if values[0] != center:
                        values.insert(0, center)
                    if values2[-1] != center:
                        values2.append(center)
                    idx = int(round(center_x / 255.0 * 4095))
                    self.point_grid[channel][idx] = center_y
                    color2 = self.colors["Lab_%s-" % channel_label]
                    line2 = Plot(values2, legend=label + suffix, colour=color2)
                elif seen_values:
                    # Check if same line (+- 0.005 tolerance) has been seen
                    for idx, seen in enumerate(seen_values):
                        match = True
                        for i, (x, y) in enumerate(seen):
                            if x != values[i][0] or abs(y - values[i][1]) > 0.005:
                                match = False
                                break
                        if match:
                            break
                    else:
                        match = False
                    if match:
                        seen_label = seen_labels[idx]
                        lines[idx + 1].attributes["colour"] = self.get_color(
                            seen_label + channel_label
                        )
                        continue
            line = Plot(values, legend=label + suffix, colour=color)
            if (colorspace == "CMYK" and label == "K") or (
                colorspace == "Lab" and label in ("a*", "b*")
            ):
                # CMYK -> KCMY, Lab -> baL for better visibilty
                lines.insert(1, line)
                seen_values.insert(0, values)
                seen_labels.insert(0, label)
                if line2:
                    lines.insert(1, line2)
            else:
                lines.append(line)
                seen_values.append(values)
                seen_labels.append(label)
                if line2:
                    lines.append(line2)
            if identical:
                break

        self._DrawCanvas(
            plot.PlotGraphics(
                lines,
                title,
                " ".join([xLabel, lang.getstr("in")]),
                " ".join([yLabel, lang.getstr("out")]),
            )
        )

    def get_color(self, channels_label, default="white"):
        if channels_label in ("RGB", "CMYK", "XYZ"):
            return "white"
        elif channels_label in ("RG", "XY"):
            return "yellow"
        elif channels_label in ("RB", "XZ"):
            return "magenta"
        elif channels_label in ("GB", "YZ"):
            return "cyan"
        elif channels_label == "CM":
            return "#0080FF"
        elif channels_label == "CY":
            return "#00FF00"
        elif channels_label == "MY":
            return "red"
        return default

    def DrawPointLabel(self, dc, mDataDict):
        """Draw point labels.

        dc - DC that will be passed
        mDataDict - Dictionary of data that you want to use for the pointLabel

        """
        if not self.last_draw:
            return
        graphics, xAxis, yAxis = self.last_draw
        # sizes axis to axis type, create lower left and upper right corners of plot
        if xAxis is None or yAxis is None:
            # One or both axis not specified in Draw
            p1, p2 = graphics.boundingBox()  # min, max points of graphics
            if xAxis is None:
                xAxis = self._axisInterval(self._xSpec, p1[0], p2[0])  # in user units
            if yAxis is None:
                yAxis = self._axisInterval(self._ySpec, p1[1], p2[1])
            # Adjust bounding box for axis spec
            p1[0], p1[1] = (
                xAxis[0],
                yAxis[0],
            )  # lower left corner user scale (xmin,ymin)
            p2[0], p2[1] = (
                xAxis[1],
                yAxis[1],
            )  # upper right corner user scale (xmax,ymax)
        else:
            # Both axis specified in Draw
            p1 = numpy.array(
                [xAxis[0], yAxis[0]]
            )  # lower left corner user scale (xmin,ymin)
            p2 = numpy.array(
                [xAxis[1], yAxis[1]]
            )  # upper right corner user scale (xmax,ymax)
        ptx, pty, rectWidth, rectHeight = self._point2ClientCoord(p1, p2)
        # allow graph to overlap axis lines by adding units to width and height
        dc.SetClippingRegion(
            int(ptx), int(pty), int(rectWidth + 2), int(rectHeight + 2)
        )

        dc.SetPen(wx.Pen(wx.WHITE, 1, wx.DOT))
        dc.SetBrush(wx.Brush(wx.WHITE, wx.SOLID))

        sx, sy = mDataDict["scaledXY"]  # Scaled x, y of closest point
        dc.DrawLine(0, int(sy), int(ptx + rectWidth + 2), int(sy))
        dc.DrawLine(int(sx), 0, int(sx), int(pty + rectHeight + 2))

    def GetClosestPoints(self, pntXY, pointScaled=True):
        """Returns list with
        [curveNumber, legend, index of closest point, pointXY, scaledXY, distance]
        list for each curve.
        Returns [] if no curves are being plotted.

        x, y in user coords
        if pointScaled == True based on screen coords
        if pointScaled == False based on user coords
        """
        if self.last_draw is None:
            # no graph available
            return []
        graphics, xAxis, yAxis = self.last_draw
        l = []
        for curveNum, obj in enumerate(graphics):
            # check there are points in the curve
            if len(obj.points) == 0 or isinstance(obj, PolyBox):
                continue  # go to next obj
            # [curveNumber, legend, index of closest point, pointXY, scaledXY, distance]
            cn = (
                [curveNum] + [obj.getLegend()] + obj.getClosestPoint(pntXY, pointScaled)
            )
            l.append(cn)
        return l

    def _disabledoublebuffer(self, event):
        window = self
        while window:
            if not isinstance(window, wx.TopLevelWindow):
                window.SetDoubleBuffered(False)
            window = window.Parent
        event.Skip()

    def _enabledoublebuffer(self, event):
        window = self
        while window:
            if not isinstance(window, wx.TopLevelWindow):
                window.SetDoubleBuffered(True)
            window = window.Parent
        event.Skip()

    def OnMouseDoubleClick(self, event):
        if self.last_draw:
            boundingbox = self.last_draw[0].boundingBox()
        else:
            boundingbox = None
        self.resetzoom(boundingbox=boundingbox)
        if self.last_draw:
            self.center()

    def OnMouseLeftDown(self, event):
        self.erase_pointlabel()
        self._zoomCorner1[0], self._zoomCorner1[1] = self._getXY(event)
        self._screenCoordinates = numpy.array(event.GetPosition())
        if self._dragEnabled:
            self.SetCursor(self.GrabHandCursor)
            self.canvas.CaptureMouse()

    def OnMouseLeftUp(self, event):
        if self._dragEnabled:
            self.SetCursor(self.HandCursor)
            if self.canvas.HasCapture():
                self.canvas.ReleaseMouse()
                self._set_center()
        if hasattr(self.TopLevelParent, "OnMotion"):
            self.TopLevelParent.OnMotion(event)

    def _DrawCanvas(self, graphics):
        """Draw proportionally correct, center and zoom"""
        w = float(self.GetSize()[0] or 1)
        h = float(self.GetSize()[1] or 1)
        if w > 45:
            w -= 45
        if h > 20:
            h -= 20
        ratio = [w / h, h / w]
        axis_x, axis_y = self.axis_x, self.axis_y
        spec_x = self.spec_x
        spec_y = self.spec_y
        if self._zoomfactor < 1:
            while spec_x * self._zoomfactor < self.spec_x / 2.0:
                spec_x *= 2
            while spec_y * self._zoomfactor < self.spec_y / 2.0:
                spec_y *= 2
        else:
            while spec_x * self._zoomfactor > self.spec_x * 2:
                spec_x /= 2
            while spec_y * self._zoomfactor > self.spec_y * 2:
                spec_y /= 2
        if self.proportional:
            if ratio[0] > ratio[1]:
                self.SetXSpec(spec_x * self._zoomfactor * ratio[0])
            else:
                self.SetXSpec(spec_x * self._zoomfactor)
            if ratio[0] > 1:
                axis_x = tuple([v * ratio[0] for v in axis_x])
            if ratio[1] > ratio[0]:
                self.SetYSpec(spec_y * self._zoomfactor * ratio[1])
            else:
                self.SetYSpec(spec_y * self._zoomfactor)
            if ratio[1] > 1:
                axis_y = tuple([v * ratio[1] for v in axis_y])
        else:
            self.SetXSpec(spec_x * self._zoomfactor)
            self.SetYSpec(spec_y * self._zoomfactor)
        x, y = self.center_x, self.center_y
        w = (axis_x[1] - axis_x[0]) * self._zoomfactor
        h = (axis_y[1] - axis_y[0]) * self._zoomfactor
        axis_x = (x - w / 2, x + w / 2)
        axis_y = (y - h / 2, y + h / 2)
        self.Draw(graphics, axis_x, axis_y)

    def _set_center(self):
        """Set center position from current X and Y axis"""
        if not self.last_draw:
            return
        axis_x = self.GetXCurrentRange()
        axis_y = self.GetYCurrentRange()
        if axis_x[0] < 0:
            if axis_x[1] < 0:
                x = axis_x[0] + (abs(axis_x[0]) - abs(axis_x[1])) / 2.0
            else:
                x = axis_x[0] + (abs(axis_x[1]) + abs(axis_x[0])) / 2.0
        else:
            x = axis_x[0] + (abs(axis_x[1]) - abs(axis_x[0])) / 2.0
        if axis_y[0] < 0:
            if axis_y[1] < 0:
                y = axis_y[0] + (abs(axis_y[0]) - abs(axis_y[1])) / 2.0
            else:
                y = axis_y[0] + (abs(axis_y[1]) + abs(axis_y[0])) / 2.0
        else:
            y = axis_y[0] + (abs(axis_y[1]) - abs(axis_y[0])) / 2.0
        self.center_x, self.center_y = x, y

    def center(self, boundingbox=None):
        """Center the current graphic"""
        if boundingbox:
            # Min, max points of graphics
            p1, p2 = boundingbox
            # In user units
            min_x, max_x = self._axisInterval(self._xSpec, p1[0], p2[0])
            min_y, max_y = self._axisInterval(self._ySpec, p1[1], p2[1])
        else:
            min_x, max_x = self.GetXMaxRange()
            min_y, max_y = self.GetYMaxRange()
        self.center_x = 0 + sum((min_x, max_x)) / 2.0
        self.center_y = 0 + sum((min_y, max_y)) / 2.0
        if not boundingbox:
            self.erase_pointlabel()
            self._DrawCanvas(self.last_draw[0])

    def erase_pointlabel(self):
        if self.GetEnablePointLabel() and self.last_PointLabel:
            # Erase point label
            self._drawPointLabel(self.last_PointLabel)
            self.last_PointLabel = None

    def resetzoom(self, boundingbox=None):
        self.center_x = 0
        self.center_y = 0
        self._zoomfactor = 1.0
        if boundingbox:
            # Min, max points of graphics
            p1, p2 = boundingbox
            # In user units
            min_x, max_x = self._axisInterval(self._xSpec, p1[0], p2[0])
            min_y, max_y = self._axisInterval(self._ySpec, p1[1], p2[1])
            max_abs_x = abs(min_x) + max_x
            max_abs_y = abs(min_y) + max_y
            max_abs_axis_x = abs(self.axis_x[0]) + self.axis_x[1]
            max_abs_axis_y = abs(self.axis_y[0]) + self.axis_y[1]
            w = float(self.GetSize()[0] or 1)
            h = float(self.GetSize()[1] or 1)
            if w > 45:
                w -= 45
            if h > 20:
                h -= 20
            ratio = [w / h, h / w]
            if ratio[0] > 1:
                max_abs_axis_x *= ratio[0]
            if ratio[1] > 1:
                max_abs_axis_y *= ratio[1]
            if max_abs_x / max_abs_y > max_abs_axis_x / max_abs_axis_y:
                self._zoomfactor = max_abs_x / max_abs_axis_x
            else:
                self._zoomfactor = max_abs_y / max_abs_axis_y

    def zoom(self, direction=1.0):
        _zoomfactor = 0.025 * direction
        if 0 < self._zoomfactor + _zoomfactor <= 5:
            self._zoomfactor += _zoomfactor
            self._set_center()
            self.erase_pointlabel()
            self._DrawCanvas(self.last_draw[0])


class LUTFrame(BaseFrame):
    """LUTFrame."""

    def __init__(self, *args, **kwargs):
        if len(args) < 3 and "title" not in kwargs:
            kwargs["title"] = lang.getstr("calibration.lut_viewer.title")
        if "name" not in kwargs:
            kwargs["name"] = "lut_viewer"

        BaseFrame.__init__(self, *args, **kwargs)

        self.SetIcons(
            config.get_icon_bundle([256, 48, 32, 16], f"{appname}-curve-viewer")
        )

        self.profile = None
        self.xLabel = lang.getstr("in")
        self.yLabel = lang.getstr("out")

        self.sizer = wx.BoxSizer(wx.VERTICAL)
        self.SetSizer(self.sizer)

        panel = wx_Panel(self)
        panel.SetBackgroundColour(BGCOLOUR)
        self.sizer.Add(panel, flag=wx.EXPAND)
        panel.SetSizer(wx.FlexGridSizer(0, 5, 0, 8))
        panel.Sizer.AddGrowableCol(0)
        panel.Sizer.AddGrowableCol(4)

        panel.Sizer.Add((1, 1))
        panel.Sizer.Add((1, 12))
        panel.Sizer.Add((1, 12))
        panel.Sizer.Add((1, 12))
        panel.Sizer.Add((1, 1))

        panel.Sizer.Add((1, 1))

        self.plot_mode_select = wx.Choice(panel, -1, size=(-1, -1), choices=[])
        panel.Sizer.Add(self.plot_mode_select, flag=wx.ALIGN_CENTER_VERTICAL)
        self.Bind(
            wx.EVT_CHOICE,
            self.plot_mode_select_handler,
            id=self.plot_mode_select.GetId(),
        )

        self.tooltip_btn = BitmapButton(
            panel, -1, geticon(16, "question-inverted"), style=wx.NO_BORDER
        )
        self.tooltip_btn.SetBackgroundColour(BGCOLOUR)
        self.tooltip_btn.Bind(wx.EVT_BUTTON, self.tooltip_handler)
        self.tooltip_btn.SetToolTipString(lang.getstr("gamut_plot.tooltip"))
        panel.Sizer.Add(self.tooltip_btn, flag=wx.ALIGN_CENTER_VERTICAL)

        self.save_plot_btn = BitmapButton(
            panel, -1, geticon(16, "image-x-generic-inverted"), style=wx.NO_BORDER
        )
        self.save_plot_btn.SetBackgroundColour(BGCOLOUR)
        panel.Sizer.Add(self.save_plot_btn, flag=wx.ALIGN_CENTER_VERTICAL)
        self.save_plot_btn.Bind(wx.EVT_BUTTON, self.SaveFile)
        self.save_plot_btn.SetToolTipString(
            f"{lang.getstr('save_as')} (*.bmp, *.xbm, *.xpm, *.jpg, *.png)"
        )
        self.save_plot_btn.Disable()

        panel.Sizer.Add((1, 1))

        panel.Sizer.Add((1, 1))
        panel.Sizer.Add((1, 4))
        panel.Sizer.Add((1, 4))
        panel.Sizer.Add((1, 4))
        panel.Sizer.Add((1, 1))

        self.client = LUTCanvas(self)
        self.sizer.Add(self.client, 1, wx.EXPAND)

        self.box_panel = wx_Panel(self)
        self.box_panel.SetBackgroundColour(BGCOLOUR)
        self.sizer.Add(self.box_panel, flag=wx.EXPAND)

        self.status = BitmapBackgroundPanelText(self, name="statuspanel")
        self.gamut_status = BitmapBackgroundPanelTextGamut(self, name="statuspanel")
        self.status.SetMaxFontSize(11)
        self.status.label_y = 8
        self.status.textshadow = False
        self.status.SetBackgroundColour(BGCOLOUR)
        self.status.SetForegroundColour(FGCOLOUR)
        h = self.status.GetTextExtent("Ig")[1]
        self.status.SetMinSize((0, h * 2 + 18))
        self.sizer.Add(self.status, flag=wx.EXPAND)

        self.box_sizer = wx.FlexGridSizer(0, 3, 4, 4)
        self.box_sizer.AddGrowableCol(0)
        self.box_sizer.AddGrowableCol(2)
        self.box_panel.SetSizer(self.box_sizer)

        self.box_sizer.Add((0, 0))

        hsizer = wx.BoxSizer(wx.HORIZONTAL)

        self.box_sizer.Add(
            hsizer,
            # flag=wx.ALIGN_CENTER | wx.BOTTOM | wx.TOP, border=4)
            flag=wx.BOTTOM | wx.TOP,
            border=4,
        )

        self.rendering_intent_select = wx.Choice(
            self.box_panel,
            -1,
            choices=[
                lang.getstr("gamap.intents.a"),
                lang.getstr("gamap.intents.r"),
                lang.getstr("gamap.intents.p"),
                lang.getstr("gamap.intents.s"),
            ],
        )
        hsizer.Add((10, self.rendering_intent_select.Size[1]))
        hsizer.Add(self.rendering_intent_select, flag=wx.ALIGN_CENTER_VERTICAL)
        self.rendering_intent_select.Bind(
            wx.EVT_CHOICE, self.rendering_intent_select_handler
        )
        self.rendering_intent_select.SetSelection(1)

        self.direction_select = wx.Choice(
            self.box_panel,
            -1,
            choices=[
                lang.getstr("direction.backward"),
                lang.getstr("direction.forward.inverted"),
            ],
        )
        hsizer.Add(
            self.direction_select, flag=wx.ALIGN_CENTER_VERTICAL | wx.LEFT, border=10
        )
        self.direction_select.Bind(wx.EVT_CHOICE, self.direction_select_handler)
        self.direction_select.SetSelection(0)

        self.show_actual_lut_cb = CustomCheckBox(
            self.box_panel, -1, lang.getstr("calibration.show_actual_lut")
        )
        self.show_actual_lut_cb.SetForegroundColour(FGCOLOUR)
        self.show_actual_lut_cb.SetMaxFontSize(11)
        hsizer.Add(
            self.show_actual_lut_cb,
            # flag=wx.ALIGN_CENTER |
            flag=wx.ALIGN_CENTER_VERTICAL,
        )
        self.show_actual_lut_cb.Bind(wx.EVT_CHECKBOX, self.show_actual_lut_handler)

        self.box_sizer.Add((0, 0))

        self.box_sizer.Add((0, 0))

        self.cbox_sizer = wx.BoxSizer(wx.HORIZONTAL)
        self.box_sizer.Add(
            self.cbox_sizer,
            # flag=wx.ALIGN_CENTER | wx.ALIGN_CENTER_VERTICAL | wx.TOP,
            flag=wx.ALIGN_CENTER_VERTICAL | wx.TOP,
            border=4,
        )

        self.box_sizer.Add((0, 0))

        self.cbox_sizer.Add((10, 0))

        self.reload_vcgt_btn = BitmapButton(
            self.box_panel,
            -1,
            geticon(16, "stock_refresh-inverted"),
            style=wx.NO_BORDER,
        )
        self.reload_vcgt_btn.SetBackgroundColour(BGCOLOUR)
        self.cbox_sizer.Add(
            self.reload_vcgt_btn,
            # flag=wx.ALIGN_CENTER_VERTICAL | wx.RIGHT,
            flag=wx.ALIGN_CENTER_VERTICAL,
            border=8,
        )
        self.reload_vcgt_btn.Bind(wx.EVT_BUTTON, self.reload_vcgt_handler)
        self.reload_vcgt_btn.SetToolTipString(
            lang.getstr("calibration.load_from_display_profile")
        )
        self.reload_vcgt_btn.Disable()

        self.apply_bpc_btn = BitmapButton(
            self.box_panel, -1, geticon(16, "color-inverted"), style=wx.NO_BORDER
        )
        self.apply_bpc_btn.SetBackgroundColour(BGCOLOUR)
        self.cbox_sizer.Add(
            self.apply_bpc_btn,
            # flag=wx.ALIGN_CENTER_VERTICAL | wx.RIGHT,
            flag=wx.ALIGN_CENTER_VERTICAL,
            border=8,
        )
        self.apply_bpc_btn.Bind(wx.EVT_BUTTON, self.apply_bpc_handler)
        self.apply_bpc_btn.SetToolTipString(lang.getstr("black_point_compensation"))
        self.apply_bpc_btn.Disable()

        self.install_vcgt_btn = BitmapButton(
            self.box_panel, -1, geticon(16, "install-inverted"), style=wx.NO_BORDER
        )
        self.install_vcgt_btn.SetBackgroundColour(BGCOLOUR)
        self.cbox_sizer.Add(
            self.install_vcgt_btn,
            # flag=wx.ALIGN_CENTER_VERTICAL | wx.RIGHT,
            flag=wx.ALIGN_CENTER_VERTICAL,
            border=8,
        )
        self.install_vcgt_btn.Bind(wx.EVT_BUTTON, self.install_vcgt_handler)
        self.install_vcgt_btn.SetToolTipString(lang.getstr("apply_cal"))
        self.install_vcgt_btn.Disable()

        self.save_vcgt_btn = BitmapButton(
            self.box_panel,
            -1,
            geticon(16, "document-save-as-inverted"),
            style=wx.NO_BORDER,
        )
        self.save_vcgt_btn.SetBackgroundColour(BGCOLOUR)
        self.cbox_sizer.Add(
            self.save_vcgt_btn,
            # flag=wx.ALIGN_CENTER_VERTICAL | wx.RIGHT,
            flag=wx.ALIGN_CENTER_VERTICAL,
            border=20,
        )
        self.save_vcgt_btn.Bind(wx.EVT_BUTTON, self.SaveFile)
        self.save_vcgt_btn.SetToolTipString(f"{lang.getstr('save_as')} (*.cal)")
        self.save_vcgt_btn.Disable()

        self.show_as_L = CustomCheckBox(self.box_panel, -1, "L* \u2192")
        self.show_as_L.SetForegroundColour(FGCOLOUR)
        self.show_as_L.SetMaxFontSize(11)
        self.show_as_L.SetValue(True)
        self.cbox_sizer.Add(
            self.show_as_L,
            # flag=wx.ALIGN_CENTER_VERTICAL | wx.RIGHT,
            flag=wx.ALIGN_CENTER_VERTICAL,
            border=4,
        )
        self.show_as_L.Bind(wx.EVT_CHECKBOX, self.DrawLUT)

        self.add_toggles(self.box_panel, self.cbox_sizer)

        self.toggle_clut = CustomCheckBox(self.box_panel, -1, "LUT")
        self.toggle_clut.SetForegroundColour(FGCOLOUR)
        self.toggle_clut.SetMaxFontSize(11)
        self.cbox_sizer.Add(
            self.toggle_clut,
            # flag=wx.ALIGN_CENTER_VERTICAL | wx.LEFT,
            flag=wx.ALIGN_CENTER_VERTICAL,
            border=16,
        )
        self.toggle_clut.Bind(wx.EVT_CHECKBOX, self.toggle_clut_handler)

        self.client.canvas.Bind(wx.EVT_MOTION, self.OnMotion)

        self.droptarget = FileDrop(self)
        self.droptarget.drophandlers = {
            ".cal": self.drop_handler,
            ".icc": self.drop_handler,
            ".icm": self.drop_handler,
        }
        self.client.SetDropTarget(self.droptarget)

        border, titlebar = get_platform_window_decoration_size()
        self.MinSize = (
            config.defaults["size.lut_viewer.w"] + border * 2,
            config.defaults["size.lut_viewer.h"] + titlebar + border,
        )
        self.SetSaneGeometry(
            getcfg("position.lut_viewer.x"),
            getcfg("position.lut_viewer.y"),
            getcfg("size.lut_viewer.w"),
            getcfg("size.lut_viewer.h"),
        )

        self.Bind(wx.EVT_MOVE, self.OnMove)
        self.Bind(wx.EVT_SIZE, self.OnSize)

        children = list(self.GetAllChildren())

        self.Bind(wx.EVT_KEY_DOWN, self.key_handler)
        for child in children:
            if isinstance(child, wx.Choice):
                child.SetMaxFontSize(11)
            child.Bind(wx.EVT_KEY_DOWN, self.key_handler)
            child.Bind(wx.EVT_MOUSEWHEEL, self.OnWheel)
            if (
                sys.platform == "win32"
                and sys.getwindowsversion() >= (6,)
                and isinstance(child, wx.Panel)
            ):
                # No need to enable double buffering under Linux and Mac OS X.
                # Under Windows, enabling double buffering on the panel seems
                # to work best to reduce flicker.
                child.SetDoubleBuffered(True)

        self.display_no = -1
        self.display_rects = get_display_rects()

    def apply_bpc_handler(self, event):
        cal = vcgt_to_cal(self.profile)
        cal.filename = self.profile.fileName or ""
        cal.apply_bpc(weight=True)
        self.LoadProfile(cal_to_fake_profile(cal))

    def drop_handler(self, path):
        """Drag'n'drop handler for .cal/.icc/.icm files."""
        filename, ext = os.path.splitext(path)
        if ext.lower() not in (".icc", ".icm"):
            profile = cal_to_fake_profile(path)
            if not profile:
                InfoDialog(
                    self,
                    msg=lang.getstr("error.file.open", path),
                    ok=lang.getstr("ok"),
                    bitmap=geticon(32, "dialog-error"),
                )
                profile = None
        else:
            try:
                profile = ICCProfile(path)
            except (IOError, ICCProfileInvalidError):
                InfoDialog(
                    self,
                    msg=f"{lang.getstr('profile.invalid')}\n{path}",
                    ok=lang.getstr("ok"),
                    bitmap=geticon(32, "dialog-error"),
                )
                profile = None
        self.show_actual_lut_cb.SetValue(False)
        self.current_cal = profile
        self.LoadProfile(profile)

    get_display = MeasureFrame.__dict__["get_display"]

    def handle_errors(self):
        if self.client.errors:
            show_result_dialog(
                Error(
                    "\n\n".join(
                        [
                            "{}".format(e)
                            for e in set(self.client.errors)
                            if e is not None
                        ]
                    )
                ),
                self,
            )
            self.client.errors = []

    def install_vcgt_handler(self, event):
        cwd = self.worker.create_tempdir()
        if isinstance(cwd, Exception):
            show_result_dialog(cwd, self)
        else:
            cal = os.path.join(
                cwd,
                re.sub(
                    r"[\\/:*?\"<>|]+",
                    "",
                    make_argyll_compatible_path(
                        self.profile.getDescription() or "Video LUT"
                    ),
                ),
            )
            vcgt_to_cal(self.profile).write(cal)
            cmd, args = self.worker.prepare_dispwin(cal)
            if isinstance(cmd, Exception):
                show_result_dialog(cmd, self)
            elif cmd:
                result = self.worker.exec_cmd(
                    cmd, args, capture_output=True, skip_scripts=True
                )
                if isinstance(result, Exception):
                    show_result_dialog(result, self)
                elif not result:
                    show_result_dialog(Error("".join(self.worker.errors)), self)
            # Important:
            # Make sure to only delete the temporary cal file we created
            try:
                os.remove(cal)
            except Exception as exception:
                print(
                    "Warning - temporary file '%s' could not be removed: %s"
                    % (cal, exception)
                )

    def key_handler(self, event):
        # AltDown
        # CmdDown
        # ControlDown
        # GetKeyCode
        # GetModifiers
        # GetPosition
        # GetRawKeyCode
        # GetRawKeyFlags
        # GetUniChar
        # GetUnicodeKey
        # GetX
        # GetY
        # HasModifiers
        # KeyCode
        # MetaDown
        # Modifiers
        # Position
        # RawKeyCode
        # RawKeyFlags
        # ShiftDown
        # UnicodeKey
        # X
        # Y
        key = event.GetKeyCode()
        if (
            event.ControlDown() or event.CmdDown()
        ):  # CTRL (Linux/Mac/Windows) / CMD (Mac)
            if key == 83 and self.profile:  # S
                self.SaveFile()
                return
            else:
                event.Skip()
        elif key in (43, wx.WXK_NUMPAD_ADD):
            # + key zoom in
            self.client.zoom(-1)
        elif key in (45, wx.WXK_NUMPAD_SUBTRACT):
            # - key zoom out
            self.client.zoom(1)
        else:
            event.Skip()

    def direction_select_handler(self, event):
        self.toggle_clut_handler(event)

    def rendering_intent_select_handler(self, event):
        self.toggle_clut_handler(event)

    def toggle_clut_handler(self, event):
        try:
            self.lookup_tone_response_curves()
        except Exception as exception:
            import traceback

            print(traceback.format_exc())
            show_result_dialog(exception, self)
        else:
            self.trc = None
            self.DrawLUT()
            self.handle_errors()

    def tooltip_handler(self, event):
        if not hasattr(self, "tooltip_window"):
            self.tooltip_window = TooltipWindow(
                self,
                msg=event.EventObject.ToolTip.Tip,
                title=event.EventObject.TopLevelParent.Title,
                bitmap=geticon(32, "dialog-information"),
            )
        else:
            self.tooltip_window.Show()
            self.tooltip_window.Raise()

    def show_actual_lut_handler(self, event):
        setcfg("lut_viewer.show_actual_lut", int(self.show_actual_lut_cb.GetValue()))
        if hasattr(self, "current_cal"):
            profile = self.current_cal
        else:
            profile = None
        self.load_lut(profile=profile)

    def load_lut(self, profile=None):
        self.current_cal = profile
        if (
            getcfg("lut_viewer.show_actual_lut")
            and (
                self.worker.argyll_version[0:3] > [1, 1, 0]
                or (
                    self.worker.argyll_version[0:3] == [1, 1, 0]
                    and "Beta" not in self.worker.argyll_version_string
                )
            )
            and not config.is_untethered_display()
        ):
            tmp = self.worker.create_tempdir()
            if isinstance(tmp, Exception):
                show_result_dialog(tmp, self)
                return
            outfilename = os.path.join(
                tmp,
                re.sub(
                    r"[\\/:*?\"<>|]+",
                    "",
                    make_argyll_compatible_path(
                        config.get_display_name(include_geometry=True) or "Video LUT"
                    ),
                ),
            )
            result = self.worker.save_current_video_lut(
                self.worker.get_display(),
                outfilename,
                silent=not __name__ == "__main__",
            )
            if not isinstance(result, Exception) and result:
                profile = cal_to_fake_profile(outfilename)
            else:
                if isinstance(result, Exception):
                    print(result)
            # Important: lut_viewer_load_lut is called after measurements,
            # so make sure to only delete the temporary cal file we created
            try:
                os.remove(outfilename)
            except Exception as exception:
                print(
                    "Warning - temporary file '%s' could not be removed: %s"
                    % (outfilename, exception)
                )
        if profile and (
            profile.is_loaded
            or not profile.fileName
            or os.path.isfile(profile.fileName)
        ):
            if (
                not self.profile
                or self.profile.fileName != profile.fileName
                or not self.profile.isSame(profile)
            ):
                self.LoadProfile(profile)
        else:
            self.LoadProfile(None)

    def lookup_tone_response_curves(self, intent="r"):
        """Lookup Y -> RGB tone values through TRC tags or LUT"""

        profile = self.profile

        # Final number of coordinates
        if profile.connectionColorSpace == b"RGB":
            size = 256
        elif profile.connectionColorSpace == b"Lab":
            size = 1001
        else:
            size = 1024

        if hasattr(self, "rendering_intent_select"):
            intent = {0: "a", 1: "r", 2: "p", 3: "s"}.get(
                self.rendering_intent_select.GetSelection()
            )

        use_trc_tags = (
            intent == "r"
            and (
                not ("B2A0" in self.profile.tags or "A2B0" in self.profile.tags)
                or not self.toggle_clut.GetValue()
            )
            and isinstance(self.rTRC, CurveType)
            and isinstance(self.gTRC, CurveType)
            and isinstance(self.bTRC, CurveType)
            and len(self.rTRC) == len(self.gTRC) == len(self.bTRC)
        )
        has_same_trc = self.rTRC == self.gTRC == self.bTRC

        if profile.version >= 4 and not profile.convert_iccv4_tags_to_iccv2():
            self.client.errors.append(
                Error(
                    "\n".join(
                        [
                            lang.getstr("profile.iccv4.unsupported"),
                            profile.getDescription(),
                        ]
                    )
                )
            )
            return

        if profile.colorSpace not in (
            b"RGB",
            b"GRAY",
            b"CMYK",
        ) or profile.connectionColorSpace not in (b"Lab", b"XYZ", b"RGB"):
            if profile.colorSpace not in (b"RGB", b"GRAY", b"CMYK"):
                unsupported_colorspace = profile.colorSpace
            else:
                unsupported_colorspace = profile.connectionColorSpace
            self.client.errors.append(
                Error(
                    lang.getstr(
                        "profile.unsupported",
                        (profile.profileClass, unsupported_colorspace),
                    )
                )
            )
            return

        if profile.colorSpace == b"GRAY":
            direction = "b"
        elif profile.connectionColorSpace == b"RGB":
            direction = "f"
        elif "B2A0" in profile.tags:
            index = self.direction_select.GetSelection()
            if index == -1:  # Fix for #31
                index = 0
                self.direction_select.SetSelection(index)
            direction = {0: "b", 1: "if", 2: "f", 3: "ib"}.get(
                self.direction_select.GetSelection()
            )
        else:
            direction = "if"

        # Prepare input Lab values
        XYZ_triplets = []
        Lab_triplets = []
        devicevalues = []
        for i in range(0, size):
            if direction in ("b", "if"):
                if intent == "a":
                    # For display profiles, identical to relcol
                    # For print profiles, makes max L* match paper white
                    XYZwp_ir = list(profile.tags.wtpt.ir.values())
                    Labwp_ir = profile.tags.wtpt.ir.Lab
                    XYZwp_D50 = colormath.Lab2XYZ(Labwp_ir[0], 0, 0)
                    X, Y, Z = colormath.Lab2XYZ(
                        min(i * (100.0 / (size - 1)), Labwp_ir[0]), 0, 0
                    )
                    L, a, b = colormath.XYZ2Lab(
                        *[
                            v * 100
                            for v in colormath.adapt(X, Y, Z, XYZwp_D50, XYZwp_ir)
                        ]
                    )
                else:
                    L = i * (100.0 / (size - 1))
                    a = b = 0
                Lab_triplets.append([L, a, b])
            else:
                devicevalues.append([i * (1.0 / (size - 1))] * len(profile.colorSpace))
        if profile.colorSpace == "GRAY":
            use_icclu = True
            pcs = "x"
            for Lab in Lab_triplets:
                XYZ_triplets.append(colormath.Lab2XYZ(*Lab))
        elif profile.connectionColorSpace == b"RGB":
            use_icclu = False
            pcs = None
            intent = None
        else:
            use_icclu = False
            pcs = "l"
        if direction in ("b", "if"):
            if pcs == "l":
                idata = Lab_triplets
            else:
                idata = XYZ_triplets
        else:
            idata = devicevalues

        order = {True: "n", False: "r"}.get(
            ("B2A0" in self.profile.tags or "A2B0" in self.profile.tags)
            and self.toggle_clut.GetValue()
        )

        # Lookup values through 'input' profile using xicclu
        try:
            odata = self.worker.xicclu(
                profile,
                idata,
                intent,
                direction,
                order,
                pcs,
                use_icclu=use_icclu,
                get_clip=direction == "if",
            )
        except Exception as exception:
            self.client.errors.append(Error(exception))

        if self.client.errors:
            return

        if direction in ("b", "if") or profile.connectionColorSpace == b"RGB":
            if direction == "if" and profile.colorSpace == b"RGB":
                Lbp = self.worker.xicclu(
                    profile, [[0, 0, 0]], intent, "f", order, "l", use_icclu=use_icclu
                )[0][0]
                maxval = size - 1.0

                # Deal with values that got clipped (below black as well as white)
                # XXX: We should not clip these when plotting, because our input
                # values are all a = b = 0
                do_low_clip = False
                make_mono = True
                # Keep the max amount of useful information from Lbp onwards
                mono_end = 0
                for i, values in enumerate(odata):
                    if values[3] is True or i == 0:
                        if (do_low_clip and (i / maxval * 100 < Lbp or i == 0)) or (
                            make_mono and i == 0
                        ):
                            # Set to black
                            values[:] = [0.0, 0.0, 0.0]
                        elif i == maxval and [round(v, 4) for v in values[:3]] == [
                            1,
                            1,
                            1,
                        ]:
                            # Set to white
                            values[:] = [1.0, 1.0, 1.0]
                        elif i / maxval * 100 < Lbp:
                            mono_end = i + 2
                    else:
                        # First non-clipping value disables low clipping
                        do_low_clip = False
                    if len(values) > 3:
                        values.pop()

                if mono_end and mono_end < len(odata):
                    # Make segment from first non-zero value to Lbp
                    # monotonically increasing
                    mono = [[], [], []]
                    for i, values in enumerate(odata):
                        for j in range(3):
                            mono[j].append(values[j])
                    for j, values in enumerate(mono):
                        for i, v in enumerate(values):
                            if v:
                                break
                        if i:
                            i -= 1
                        if i + 2 < mono_end:
                            mono[j][i:mono_end] = (
                                colormath.make_monotonically_increasing(
                                    values[i:mono_end]
                                )
                            )
                    odata = []
                    for i in range(len(mono[0])):
                        values = []
                        for j in range(3):
                            values.append(mono[j][i])
                        odata.append(values)

            devicevalues = odata
        else:
            Lab_triplets = odata

        if profile.colorSpace in (b"RGB", b"GRAY"):
            maxv = 255
        else:
            maxv = 100
        self.rTRC = CoordinateType(self.profile)
        self.gTRC = CoordinateType(self.profile)
        self.bTRC = CoordinateType(self.profile)
        self.kTRC = CoordinateType(self.profile)
        for j, sample in enumerate(devicevalues):
            for i, v in enumerate(sample):
                v = min(v, 1.0)
                if (
                    not v
                    and j < len(devicevalues) - 1
                    and not min(devicevalues[j + 1][i], 1.0)
                ):
                    continue
                v *= maxv
                if profile.connectionColorSpace == b"RGB":
                    x = j / (size - 1.0) * maxv
                    if i == 0:
                        self.rTRC.append([x, v])
                    elif i == 1:
                        self.gTRC.append([x, v])
                    elif i == 2:
                        self.bTRC.append([x, v])
                else:
                    X, Y, Z = colormath.Lab2XYZ(*Lab_triplets[j], **{"scale": 100})
                    if direction in ("b", "if"):
                        X = Z = Y
                    elif intent == "a":
                        wp = list(profile.tags.wtpt.ir.values())
                        X, Y, Z = colormath.adapt(X, Y, Z, wp, (1, 1, 1))
                    elif intent != "a":
                        wp = list(profile.tags.wtpt.ir.values())
                        X, Y, Z = colormath.adapt(X, Y, Z, "D50", (1, 1, 1))
                    if i == 0:
                        self.rTRC.append([X, v])
                    elif i == 1:
                        self.gTRC.append([Y, v])
                    elif i == 2:
                        self.bTRC.append([Z, v])
                    elif i == 3:
                        self.kTRC.append([Y, v])
        if profile.connectionColorSpace == b"RGB":
            return
        if use_trc_tags:
            if has_same_trc:
                self.bTRC = self.gTRC = self.rTRC
            return
        # Generate interpolated TRCs for transfer function detection
        prev = None
        for sig in ("rTRC", "gTRC", "bTRC", "kTRC"):
            x, xp, y, yp = [], [], [], []
            # First, get actual values
            for i, (Y, v) in enumerate(getattr(self, sig)):
                # if not i or Y >= trc[sig][i - 1]:
                xp.append(v)
                yp.append(Y)
            setattr(self, f"tf_{sig}", CoordinateType(self.profile))
            if not xp or not yp:
                if prev:
                    getattr(self, f"tf_{sig}")[:] = prev
                continue
            # Second, interpolate to given size and use the same y axis
            # for all channels
            for i in range(size):
                x.append(i / (size - 1.0) * maxv)
                y.append(colormath.Lab2XYZ(i / (size - 1.0) * 100, 0, 0)[1] * 100)
            xi = numpy.interp(y, yp, xp)
            yi = numpy.interp(x, xi, y)
            prev = getattr(self, f"tf_{sig}")
            for Y, v in zip(yi, x):
                if Y <= yp[0]:
                    Y = yp[0]
                prev.append([Y, v])

    def move_handler(self, event):
        if not self.IsShownOnScreen():
            return
        display_no, geometry, client_area = self.get_display()
        if display_no != self.display_no:
            self.display_no = display_no
            # Translate from wx display index to Argyll display index
            n = get_argyll_display_number(geometry)
            if n is not None:
                # Save Argyll display index to configuration
                setcfg("display.number", n + 1)
                # Load profile
                self.load_lut(get_display_profile(n))
        event.Skip()

    def plot_mode_select_handler(self, event):
        # self.client.resetzoom()
        self.DrawLUT()

    def get_commands(self):
        return self.get_common_commands() + [
            "curve-viewer [filename]",
            "load <filename>",
        ]

    def process_data(self, data):
        if (data[0] == "curve-viewer" and len(data) < 3) or (
            data[0] == "load" and len(data) == 2
        ):
            if self.IsIconized():
                self.Restore()
            self.Raise()
            if len(data) == 2:
                path = data[1]
                if not os.path.isfile(path) and not os.path.isabs(path):
                    path = get_data_path(path)
                if not path:
                    return "fail"
                else:
                    self.droptarget.OnDropFiles(0, 0, [path])
            return "ok"
        return "invalid"

    def reload_vcgt_handler(self, event):
        cmd, args = self.worker.prepare_dispwin(True)
        if isinstance(cmd, Exception):
            show_result_dialog(cmd, self)
        elif cmd:
            result = self.worker.exec_cmd(
                cmd, args, capture_output=True, skip_scripts=True
            )
            if isinstance(result, Exception):
                show_result_dialog(result, self)
            elif not result:
                show_result_dialog(UnloggedError("".join(self.worker.errors)), self)
            else:
                self.load_lut(get_display_profile())

    def LoadProfile(self, profile):
        if profile and not isinstance(profile, ICCProfile):
            try:
                profile = ICCProfile(profile)
            except (IOError, ICCProfileInvalidError) as exception:
                show_result_dialog(
                    Error(f"{lang.getstr('profile.invalid')}\n{profile}"), self
                )
                profile = None
        if not profile:
            profile = ICCProfile()
            profile._data = "\0" * 128
            profile._tags.desc = TextDescriptionType(b"", "desc")
            profile.size = len(profile.data)
            profile.is_loaded = True
        center = False
        if (
            getattr(self, "profile", None)
            and self.profile.colorSpace != profile.colorSpace
        ):
            center = True
        if profile.getDescription():
            title = " \u2014 ".join(
                [lang.getstr("calibration.lut_viewer.title"), profile.getDescription()]
            )
        else:
            title = lang.getstr("calibration.lut_viewer.title")
        self.SetTitle(title)
        self.profile = profile
        for channel in "rgb":
            trc = profile.tags.get(f"{channel}TRC", profile.tags.get("kTRC"))
            if isinstance(trc, ParametricCurveType):
                trc = trc.get_trc()
            setattr(self, f"{channel}TRC", trc)
            setattr(self, f"tf_{channel}TRC", trc)
        self.trc = None
        curves = []
        if (
            "vcgt" in profile.tags
            or "MS00" in profile.tags
            or self.__class__ is LUTFrame
        ):
            curves.append(lang.getstr("vcgt"))
        self.client.errors = []
        self.toggle_clut.SetValue("B2A0" in profile.tags or "A2B0" in profile.tags)
        if (self.rTRC and self.gTRC and self.bTRC) or (
            self.toggle_clut.GetValue()
            and profile.colorSpace in (b"RGB", b"GRAY", b"CMYK")
        ):
            try:
                self.lookup_tone_response_curves()
            except Exception as exception:
                wx.CallAfter(show_result_dialog, exception, self)
            else:
                curves.append(lang.getstr("[rgb]TRC"))
        curves = self.add_shaper_curves(curves)
        selection = self.plot_mode_select.GetStringSelection()
        self.plot_mode_select.SetItems(curves)
        self.plot_mode_select.Enable(len(curves) > 1)
        if curves and not self.plot_mode_select.SetStringSelection(selection):
            self.plot_mode_select.SetSelection(0)
            center = True
        self.plot_mode_select.ContainingSizer.Layout()
        self.cbox_sizer.Layout()
        self.box_sizer.Layout()
        self.DrawLUT()
        if center:
            wx.CallAfter(self.client.center)
        wx.CallAfter(self.handle_errors)

    def add_shaper_curves(self, curves):
        if getcfg("show_advanced_options"):
            if isinstance(self.profile.tags.get("A2B0"), LUT16Type):
                curves.append(lang.getstr("profile.tags.A2B0.shaper_curves.input"))
                curves.append(lang.getstr("profile.tags.A2B0.shaper_curves.output"))
            if isinstance(self.profile.tags.get("A2B1"), LUT16Type):
                curves.append(lang.getstr("profile.tags.A2B1.shaper_curves.input"))
                curves.append(lang.getstr("profile.tags.A2B1.shaper_curves.output"))
            if isinstance(self.profile.tags.get("A2B2"), LUT16Type):
                curves.append(lang.getstr("profile.tags.A2B2.shaper_curves.input"))
                curves.append(lang.getstr("profile.tags.A2B2.shaper_curves.output"))
            if isinstance(self.profile.tags.get("B2A0"), LUT16Type):
                curves.append(lang.getstr("profile.tags.B2A0.shaper_curves.input"))
                curves.append(lang.getstr("profile.tags.B2A0.shaper_curves.output"))
            if isinstance(self.profile.tags.get("B2A1"), LUT16Type):
                curves.append(lang.getstr("profile.tags.B2A1.shaper_curves.input"))
                curves.append(lang.getstr("profile.tags.B2A1.shaper_curves.output"))
            if isinstance(self.profile.tags.get("B2A2"), LUT16Type):
                curves.append(lang.getstr("profile.tags.B2A2.shaper_curves.input"))
                curves.append(lang.getstr("profile.tags.B2A2.shaper_curves.output"))
        return curves

    def add_toggles(self, parent, sizer):
        # Add toggle checkboxes for up to 16 channels
        self.toggles = []
        for i in range(16):
            toggle = CustomCheckBox(parent, -1, "", name="toggle_channel_%i" % i)
            toggle.SetForegroundColour(FGCOLOUR)
            toggle.SetMaxFontSize(11)
            toggle.SetValue(True)
            sizer.Add(
                toggle,
                flag=wx.ALIGN_CENTER_VERTICAL | wx.RIGHT,
                border=4 if i < 15 else 0,
            )
            toggle.Bind(wx.EVT_CHECKBOX, self.DrawLUT)
            self.toggles.append(toggle)
            toggle.Hide()

    def add_tone_values(self, legend):
        if not self.profile:
            return
        colorants = legend[0]
        if (
            self.plot_mode_select.GetStringSelection() == lang.getstr("vcgt")
            and "vcgt" in self.profile.tags
        ):
            if "R" in colorants or "G" in colorants or "B" in colorants:
                legend.append(lang.getstr("tone_values"))
                if "=" in colorants and 0:  # NEVER
                    unique = []
                    if 0 in self.client.unique:  # Red
                        unique.append(self.client.unique[0])
                    if 1 in self.client.unique:  # Green
                        unique.append(self.client.unique[1])
                    if 2 in self.client.unique:  # Blue
                        unique.append(self.client.unique[2])
                    unique = min(unique)
                    legend[-1] += " %.1f%% (%i/%i)" % (
                        unique / (self.client.entryCount / 100.0),
                        unique,
                        self.client.entryCount,
                    )
                else:
                    if 0 in self.client.unique:  # Red
                        legend[-1] += " %.1f%% (%i/%i)" % (
                            self.client.unique[0] / (self.client.entryCount / 100.0),
                            self.client.unique[0],
                            self.client.entryCount,
                        )
                    if 1 in self.client.unique:  # Green
                        legend[-1] += " %.1f%% (%i/%i)" % (
                            self.client.unique[1] / (self.client.entryCount / 100.0),
                            self.client.unique[1],
                            self.client.entryCount,
                        )
                    if 2 in self.client.unique:  # Blue
                        legend[-1] += " %.1f%% (%i/%i)" % (
                            self.client.unique[2] / (self.client.entryCount / 100.0),
                            self.client.unique[2],
                            self.client.entryCount,
                        )
                unique = list(self.client.unique.values())
                if 0 not in unique and "R=G=B" not in colorants:
                    unique = min(unique)
                    legend[-1] += ", %s %.1f%% (%i/%i)" % (
                        lang.getstr("grayscale"),
                        unique / (self.client.entryCount / 100.0),
                        unique,
                        self.client.entryCount,
                    )
        elif (
            self.plot_mode_select.GetStringSelection() == lang.getstr("[rgb]TRC")
            and isinstance(self.tf_rTRC, (CurveType, CoordinateType))
            and len(self.tf_rTRC) > 1
            and isinstance(self.tf_gTRC, (CurveType, CoordinateType))
            and len(self.tf_gTRC) > 1
            and isinstance(self.tf_bTRC, (CurveType, CoordinateType))
            and len(self.tf_bTRC) > 1
        ):
            transfer_function = None
            if not getattr(self, "trc", None) and len(self.tf_rTRC) == len(
                self.tf_gTRC
            ) == len(self.tf_bTRC):
                if isinstance(self.tf_rTRC, CurveType):
                    self.trc = CurveType(profile=self.profile)
                    for i in range(len(self.tf_rTRC)):
                        self.trc.append(
                            (self.tf_rTRC[i] + self.tf_gTRC[i] + self.tf_bTRC[i]) / 3.0
                        )
                else:
                    self.trc = CoordinateType(self.profile)
                    for i in range(len(self.tf_rTRC)):
                        self.trc.append(
                            [
                                (
                                    self.tf_rTRC[i][0]
                                    + self.tf_gTRC[i][0]
                                    + self.tf_bTRC[i][0]
                                )
                                / 3.0,
                                (
                                    self.tf_rTRC[i][1]
                                    + self.tf_gTRC[i][1]
                                    + self.tf_bTRC[i][1]
                                )
                                / 3.0,
                            ]
                        )
            if getattr(self, "trc", None):
                transfer_function = self.trc.get_transfer_function(
                    slice=(0.00, 1.00), outoffset=1.0
                )
            # if "R" in colorants and "G" in colorants and "B" in colorants:
            # if self.profile.tags.rTRC == self.profile.tags.gTRC == self.profile.tags.bTRC:
            # transfer_function = self.profile.tags.rTRC.get_transfer_function()
            # elif ("R" in colorants and
            # (not "G" in colorants or
            # self.profile.tags.rTRC == self.profile.tags.gTRC) and
            # (not "B" in colorants or
            # self.profile.tags.rTRC == self.profile.tags.bTRC)):
            # transfer_function = self.profile.tags.rTRC.get_transfer_function()
            # elif ("G" in colorants and
            # (not "R" in colorants or
            # self.profile.tags.gTRC == self.profile.tags.rTRC) and
            # (not "B" in colorants or
            # self.profile.tags.gTRC == self.profile.tags.bTRC)):
            # transfer_function = self.profile.tags.gTRC.get_transfer_function()
            # elif ("B" in colorants and
            # (not "G" in colorants or
            # self.profile.tags.bTRC == self.profile.tags.gTRC) and
            # (not "R" in colorants or
            # self.profile.tags.bTRC == self.profile.tags.rTRC)):
            # transfer_function = self.profile.tags.bTRC.get_transfer_function()
            if transfer_function and transfer_function[1] >= 0.95:
                if self.tf_rTRC == self.tf_gTRC == self.tf_bTRC:
                    label = lang.getstr("rgb.trc")
                else:
                    label = lang.getstr("rgb.trc.averaged")
                if round(transfer_function[1], 2) == 1.0:
                    value = "%s" % (transfer_function[0][0])
                else:
                    value = "≈ %s (Δ %.2f%%)" % (
                        transfer_function[0][0],
                        100 - transfer_function[1] * 100,
                    )
                legend.append(" ".join([label, value]))

    def DrawLUT(self, event=None):
        self.SetStatusText("")
        self.Freeze()
        curves = None
        curves_colorspace = self.profile.colorSpace
        connection_colorspace = b"RGB"
        if self.profile and self.plot_mode_select.Items:
            if self.plot_mode_select.GetStringSelection() == lang.getstr("vcgt"):
                # Convert calibration information from embedded WCS profile
                # (if present) to VideCardFormulaType if the latter is not present
                if (
                    isinstance(self.profile.tags.get("MS00"), WcsProfilesTagType)
                    and "vcgt" not in self.profile.tags
                ):
                    vcgt = self.profile.tags["MS00"].get_vcgt()
                    if vcgt:
                        self.profile.tags["vcgt"] = vcgt
                if "vcgt" in self.profile.tags:
                    curves = self.profile.tags["vcgt"]
                else:
                    curves = None
            elif self.plot_mode_select.GetStringSelection() == lang.getstr("[rgb]TRC"):
                if not (
                    isinstance(self.rTRC, (CurveType, CoordinateType))
                    and isinstance(self.gTRC, (CurveType, CoordinateType))
                    and isinstance(self.bTRC, (CurveType, CoordinateType))
                ):
                    curves = None
                elif (
                    len(self.rTRC) == 1 and len(self.gTRC) == 1 and len(self.bTRC) == 1
                ):
                    # gamma
                    curves = {
                        "redMin": 0.0,
                        "redGamma": self.rTRC[0],
                        "redMax": 1.0,
                        "greenMin": 0.0,
                        "greenGamma": self.gTRC[0],
                        "greenMax": 1.0,
                        "blueMin": 0.0,
                        "blueGamma": self.bTRC[0],
                        "blueMax": 1.0,
                    }
                else:
                    # curves
                    data = [self.rTRC, self.gTRC, self.bTRC]
                    if hasattr(self, "kTRC"):
                        data.append(self.kTRC)
                    curves = {
                        "data": data,
                        "entryCount": len(self.rTRC),
                        "entrySize": 2,
                    }
            elif self.plot_mode_select.GetStringSelection() != lang.getstr("gamut"):
                to_pcs = False
                if self.plot_mode_select.GetStringSelection() == lang.getstr(
                    "profile.tags.A2B0.shaper_curves.input"
                ):
                    tables = self.profile.tags.A2B0.input
                elif self.plot_mode_select.GetStringSelection() == lang.getstr(
                    "profile.tags.A2B0.shaper_curves.output"
                ):
                    tables = self.profile.tags.A2B0.output
                    curves_colorspace = self.profile.connectionColorSpace
                    to_pcs = True
                elif self.plot_mode_select.GetStringSelection() == lang.getstr(
                    "profile.tags.A2B1.shaper_curves.input"
                ):
                    tables = self.profile.tags.A2B1.input
                elif self.plot_mode_select.GetStringSelection() == lang.getstr(
                    "profile.tags.A2B1.shaper_curves.output"
                ):
                    tables = self.profile.tags.A2B1.output
                    curves_colorspace = self.profile.connectionColorSpace
                    to_pcs = True
                elif self.plot_mode_select.GetStringSelection() == lang.getstr(
                    "profile.tags.A2B2.shaper_curves.input"
                ):
                    tables = self.profile.tags.A2B2.input
                elif self.plot_mode_select.GetStringSelection() == lang.getstr(
                    "profile.tags.A2B2.shaper_curves.output"
                ):
                    tables = self.profile.tags.A2B2.output
                    curves_colorspace = self.profile.connectionColorSpace
                    to_pcs = True
                elif self.plot_mode_select.GetStringSelection() == lang.getstr(
                    "profile.tags.B2A0.shaper_curves.input"
                ):
                    tables = self.profile.tags.B2A0.input
                    curves_colorspace = self.profile.connectionColorSpace
                elif self.plot_mode_select.GetStringSelection() == lang.getstr(
                    "profile.tags.B2A0.shaper_curves.output"
                ):
                    tables = self.profile.tags.B2A0.output
                elif self.plot_mode_select.GetStringSelection() == lang.getstr(
                    "profile.tags.B2A1.shaper_curves.input"
                ):
                    tables = self.profile.tags.B2A1.input
                    curves_colorspace = self.profile.connectionColorSpace
                elif self.plot_mode_select.GetStringSelection() == lang.getstr(
                    "profile.tags.B2A1.shaper_curves.output"
                ):
                    tables = self.profile.tags.B2A1.output
                elif self.plot_mode_select.GetStringSelection() == lang.getstr(
                    "profile.tags.B2A2.shaper_curves.input"
                ):
                    tables = self.profile.tags.B2A2.input
                    curves_colorspace = self.profile.connectionColorSpace
                elif self.plot_mode_select.GetStringSelection() == lang.getstr(
                    "profile.tags.B2A2.shaper_curves.output"
                ):
                    tables = self.profile.tags.B2A2.output
                entry_count = len(tables[0])
                if curves_colorspace != b"RGB":
                    maxv = 100
                else:
                    maxv = 255
                lin = [v / (entry_count - 1.0) * maxv for v in range(entry_count)]
                data = []
                for i, table in enumerate(tables):
                    xp = lin
                    if curves_colorspace == b"Lab" and i == 0:
                        if to_pcs:
                            table = [v / 65280.0 * 65535.0 for v in table]
                        else:
                            xp = [
                                min(
                                    v / (entry_count - 1.0) * (100 + 25500 / 65280.0),
                                    maxv,
                                )
                                for v in range(entry_count)
                            ]
                    yp = [v / 65535.0 * maxv for v in table]
                    if curves_colorspace == b"Lab" and i == 0:
                        # Interpolate to given size and use the same y axis
                        # for all channels
                        xi = numpy.interp(lin, yp, xp)
                        yi = numpy.interp(lin, xi, lin)
                    else:
                        yi = yp
                    xy = []
                    for Y, v in zip(yi, lin):
                        if Y <= yp[0]:
                            Y = yp[0]
                        xy.append([v, Y])
                    data.append(xy)
                curves = {"data": data, "entryCount": entry_count, "entrySize": 2}
        yLabel = []
        numchannels = {
            b"XYZ": 3,
            b"Lab": 3,
            b"Luv": 3,
            b"YCbr": 3,
            b"Yxy": 3,
            b"RGB": 3,
            b"GRAY": 1,
            b"HSV": 3,
            b"HLS": 3,
            b"CMYK": 4,
            b"CMY": 3,
            b"2CLR": 2,
            b"3CLR": 3,
            b"4CLR": 4,
            b"5CLR": 5,
            b"6CLR": 6,
            b"7CLR": 7,
            b"8CLR": 8,
            b"9CLR": 9,
            b"ACLR": 10,
            b"BCLR": 11,
            b"CCLR": 12,
            b"DCLR": 13,
            b"ECLR": 14,
            b"FCLR": 15,
        }.get(curves_colorspace, 3)
        is_ktrc = (
            self.plot_mode_select.GetStringSelection() == lang.getstr("[rgb]TRC")
            and curves_colorspace != b"RGB"
            and False
        )
        if is_ktrc:
            curves_colorspace = b"GRAY"
            numchannels = 1
        for channel, toggle in enumerate(self.toggles):
            toggle.Show(channel < numchannels)
            if channel < numchannels:
                if curves_colorspace == b"GRAY":
                    toggle.Label = "R=G=B"
                elif curves_colorspace == b"YCbr":
                    toggle.Label = ("Y", "Cb", "Cr")[channel]
                elif curves_colorspace.endswith(b"CLR"):
                    curves_colorspace = b"nCLR"
                    toggle.Label = "%i" % channel
                elif curves_colorspace == b"Lab":
                    toggle.Label = ("L*", "a*", "b*")[channel]
                else:
                    toggle.Label = curves_colorspace[channel : channel + 1].decode(
                        "utf-8"
                    )
                if toggle.GetValue():
                    yLabel.append(toggle.Label)
            toggle.Enable(bool(curves))
        if self.plot_mode_select.GetStringSelection() not in (
            lang.getstr("[rgb]TRC"),
            lang.getstr("gamut"),
        ) or (self.profile and self.profile.connectionColorSpace == b"RGB"):
            self.xLabel = "".join(yLabel)
        else:
            if self.show_as_L.GetValue():
                connection_colorspace = b"Lab"
                self.xLabel = "L*"
            else:
                connection_colorspace = b"XYZ"
                self.xLabel = "Y"
        self.yLabel = "".join(yLabel)

        self.show_as_L.Enable(bool(curves))
        self.show_as_L.Show(
            self.plot_mode_select.GetStringSelection() == lang.getstr("[rgb]TRC")
            and self.profile.connectionColorSpace != b"RGB"
        )
        self.toggle_clut.Show(
            self.profile.colorSpace == b"RGB"
            and self.profile.connectionColorSpace != b"RGB"
            and self.plot_mode_select.GetStringSelection() == lang.getstr("[rgb]TRC")
            and ("B2A0" in self.profile.tags or b"A2B0" in self.profile.tags)
        )
        self.toggle_clut.Enable(
            self.profile.connectionColorSpace != b"RGB"
            and self.plot_mode_select.GetStringSelection() == lang.getstr("[rgb]TRC")
            and isinstance(self.profile.tags.get("rTRC"), CurveType)
            and isinstance(self.profile.tags.get("gTRC"), CurveType)
            and isinstance(self.profile.tags.get("bTRC"), CurveType)
        )
        self.save_plot_btn.Enable(bool(curves))
        if hasattr(self, "reload_vcgt_btn"):
            self.reload_vcgt_btn.Enable(
                self.plot_mode_select.GetStringSelection() == lang.getstr("vcgt")
                and bool(self.profile)
            )
            self.reload_vcgt_btn.Show(
                self.plot_mode_select.GetStringSelection() == lang.getstr("vcgt")
            )
        if hasattr(self, "apply_bpc_btn"):
            enable_bpc = (
                self.plot_mode_select.GetStringSelection() == lang.getstr("vcgt")
                and bool(self.profile)
                and isinstance(self.profile.tags.get("vcgt"), VideoCardGammaType)
            )
            if enable_bpc:
                values = self.profile.tags.vcgt.getNormalizedValues()
            self.apply_bpc_btn.Enable(enable_bpc and values[0] != (0, 0, 0))
            self.apply_bpc_btn.Show(
                self.plot_mode_select.GetStringSelection() == lang.getstr("vcgt")
            )
        if hasattr(self, "install_vcgt_btn"):
            self.install_vcgt_btn.Enable(
                self.plot_mode_select.GetStringSelection() == lang.getstr("vcgt")
                and bool(self.profile)
                and isinstance(self.profile.tags.get("vcgt"), VideoCardGammaType)
            )
            self.install_vcgt_btn.Show(
                self.plot_mode_select.GetStringSelection() == lang.getstr("vcgt")
            )
        if hasattr(self, "save_vcgt_btn"):
            self.save_vcgt_btn.Enable(
                self.plot_mode_select.GetStringSelection() == lang.getstr("vcgt")
                and bool(self.profile)
                and isinstance(self.profile.tags.get("vcgt"), VideoCardGammaType)
            )
            self.save_vcgt_btn.Show(
                self.plot_mode_select.GetStringSelection() == lang.getstr("vcgt")
            )
        if hasattr(self, "show_actual_lut_cb"):
            self.show_actual_lut_cb.Show(
                self.plot_mode_select.GetStringSelection() == lang.getstr("vcgt")
            )
        if hasattr(self, "rendering_intent_select"):
            self.rendering_intent_select.Show(
                self.plot_mode_select.GetStringSelection() == lang.getstr("[rgb]TRC")
                and self.profile.connectionColorSpace != b"RGB"
            )
        if hasattr(self, "direction_select"):
            self.direction_select.Show(
                self.rendering_intent_select.IsShown()
                and "B2A0" in self.profile.tags
                and "A2B0" in self.profile.tags
            )
        self.show_as_L.GetContainingSizer().Layout()
        if hasattr(self, "cbox_sizer"):
            self.cbox_sizer.Layout()
        if hasattr(self, "box_sizer"):
            self.box_sizer.Layout()
        if self.client.last_PointLabel is not None:
            self.client._drawPointLabel(self.client.last_PointLabel)  # erase old
            self.client.last_PointLabel = None
        channels = dict()
        for channel, toggle in enumerate(self.toggles):
            channels[channel] = (
                toggle.IsShown() and toggle.GetValue() and toggle.Label or ""
            )
        wx.CallAfter(
            self.client.DrawLUT,
            curves,
            xLabel=self.xLabel,
            yLabel=self.yLabel,
            channels=channels,
            colorspace=curves_colorspace,
            connection_colorspace=connection_colorspace,
        )
        self.Thaw()

    def OnClose(self, event):
        self.listening = False
        if self.worker.tempdir and os.path.isdir(self.worker.tempdir):
            self.worker.wrapup(False)
        config.writecfg(
            module="curve-viewer",
            options=("display.number", "position.lut_viewer", "size.lut_viewer"),
        )
        # Hide first (looks nicer)
        self.Hide()
        # Need to use CallAfter to prevent hang under Windows if minimized
        wx.CallAfter(self.Destroy)

    def OnMotion(self, event):
        if isinstance(event, wx.MouseEvent):
            if not event.LeftIsDown():
                self.UpdatePointLabel(self.client._getXY(event))
            else:
                self.client.erase_pointlabel()
            event.Skip()  # Go to next handler

    def OnMove(self, event=None):
        if self.IsShownOnScreen() and not self.IsMaximized() and not self.IsIconized():
            x, y = self.GetScreenPosition()
            setcfg("position.lut_viewer.x", x)
            setcfg("position.lut_viewer.y", y)
        if event:
            event.Skip()

    def OnSize(self, event=None):
        if self.IsShownOnScreen() and not self.IsMaximized() and not self.IsIconized():
            w, h = self.ClientSize
            setcfg("size.lut_viewer.w", w)
            setcfg("size.lut_viewer.h", h)
        if event:
            event.Skip()
            if sys.platform == "win32":
                # Needed under Windows when using double buffering
                self.Refresh()

    def OnWheel(self, event):
        xy = wx.GetMousePosition()
        if self.client.last_draw:
            if event.WheelRotation < 0:
                direction = 1.0
            else:
                direction = -1.0
            self.client.zoom(direction)

    def SaveFile(self, event=None):
        """Saves the file to the type specified in the extension. If no file
        name is specified a dialog box is provided.  Returns True if sucessful,
        otherwise False.

        .bmp  Save a Windows bitmap file.
        .xbm  Save an X bitmap file.
        .xpm  Save an XPM bitmap file.
        .png  Save a Portable Network Graphics file.
        .jpg  Save a Joint Photographic Experts Group file.
        """
        fileName = " ".join(
            [
                self.plot_mode_select.GetStringSelection(),
                os.path.basename(
                    os.path.splitext(self.profile.fileName or lang.getstr("unnamed"))[0]
                ),
            ]
        )
        if (
            event
            and hasattr(self, "save_vcgt_btn")
            and event.GetId() == self.save_vcgt_btn.GetId()
        ):
            extensions = {"cal": 1}
            defType = "cal"
        else:
            extensions = {
                "bmp": wx.BITMAP_TYPE_BMP,  # Save a Windows bitmap file.
                "xbm": wx.BITMAP_TYPE_XBM,  # Save an X bitmap file.
                "xpm": wx.BITMAP_TYPE_XPM,  # Save an XPM bitmap file.
                "jpg": wx.BITMAP_TYPE_JPEG,  # Save a JPG file.
                "png": wx.BITMAP_TYPE_PNG,  # Save a PNG file.
            }
            defType = "png"

        fileName = f"{fileName}.{defType}"

        fType = None
        dlg1 = None
        while fType not in extensions:
            if dlg1:  # FileDialog exists: Check for extension
                InfoDialog(
                    self,
                    msg=lang.getstr("error.file_type_unsupported"),
                    ok=lang.getstr("ok"),
                    bitmap=geticon(32, "dialog-error"),
                )
            else:  # FileDialog doesn't exist: just check one
                dlg1 = wx.FileDialog(
                    self,
                    lang.getstr("save_as"),
                    get_verified_path("last_filedialog_path")[0],
                    fileName,
                    "|".join(
                        [
                            "%s (*.%s)|*.%s" % (ext.upper(), ext, ext)
                            for ext in sorted(extensions.keys())
                        ]
                    ),
                    wx.SAVE | wx.OVERWRITE_PROMPT,
                )
                dlg1.SetFilterIndex(sorted(extensions.keys()).index(defType))

            if dlg1.ShowModal() == wx.ID_OK:
                fileName = dlg1.GetPath()
                if not waccess(fileName, os.W_OK):
                    show_result_dialog(
                        Error(lang.getstr("error.access_denied.write", fileName)), self
                    )
                    return
                fType = fileName[-3:].lower()
                setcfg("last_filedialog_path", fileName)
            else:  # exit without saving
                dlg1.Destroy()
                return False

        if dlg1:
            dlg1.Destroy()

        # Save Bitmap
        if (
            event
            and hasattr(self, "save_vcgt_btn")
            and event.GetId() == self.save_vcgt_btn.GetId()
        ):
            res = vcgt_to_cal(self.profile)
            res.write(fileName)
        else:
            res = self.client._Buffer.SaveFile(fileName, extensions.get(fType, ".png"))
        return res

    def SetStatusText(self, text):
        self.status.Label = text
        self.status.Refresh()

    def SetGamutStatusText(self, text):
        self.gamut_status.Label = text
        self.status.Refresh()

    def UpdatePointLabel(self, xy):
        if self.client.GetEnablePointLabel():
            # Show closest point (when enbled)
            # Make up dict with info for the point label
            dlst = self.client.GetClosestPoint(xy, pointScaled=True)
            if dlst != [] and hasattr(self.client, "point_grid"):
                curveNum, legend, pIndex, pointXY, scaledXY, distance = dlst
                legend = legend.split(", ")
                channels = {}
                value = []
                for channel in self.client.point_grid:
                    toggle = self.toggles[channel]
                    channels[channel] = toggle.Label or ""
                    x = int(round(pointXY[0] / 255.0 * 4095))
                    v = self.client.point_grid[channel].get(x, 0)
                    if toggle.Label in ("a*", "b*"):
                        v = -128 + v / 100.0 * (255 + 255 / 256.0)
                    value.append((toggle.Label, v))
                identical = len(value) > 1 and all(v[1] == value[0][1] for v in value)
                if 1:
                    joiner = " \u2192 "
                    label = [_f for _f in list(channels.values()) if _f]
                    if (
                        self.plot_mode_select.GetStringSelection()
                        == lang.getstr("[rgb]TRC")
                        and self.profile.connectionColorSpace != b"RGB"
                    ):
                        if self.show_as_L.GetValue():
                            format_ = "L* %.2f", "%s %.2f"
                        else:
                            format_ = "Y %.4f", "%s %.2f"
                        axis_y = 100.0
                    elif "L*" in label and ("a*" in label or "b*" in label):
                        a = b = -128 + pointXY[0] / 100.0 * (255 + 255 / 256.0)
                        if label == ["L*", "a*", "b*"]:
                            format_ = "L* %%.2f a* %.2f b* %.2f" % (a, b), "%s %.2f"
                        elif label == ["L*", "a*"]:
                            format_ = "L* %%.2f a* %.2f" % a, "%s %.2f"
                        elif label == ["L*", "b*"]:
                            format_ = "L* %%.2f b* %.2f" % b, "%s %.2f"
                        axis_y = self.client.axis_y[1]
                    else:
                        format_ = "%s %%.2f" % "=".join(label), "%s %.2f"
                        axis_y = self.client.axis_y[1]
                    if identical:
                        # if value[0][1] is None:
                        vout = pointXY[1]
                        if "L*" not in label and ("a*" in label or "b*" in label):
                            vout = -128 + vout / 100.0 * (255 + 255 / 256.0)
                        RGB = " ".join(["=".join(label), "%.2f" % vout])
                    # else:
                    # RGB = "R=G=B %.2f" % value[0][1]
                    else:
                        RGB = " ".join(
                            [
                                format_[1] % (v, s)
                                for v, s in [v for v in value if v[1] is not None]
                            ]
                        )
                    vin = pointXY[0]
                    if "L*" not in label and ("a*" in label or "b*" in label):
                        vin = -128 + pointXY[0] / 100.0 * (255 + 255 / 256.0)
                    legend[0] = joiner.join([format_[0] % vin, RGB])
                    if (
                        self.plot_mode_select.GetStringSelection()
                        == lang.getstr("[rgb]TRC")
                        and self.profile.connectionColorSpace != b"RGB"
                    ):
                        pointXY = pointXY[1], pointXY[0]
                else:
                    joiner = " \u2192 "
                    format_ = "%.2f", "%.2f"
                    axis_y = self.client.axis_y[1]
                    legend[0] = "{} {}".format(
                        legend[0],
                        joiner.join(
                            [format_[i] % point for i, point in enumerate(pointXY)]
                        ),
                    )
                if len(legend) == 1:
                    # Calculate gamma
                    axis_x = self.client.axis_y[1]
                    gamma = []
                    for label, v in value:
                        if v is None or label in ("a*", "b*"):
                            continue
                        if (
                            self.plot_mode_select.GetStringSelection()
                            == lang.getstr("[rgb]TRC")
                            and self.profile.connectionColorSpace != b"RGB"
                        ):
                            x = v
                            y = pointXY[1]
                            if self.show_as_L.GetValue():
                                y = colormath.Lab2XYZ(y, 0, 0)[1] * 100
                        else:
                            x = pointXY[0]
                            y = v
                        if x <= 0 or x >= axis_x or y <= 0 or y >= axis_y:
                            continue
                        if identical:
                            label = "=".join(
                                [
                                    "%s" % s
                                    for s, v in [
                                        s_v for s_v in value if s_v[1] is not None
                                    ]
                                ]
                            )
                        # Note: We need to make sure each point is a float because it
                        # might be a decimal.Decimal, which can't be divided by floats!
                        gamma.append(
                            "{} {:.2f}".format(
                                label,
                                round(
                                    math.log(float(y) / axis_y) / math.log(x / axis_x),
                                    2,
                                ),
                            )
                        )
                        if "=" in label:
                            break
                    if gamma:
                        legend.append("Gamma {}".format(" ".join(gamma)))
                if self.profile.connectionColorSpace != b"RGB":
                    self.add_tone_values(legend)
                legend = [", ".join(legend[:-1])] + [legend[-1]]
                self.SetStatusText("\n".join(legend))
                # Make up dictionary to pass to DrawPointLabel
                mDataDict = {
                    "curveNum": curveNum,
                    "legend": legend,
                    "pIndex": pIndex,
                    "pointXY": pointXY,
                    "scaledXY": scaledXY,
                }
                # Pass dict to update the point label
                self.client.UpdatePointLabel(mDataDict)

    def update_controls(self):
        self.show_actual_lut_cb.Enable(
            (
                self.worker.argyll_version[0:3] > [1, 1, 0]
                or (
                    self.worker.argyll_version[0:3] == [1, 1, 0]
                    and "Beta" not in self.worker.argyll_version_string
                )
            )
            and not config.is_untethered_display()
        )
        self.show_actual_lut_cb.SetValue(
            bool(getcfg("lut_viewer.show_actual_lut"))
            and not config.is_untethered_display()
        )

    @property
    def worker(self):
        return self.client.worker

    def display_changed(self, event):
        self.worker.enumerate_displays_and_ports(
            check_lut_access=False, enumerate_ports=False, include_network_devices=False
        )


def main():
    config.initcfg("curve-viewer")
    # Backup display config
    cfg_display = getcfg("display.number")
    lang.init()
    lang.update_defaults()
    app = BaseApp(0)
    app.TopWindow = LUTFrame(None, -1)
    app.TopWindow.Bind(wx.EVT_CLOSE, app.TopWindow.OnClose, app.TopWindow)
    if sys.platform == "darwin":
        app.TopWindow.init_menubar()
    wx.CallLater(1, _main, app)
    app.MainLoop()


def _main(app):
    app.TopWindow.listen()
    app.TopWindow.display_changed(None)
    app.TopWindow.Bind(wx.EVT_DISPLAY_CHANGED, app.TopWindow.display_changed)
    app.TopWindow.display_no, geometry, client_area = app.TopWindow.get_display()
    app.TopWindow.Bind(wx.EVT_MOVE, app.TopWindow.move_handler, app.TopWindow)
    display_no = get_argyll_display_number(geometry)
    if display_no is not None:
        setcfg("display.number", display_no + 1)
    app.TopWindow.update_controls()
    for arg in sys.argv[1:]:
        if os.path.isfile(arg):
            app.TopWindow.drop_handler(os.path.abspath(arg))
            break
    else:
        app.TopWindow.load_lut(get_display_profile(display_no))
    app.TopWindow.Show()


if __name__ == "__main__":
    main()