File size: 35,761 Bytes
5ee8620
18720f0
 
 
5ee8620
 
18720f0
5ee8620
 
 
18720f0
 
 
 
 
 
 
 
 
 
 
5ee8620
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18720f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ee8620
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18720f0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5ee8620
18720f0
5ee8620
 
 
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
import colorsys
import os
import json
from tempfile import NamedTemporaryFile

import gradio as gr
import pandas as pd

import MiracleCacheTools

parameters_list_path = "data/cdiParametersList.json"
parameters_groups_data_path = "data/cdiParametersGroupsData.json"

EXP_DATA_TEMP_LIST_LENGTH = 16

parameter_preset_files = []
for root, dirs, files in os.walk("data/parameter_presets"):
    for file in files:
        if file.endswith(".json"):
            parameter_preset_files.append(os.path.join(root, file))

def numInputCheck(num, min, max):
    if num < min:
        num = min
    if num > max:
        num = max
    return num

def RGBInputProcess(r, g, b):
    r = round(numInputCheck(r, 0, 255))
    g = round(numInputCheck(g, 0, 255))
    b = round(numInputCheck(b, 0, 255))

    # RGB to HEX
    color_hex = f"#{r:02X}{g:02X}{b:02X}"

    # RGB to HLS
    hls = colorsys.rgb_to_hls(r/255.0, g/255.0, b/255.0)
    hls_h = round(hls[0] * 360)
    hls_l = round(hls[1] * 100)
    hls_s = round(hls[2] * 100)

    # RGB to HSV
    hsv = colorsys.rgb_to_hsv(r/255.0, g/255.0, b/255.0)
    hsv_h = round(hsv[0] * 360)
    hsv_s = round(hsv[1] * 100)
    hsv_v = round(hsv[2] * 100)

    return color_hex, color_hex, hls_h, hls_l, hls_s, hsv_h, hsv_s, hsv_v

def HLSInputProcess(h, l, s):
    h = round(numInputCheck(h, 0, 360))
    l = round(numInputCheck(l, 0, 100))
    s = round(numInputCheck(s, 0, 100))

    # HLS to RGB
    rgb = colorsys.hls_to_rgb(h/360.0, l/100.0, s/100.0)
    rgb_r = round(rgb[0] * 255)
    rgb_g = round(rgb[1] * 255)
    rgb_b = round(rgb[2] * 255)

    # RGB to HEX
    color_hex = f"#{rgb_r:02X}{rgb_g:02X}{rgb_b:02X}"

    # RGB to HSV
    hsv = colorsys.rgb_to_hsv(rgb_r/255.0, rgb_g/255.0, rgb_b/255.0)
    hsv_h = round(hsv[0] * 360)
    hsv_s = round(hsv[1] * 100)
    hsv_v = round(hsv[2] * 100)

    return color_hex, color_hex, rgb_r, rgb_g, rgb_b, hsv_h, hsv_s, hsv_v

def HSVInputProcess(h, s, v):
    h = round(numInputCheck(h, 0, 360))
    s = round(numInputCheck(s, 0, 100))
    v = round(numInputCheck(v, 0, 100))

    # HSV to RGB
    rgb = colorsys.hsv_to_rgb(h/360.0, s/100.0, v/100.0)
    rgb_r = round(rgb[0] * 255)
    rgb_g = round(rgb[1] * 255)
    rgb_b = round(rgb[2] * 255)

    # RGB to HEX
    color_hex = f"#{rgb_r:02X}{rgb_g:02X}{rgb_b:02X}"

    # RGB to HLS
    hls = colorsys.rgb_to_hls(rgb_r/255.0, rgb_g/255.0, rgb_b/255.0)
    hls_h = round(hls[0] * 360)
    hls_l = round(hls[1] * 100)
    hls_s = round(hls[2] * 100)

    return color_hex, color_hex, rgb_r, rgb_g, rgb_b, hls_h, hls_l, hls_s

def colorInputProcess(color_str):
    #print(f"color_str: {color_str}")
    r, g, b = MiracleCacheTools.colorStringProcessRGB(color_str)

    # RGB to HEX
    color_hex = f"#{r:02X}{g:02X}{b:02X}"

    # RGB to HLS
    hls = colorsys.rgb_to_hls(r/255.0, g/255.0, b/255.0)
    hls_h = round(hls[0] * 360)
    hls_l = round(hls[1] * 100)
    hls_s = round(hls[2] * 100)

    # RGB to HSV
    hsv = colorsys.rgb_to_hsv(r/255.0, g/255.0, b/255.0)
    hsv_h = round(hsv[0] * 360)
    hsv_s = round(hsv[1] * 100)
    hsv_v = round(hsv[2] * 100)

    return color_hex, r, g, b, hls_h, hls_l, hls_s, hsv_h, hsv_s, hsv_v

def parameterPresetFileSelectUpdate(parameter_preset_file_select_type):
    if parameter_preset_file_select_type == "选择已有文件":
        return gr.Dropdown(value=None, interactive=True), gr.File(value=None, interactive=False)
    elif parameter_preset_file_select_type == "上传本地文件":
        return gr.Dropdown(value=None, interactive=False), gr.File(value=None, interactive=True)
    else:
        return gr.Dropdown(value=None, interactive=False), gr.File(value=None, interactive=False)

def parameterPresetFileProcess(parameter_preset_file):
    with open(parameter_preset_file, "r", encoding="utf8") as file:
        data = json.load(file)

    parameter_preset_list = list(data)

    return gr.Dropdown(choices=parameter_preset_list), data

def processButton0201Click(parameter_preset, expression_acceptor_file, expression_donor_file, parameter_preset_data):
    if parameter_preset is None:
        raise ValueError("parameter preset not found")
    if expression_acceptor_file is None:
        raise ValueError("expression acceptor not found")
    if expression_donor_file is None:
        raise ValueError("expression donor not found")
    
    with open(expression_acceptor_file, "r", encoding="utf8") as file:
        data_acceptor = json.load(file)

    with open(expression_donor_file, "r", encoding="utf8") as file:
        data_donor = json.load(file)

    parameters_list = list(parameter_preset_data[parameter_preset])

    data_result = MiracleCacheTools.expressionParametersMigration(parameters_list, data_acceptor, data_donor)

    with NamedTemporaryFile(suffix=".exp3.json", delete=False, mode="w", encoding="utf8") as file:
        json.dump(data_result, file, ensure_ascii=False, indent=4)
        result_file_path = file.name

    return result_file_path

def useButton0202Click(select_type, file_select, file_upload):
    if select_type == "选择已有文件":
        if file_select is None:
            raise ValueError("parameter preset file not found")
        file = file_select

    elif select_type == "上传本地文件":
        if file_upload is None:
            raise ValueError("parameter preset file not found")
        file = file_upload

    else:
        raise ValueError(f"unknown select type : {select_type}")
    
    with open(file, "r", encoding="utf8") as f:
        data = json.load(f)

    return data

def useButton0202aClick(file_upload):
    if file_upload is None:
        raise ValueError("parameters list update file not found")
    
    with open(file_upload, "r", encoding="utf8") as file:
        data_raw = json.load(file)

    data_result = MiracleCacheTools.cdiParametersListGet(data_raw)
    data_result_df = MiracleCacheTools.parametersListTextGet(data_result)

    return data_result, data_result_df

def addButton0202Click(data_raw, name, params, parameters_list_df):
    if name is None:
        raise ValueError("parameter preset name is empty")
    if params is None:
        params = []
    
    name = name.strip()
    if name == "":
        raise ValueError("parameter preset name is empty")
    if name in data_raw:
        raise ValueError(f"Duplicate parameter preset name found: {name}")
    
    pdata = {}
    for param in params:
        pid = parameters_list_df[parameters_list_df["text"]==param].reset_index(drop=True)["id"][0]
        pname = parameters_list_df[parameters_list_df["text"]==param].reset_index(drop=True)["name"][0]
        pdata[pid] = {"Name": pname}

    data_raw[name] = pdata.copy()

    return data_raw, "", []

def deleteButton0202Click(data_raw, delete_list):
    if delete_list is None:
        delete_list = []

    for name in delete_list:
        if name in data_raw:
            del data_raw[name]

    return data_raw, []

def saveButton0202Click(data_raw):
    with NamedTemporaryFile(suffix=".json", delete=False, mode="w", encoding="utf8") as file:
        json.dump(data_raw, file, ensure_ascii=False)
        result_file_path = file.name

    return result_file_path

def updateParametersGroupsDataByCdiFile(file_upload):
    if file_upload is None:
        raise ValueError("parameters groups data update file not found")
    
    with open(file_upload, "r", encoding="utf8") as file:
        data_raw = json.load(file)

    data_result = MiracleCacheTools.cdiParametersGroupsDataGet(data_raw)
    data_result_df = MiracleCacheTools.parametersListTextGet(data_result)

    return data_result, data_result_df

def processButton0203bClick(parameters_groups, expression_acceptor_file, expression_donor_file, parameters_groups_data, parameters_groups_dataframe):
    if parameters_groups is None:
        parameters_groups = []
    if expression_acceptor_file is None:
        raise ValueError("expression acceptor not found")
    if expression_donor_file is None:
        raise ValueError("expression donor not found")
    
    with open(expression_acceptor_file, "r", encoding="utf8") as file:
        data_acceptor = json.load(file)

    with open(expression_donor_file, "r", encoding="utf8") as file:
        data_donor = json.load(file)

    groups = {}
    for gtext in parameters_groups:
        gid = parameters_groups_dataframe[parameters_groups_dataframe["text"]==gtext].reset_index(drop=True)["id"][0]
        groups[gid] = parameters_groups_data[gid]

    data_result = MiracleCacheTools.expressionParametersMigrationByGroups(groups, data_acceptor, data_donor)
        
    with NamedTemporaryFile(suffix=".exp3.json", delete=False, mode="w", encoding="utf8") as file:
        json.dump(data_result, file, ensure_ascii=False, indent=4)
        result_file_path = file.name

    return result_file_path

def multipleExpressionsParametersMigration(expression_donor_files, parameters_groups, expression_data_temp_list, parameters_groups_data, parameters_groups_dataframe):
    if parameters_groups is None:
        return expression_data_temp_list
    elif len(parameters_groups) == 0:
        return expression_data_temp_list

    if expression_donor_files is None:
        return expression_data_temp_list
    elif len(expression_donor_files) == 0:
        return expression_data_temp_list

    data_result = expression_data_temp_list[-1]
    data_list_result = expression_data_temp_list.copy()
    groups = {}
    for gtext in parameters_groups:
        gid = parameters_groups_dataframe[parameters_groups_dataframe["text"]==gtext].reset_index(drop=True)["id"][0]
        groups[gid] = parameters_groups_data[gid]
    
    for expression_donor_file in expression_donor_files:
        with open(expression_donor_file, "r", encoding="utf8") as file:
            data_donor = json.load(file)
        data_result = MiracleCacheTools.expressionParametersMigrationByGroups(groups, data_result, data_donor)

    data_list_result.append(data_result.copy())
    if len(data_list_result) > EXP_DATA_TEMP_LIST_LENGTH:
        data_list_result = data_list_result[-EXP_DATA_TEMP_LIST_LENGTH:]

    return data_list_result

def multipleExpressionsParametersOverlay(expression_donor_files, parameters_groups, expression_data_temp_list, parameters_groups_data, parameters_groups_dataframe):
    if parameters_groups is None:
        return expression_data_temp_list
    elif len(parameters_groups) == 0:
        return expression_data_temp_list

    if expression_donor_files is None:
        return expression_data_temp_list
    elif len(expression_donor_files) == 0:
        return expression_data_temp_list

    data_result = expression_data_temp_list[-1]
    data_list_result = expression_data_temp_list.copy()
    groups = {}
    for gtext in parameters_groups:
        gid = parameters_groups_dataframe[parameters_groups_dataframe["text"]==gtext].reset_index(drop=True)["id"][0]
        groups[gid] = parameters_groups_data[gid]
    
    for expression_donor_file in expression_donor_files:
        with open(expression_donor_file, "r", encoding="utf8") as file:
            data_donor = json.load(file)
        data_result = MiracleCacheTools.expressionParametersOverlayByGroups(groups, data_result, data_donor)

    data_list_result.append(data_result.copy())
    if len(data_list_result) > EXP_DATA_TEMP_LIST_LENGTH:
        data_list_result = data_list_result[-EXP_DATA_TEMP_LIST_LENGTH:]

    return data_list_result

def expressionDataTempListAppendEmptyExpressionsDict(expression_data_temp_list):
    expression_data_temp_list.append({"Type": "Live2D Expression", "Parameters": []})
    if len(expression_data_temp_list) > EXP_DATA_TEMP_LIST_LENGTH:
        expression_data_temp_list = expression_data_temp_list[-EXP_DATA_TEMP_LIST_LENGTH:]

    return expression_data_temp_list
    
def saveButton0204bClick(data_raw_list):
    data_raw = data_raw_list[-1]
    with NamedTemporaryFile(suffix=".exp3.json", delete=False, mode="w", encoding="utf8") as file:
        json.dump(data_raw, file, ensure_ascii=False)
        result_file_path = file.name

    return result_file_path

def expressionDataTempListStateChange(expression_data_temp_list):
    expression_data = expression_data_temp_list[-1]
    if len(expression_data_temp_list) <= 1:
        back_interactive = False
    else:
        back_interactive = True

    return gr.JSON(value=expression_data), gr.Button(interactive=back_interactive)



with gr.Blocks(title="MiracleCacheTools") as demo:

    gr.Markdown("## 奇迹缓缓辅助工具箱")

    with gr.Tab("颜色空间换算"):

        comment_01 = gr.Markdown("注意事项:<br>滑块监听事件为其输入值的改变,快速拖动可能会出现数值更新不及时的bug。(程序缓不过来了)<br>调色时建议先通过点击确定大概位置,再缓慢微移滑块。")
        RGB_hex = gr.Textbox(value="#000000", label="十六进制RGB值", interactive=False)

        with gr.Row():
            with gr.Column(scale=1):
                gr.Markdown("**RGB**")
                RGB_r = gr.Slider(0, 255, value=0, step=1, label="R(红色)")
                RGB_g = gr.Slider(0, 255, value=0, step=1, label="G(绿色)")
                RGB_b = gr.Slider(0, 255, value=0, step=1, label="B(蓝色)")

            with gr.Column(scale=1):
                gr.Markdown("**HLS**")
                HLS_h = gr.Slider(0, 360, value=0, step=1, label="H(色相)")
                HLS_l = gr.Slider(0, 100, value=0, step=1, label="L(亮度)")
                HLS_s = gr.Slider(0, 100, value=0, step=1, label="S(饱和度)")

            with gr.Column(scale=1):
                gr.Markdown("**HSV**")
                HSV_h = gr.Slider(0, 360, value=0, step=1, label="H(色相)")
                HSV_s = gr.Slider(0, 100, value=0, step=1, label="S(饱和度)")
                HSV_v = gr.Slider(0, 100, value=0, step=1, label="V(明度)")

        with gr.Row():
            with gr.Column(scale=1):
                color = gr.ColorPicker(value="#000000", label="调色板", scale=1)
                gr.Markdown("右侧可上传参考图片,配合调色板的吸管工具使用。<br>试验性功能,请谨慎使用。")
            image_sample = gr.Image(value=None, label="参考图片", interactive=True, scale=3) 


        RGB_r.input(
            fn=RGBInputProcess,
            inputs=[RGB_r, RGB_g, RGB_b],
            outputs=[color, RGB_hex, HLS_h, HLS_l, HLS_s, HSV_h, HSV_s, HSV_v]
        )

        RGB_g.input(
            fn=RGBInputProcess,
            inputs=[RGB_r, RGB_g, RGB_b],
            outputs=[color, RGB_hex, HLS_h, HLS_l, HLS_s, HSV_h, HSV_s, HSV_v]
        )

        RGB_b.input(
            fn=RGBInputProcess,
            inputs=[RGB_r, RGB_g, RGB_b],
            outputs=[color, RGB_hex, HLS_h, HLS_l, HLS_s, HSV_h, HSV_s, HSV_v]
        )

        HLS_h.input(
            fn=HLSInputProcess,
            inputs=[HLS_h, HLS_l, HLS_s],
            outputs=[color, RGB_hex, RGB_r, RGB_g, RGB_b, HSV_h, HSV_s, HSV_v]
        )

        HLS_l.input(
            fn=HLSInputProcess,
            inputs=[HLS_h, HLS_l, HLS_s],
            outputs=[color, RGB_hex, RGB_r, RGB_g, RGB_b, HSV_h, HSV_s, HSV_v]
        )

        HLS_s.input(
            fn=HLSInputProcess,
            inputs=[HLS_h, HLS_l, HLS_s],
            outputs=[color, RGB_hex, RGB_r, RGB_g, RGB_b, HSV_h, HSV_s, HSV_v]
        )

        HSV_h.input(
            fn=HSVInputProcess,
            inputs=[HSV_h, HSV_s, HSV_v],
            outputs=[color, RGB_hex, RGB_r, RGB_g, RGB_b, HLS_h, HLS_l, HLS_s]
        )

        HSV_s.input(
            fn=HSVInputProcess,
            inputs=[HSV_h, HSV_s, HSV_v],
            outputs=[color, RGB_hex, RGB_r, RGB_g, RGB_b, HLS_h, HLS_l, HLS_s]
        )

        HSV_v.input(
            fn=HSVInputProcess,
            inputs=[HSV_h, HSV_s, HSV_v],
            outputs=[color, RGB_hex, RGB_r, RGB_g, RGB_b, HLS_h, HLS_l, HLS_s]
        )

        color.input(
            fn=colorInputProcess,
            inputs=[color],
            outputs=[RGB_hex, RGB_r, RGB_g, RGB_b, HLS_h, HLS_l, HLS_s, HSV_h, HSV_s, HSV_v]
        )


    with gr.Tab("表情参数批量迁移"):

        with open(parameters_list_path, "r", encoding="utf8") as file:
            parameters_list_dict = json.load(file)
        parameters_list_dict_state = gr.State(parameters_list_dict)
        parameters_list_df_state = gr.State(MiracleCacheTools.parametersListTextGet(parameters_list_dict))

        ## 此处参数分组表格和参数表格使用同一个函数获取
        with open(parameters_groups_data_path, "r", encoding="utf8") as file:
            parameters_groups_data_dict = json.load(file)
        parameters_groups_data_dict_state = gr.State(parameters_groups_data_dict)
        parameters_groups_data_df_state = gr.State(MiracleCacheTools.parametersListTextGet(parameters_groups_data_dict))

        

        gr.Markdown("表情参数批量迁移,试验性功能,请谨慎使用。<br>实际效果为,根据参考的参数列表,将参数施主的表情参数复制到参数受主中,覆盖原有表情参数或新增表情参数。(详见下方的详细说明)<br>参数列表应随奇迹缓缓一齐更新。<br>工具不会处理数据文件可能存在的格式问题,请自行确保数据文件格式正确并备份原始数据。")

        with gr.Tab("使用参数预设"):

            parameter_preset_data_state = gr.State({})

            with gr.Row():
                with gr.Column(scale=1):
                    gr.Markdown("#### 参数预设文件")
                    gr.Markdown("选择或上传参数预设文件并选择参数预设")
                    parameter_preset_file_select_type = gr.Radio(choices=["选择已有文件", "上传本地文件"], value="选择已有文件", label="参数预设文件选择方式", interactive=True)
                    parameter_preset_file_select = gr.Dropdown(choices=parameter_preset_files, value=None, multiselect=False, label="选择参数预设文件", interactive=True)
                    parameter_preset_file_upload = gr.File(file_types=[".json"], label="上传参数预设文件", interactive=False)
                    parameter_preset = gr.Dropdown(choices=[], value=None, multiselect=False, label="选择参数预设", interactive=True)

                with gr.Column(scale=1):
                    expression_acceptor_file = gr.File(file_types=[".json"], label="上传参数受主文件", interactive=True)
                    expression_donor_file = gr.File(file_types=[".json"], label="上传参数施主文件", interactive=True)
                    with gr.Row():
                        process_button_0201 = gr.Button("处理", variant="primary")
                        clear_button_0201 = gr.Button("清空", variant="primary")
                    output_file_0201 = gr.File(label="输出文件", interactive=False)

            gr.Markdown("#### 使用方法:")
            gr.Markdown("1. 选择或上传参数预设文件,选择参数预设。<br>2. 上传参数受主文件和参数施主文件。")
            gr.Markdown("#### 原理说明:")
            gr.Markdown("遍历预设列表中的参数。<br>若其存在于施主文件中,则将其复制到受主文件中,覆盖原有参数或新增参数。<br>若其不存在于施主文件中,则删去受主文件中的该参数(若存在)。")

            parameter_preset_file_select_type.change(
                fn=parameterPresetFileSelectUpdate,
                inputs=[parameter_preset_file_select_type],
                outputs=[parameter_preset_file_select, parameter_preset_file_upload]
            )

            parameter_preset_file_select.input(
                fn=parameterPresetFileProcess,
                inputs=[parameter_preset_file_select],
                outputs=[parameter_preset, parameter_preset_data_state]
            )

            parameter_preset_file_upload.upload(
                fn=parameterPresetFileProcess,
                inputs=[parameter_preset_file_upload],
                outputs=[parameter_preset, parameter_preset_data_state]
            )

            process_button_0201.click(
                fn=processButton0201Click,
                inputs=[parameter_preset, expression_acceptor_file, expression_donor_file, parameter_preset_data_state],
                outputs=[output_file_0201]
            )

            clear_button_0201.click(
                fn=lambda : ["选择已有文件", [], None, [], None, None],
                outputs=[parameter_preset_file_select_type, parameter_preset_file_select, parameter_preset_file_upload, parameter_preset, expression_acceptor_file, expression_donor_file]
            )

        with gr.Tab("自定义参数预设"):

            parameter_preset_0202_state = gr.State({})

            with gr.Row():
                with gr.Column(scale=1):
                    gr.Markdown("#### 参数预设模板文件")
                    gr.Markdown("选择或上传参数预设文件并以其作为参数预设模板")
                    parameter_preset_file_select_type_0202 = gr.Radio(choices=["选择已有文件", "上传本地文件"], value="选择已有文件", label="参数预设文件选择方式", interactive=True)
                    parameter_preset_file_select_0202 = gr.Dropdown(choices=parameter_preset_files, value=None, multiselect=False, label="选择参数预设文件", interactive=True)
                    parameter_preset_file_upload_0202 = gr.File(file_types=[".json"], label="上传参数预设文件", interactive=False)
                    with gr.Row():
                        use_button_0202 = gr.Button("使用参数预设模板", variant="primary")
                        clear_button_0202 = gr.Button("清空", variant="primary")

                    gr.Markdown("#### 参数列表更新")
                    gr.Markdown("默认使用现有参数列表<br>可上传『.cdi3.json』文件以更新参数列表")
                    parameters_list_update_file = gr.File(file_types=[".json"], show_label=False)
                    with gr.Row():
                        use_button_0202a = gr.Button("更新参数列表", variant="primary")

                with gr.Column(scale=1):
                    gr.Markdown("#### 添加参数预设")
                    gr.Markdown("注意,预设名称不可与现有的重复。")
                    parameter_preset_name = gr.Textbox(value="", label="参数预设名称", interactive=True)
                    parameter_preset_parameters = gr.Dropdown(choices=parameters_list_df_state.value["text"].to_list(), value=None, multiselect=True, label="选择预设参数", interactive=True, info="参数过多时,下拉框存在局限,后续将进行优化。")
                    with gr.Row():
                        add_button_0202 = gr.Button("添加参数预设", variant="primary")
                        clear_button_0202b = gr.Button("清空", variant="primary")
                    gr.Markdown("#### 删除参数预设")
                    parameter_preset_delete = gr.Dropdown(choices=list(parameter_preset_0202_state.value), value=None, multiselect=True, label="选择要删除的参数预设", interactive=True)
                    with gr.Row():
                        delete_button_0202 = gr.Button("删除参数预设", variant="primary")
                        clear_button_0202c = gr.Button("清空", variant="primary")

                    gr.Markdown("#### 参数预设预览")
                    parameter_preset_preview = gr.Dataframe(MiracleCacheTools.parameterPresetDataFramePreviewGet(parameter_preset_0202_state.value), headers=["预设名称", "参数列表"], interactive=False, label="参数预设预览")

                    gr.Markdown("#### 参数预设保存")
                    save_button_0202 = gr.Button("保存参数预设文件", variant="primary")
                    output_file_0202 = gr.File(label="输出文件", interactive=False)

            parameter_preset_file_select_type_0202.change(
                fn=parameterPresetFileSelectUpdate,
                inputs=[parameter_preset_file_select_type_0202],
                outputs=[parameter_preset_file_select_0202, parameter_preset_file_upload_0202]
            )

            use_button_0202.click(
                fn=useButton0202Click,
                inputs=[parameter_preset_file_select_type_0202, parameter_preset_file_select_0202, parameter_preset_file_upload_0202],
                outputs=[parameter_preset_0202_state]
            )

            clear_button_0202.click(
                fn=lambda :["选择已有文件", [], None],
                outputs=[parameter_preset_file_select_type_0202, parameter_preset_file_select_0202, parameter_preset_file_upload_0202]
            )

            use_button_0202a.click(
                fn=useButton0202aClick,
                inputs=[parameters_list_update_file],
                outputs=[parameters_list_dict_state, parameters_list_df_state]
            )

            add_button_0202.click(
                fn=addButton0202Click,
                inputs=[parameter_preset_0202_state, parameter_preset_name, parameter_preset_parameters, parameters_list_df_state],
                outputs=[parameter_preset_0202_state, parameter_preset_name, parameter_preset_parameters]
            )

            clear_button_0202b.click(
                fn=lambda : ["", []],
                outputs=[parameter_preset_name, parameter_preset_parameters]
            )

            delete_button_0202.click(
                fn=deleteButton0202Click,
                inputs=[parameter_preset_0202_state, parameter_preset_delete],
                outputs=[parameter_preset_0202_state, parameter_preset_delete]
            )

            clear_button_0202c.click(
                fn=lambda : [[]],
                outputs=[parameter_preset_delete]
            )

            save_button_0202.click(
                fn=saveButton0202Click,
                inputs=[parameter_preset_0202_state],
                outputs=[output_file_0202]
            )

            parameters_list_df_state.change(
                fn=lambda df:gr.Dropdown(choices=df["text"].to_list()),
                inputs=[parameters_list_df_state],
                outputs=[parameter_preset_parameters]
            )

            parameter_preset_0202_state.change(
                fn=lambda preset:[gr.Dataframe(MiracleCacheTools.parameterPresetDataFramePreviewGet(preset)), gr.Dropdown(choices=list(preset))],
                inputs=[parameter_preset_0202_state],
                outputs=[parameter_preset_preview, parameter_preset_delete]
            )

        with gr.Tab("使用原生参数分组"):
            gr.Markdown("使用原生的参数分组作为参考的参数列表。<br>分组可多选。")

            with gr.Row():
                with gr.Column(scale=1):
                    parameters_groups_select_0203 = gr.Dropdown(choices=parameters_groups_data_df_state.value["text"].to_list(), value=None, multiselect=True, interactive=True, label="选择参数分组")
                    with gr.Row():
                        all_button_0203 = gr.Button("全选", variant="primary")
                        #clear_button_0203 = gr.Button("清空", variant="primary")

                    gr.Markdown("#### 参数分组数据更新")
                    gr.Markdown("默认使用现有参数分组数据<br>可上传『.cdi3.json』文件以更新参数分组数据")
                    parameters_groups_data_update_file = gr.File(file_types=[".json"], show_label=False)
                    with gr.Row():
                        use_button_0203a = gr.Button("更新参数分组", variant="primary")

                with gr.Column(scale=1):
                    expression_acceptor_file_0203 = gr.File(file_types=[".json"], label="上传参数受主文件", interactive=True)
                    expression_donor_file_0203 = gr.File(file_types=[".json"], label="上传参数施主文件", interactive=True)
                    with gr.Row():
                        process_button_0203b = gr.Button("处理", variant="primary")
                        clear_button_0203b = gr.Button("清空", variant="primary")
                    output_file_0203 = gr.File(label="输出文件", interactive=False)

            all_button_0203.click(
                fn=lambda df: gr.Dropdown(value=df["text"].to_list()),
                inputs=[parameters_groups_data_df_state],
                outputs=[parameters_groups_select_0203]
            )

            use_button_0203a.click(
                fn=updateParametersGroupsDataByCdiFile,
                inputs=[parameters_groups_data_update_file],
                outputs=[parameters_groups_data_dict_state, parameters_groups_data_df_state]
            )

            process_button_0203b.click(
                fn=processButton0203bClick,
                inputs=[parameters_groups_select_0203, expression_acceptor_file_0203, expression_donor_file_0203, parameters_groups_data_dict_state, parameters_groups_data_df_state],
                outputs=[output_file_0203]
            )

            clear_button_0203b.click(
                fn=lambda:[[], None, None],
                outputs=[parameters_groups_select_0203, expression_acceptor_file_0203, expression_donor_file_0203]
            )


        with gr.Tab("多表情参数迁移叠加"):

            expression_data_temp_list_state = gr.State([{"Type": "Live2D Expression", "Parameters": []}])

            gr.Markdown("此工具不再限定参数来源为一个参数施主文件和一个参数受主文件,现在可以将多个表情里指定的参数迁移到目标中。")
            gr.Markdown("新表情的参数会覆盖原有的同名参数,目前有『迁移』『叠加』两种操作。<br>『迁移』和之前一样会删掉参考列表中原表情有而新表情没有的参数,『叠加』则不会。<br>当参数列表包含所有相关参数时,其『迁移』结果新表情将会完全覆盖原表情,『叠加』结果则类似于VTS的表情叠加。")
            gr.Markdown("右侧可预览暂存的表情数据,点击保存按钮可将其保存为json文件。<br>可以撤回处理操作,目前至多支持16步。")

            with gr.Row():
                with gr.Column(scale=1):
                    gr.Markdown("#### 表情参数迁移")
                    gr.Markdown("上传参数施主文件并选择参数分组。")
                    expression_donor_files = gr.Files(file_types=[".json"], label="上传参数施主文件", interactive=True)
                    parameters_groups_select_0204 = gr.Dropdown(choices=parameters_groups_data_df_state.value["text"].to_list(), value=None, multiselect=True, interactive=True, label="选择参数分组")
                    with gr.Row():
                        all_button_0204a = gr.Button("全选", variant="primary")
                        clear_button_0204a = gr.Button("清空", variant="primary")
                    with gr.Row():
                        migration_button_0204a = gr.Button("迁移", variant="primary")
                        overlay_button_0204a = gr.Button("叠加", variant="primary")

                    gr.Markdown("#### 参数分组数据更新")
                    gr.Markdown("默认使用现有参数分组数据<br>可上传『.cdi3.json』文件以更新参数分组数据")
                    parameters_groups_data_update_file_0204 = gr.File(file_types=[".json"], show_label=False, interactive=True)
                    with gr.Row():
                        use_button_0204 = gr.Button("更新参数分组", variant="primary")

                with gr.Column(scale=1):
                    expression_data_temp_preview_json = gr.JSON(value=expression_data_temp_list_state.value[-1], label="暂存表情数据预览")
                    with gr.Row():
                        back_button_0204b = gr.Button("撤回", variant="primary", interactive=False)
                        clear_button_0204b = gr.Button("清空", variant="primary")
                        save_button_0204b = gr.Button("保存", variant="primary")
                    output_file_0204 = gr.File(label="输出文件", interactive=False)

            all_button_0204a.click(
                fn=lambda df: gr.Dropdown(value=df["text"].to_list()),
                inputs=[parameters_groups_data_df_state],
                outputs=[parameters_groups_select_0204]
            )

            clear_button_0204a.click(
                fn=lambda :[[], []],
                outputs=[expression_donor_files, parameters_groups_select_0204]
            )

            migration_button_0204a.click(
                fn=multipleExpressionsParametersMigration,
                inputs=[expression_donor_files, parameters_groups_select_0204, expression_data_temp_list_state, parameters_groups_data_dict_state, parameters_groups_data_df_state],
                outputs=[expression_data_temp_list_state]
            )

            overlay_button_0204a.click(
                fn=multipleExpressionsParametersOverlay,
                inputs=[expression_donor_files, parameters_groups_select_0204, expression_data_temp_list_state, parameters_groups_data_dict_state, parameters_groups_data_df_state],
                outputs=[expression_data_temp_list_state]
            )

            use_button_0204.click(
                fn=updateParametersGroupsDataByCdiFile,
                inputs=[parameters_groups_data_update_file_0204],
                outputs=[parameters_groups_data_dict_state, parameters_groups_data_df_state]
            )

            back_button_0204b.click(
                fn=lambda lst: lst[:-1],
                inputs=[expression_data_temp_list_state],
                outputs=[expression_data_temp_list_state]
            )

            clear_button_0204b.click(
                fn=expressionDataTempListAppendEmptyExpressionsDict,
                inputs=[expression_data_temp_list_state],
                outputs=[expression_data_temp_list_state]
            )

            save_button_0204b.click(
                fn=saveButton0204bClick,
                inputs=[expression_data_temp_list_state],
                outputs=[output_file_0204]
            )

            expression_data_temp_list_state.change(
                fn=expressionDataTempListStateChange,
                inputs=[expression_data_temp_list_state],
                outputs=[expression_data_temp_preview_json, back_button_0204b]
            )

        ## ???
        parameters_groups_data_df_state.change(
            fn=lambda df: (gr.Dropdown(choices=df["text"].to_list()), gr.Dropdown(choices=df["text"].to_list())),
            inputs=[parameters_groups_data_df_state],
            outputs=[parameters_groups_select_0203, parameters_groups_select_0204]
        )

                    

                    


    demo.launch()