Wanlau commited on
Commit
5ee8620
·
1 Parent(s): eec0da1

color space convert

Browse files
Files changed (4) hide show
  1. .gitignore +2 -0
  2. MiracleCacheTools.py +58 -0
  3. app.py +202 -0
  4. requirements.txt +1 -0
.gitignore ADDED
@@ -0,0 +1,2 @@
 
 
 
1
+ **/__pycache__/
2
+ **/test.py
MiracleCacheTools.py ADDED
@@ -0,0 +1,58 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import re
2
+ import colorsys
3
+
4
+
5
+ def colorStringProcessRGB(color_str):
6
+ color_str = color_str.strip()
7
+ if color_str.startswith("#"):
8
+ color_str = color_str[1:]
9
+ if len(color_str) == 3:
10
+ r = int(color_str[0]*2, 16)
11
+ g = int(color_str[1]*2, 16)
12
+ b = int(color_str[2]*2, 16)
13
+ return r, g, b
14
+ elif len(color_str) == 6:
15
+ r = int(color_str[0:2], 16)
16
+ g = int(color_str[2:4], 16)
17
+ b = int(color_str[4:6], 16)
18
+ return r, g, b
19
+ else:
20
+ raise ValueError("Invalid HEX color format")
21
+
22
+ match00 = re.match(r"rgb\((\s*[0-9\.]+\s*),(\s*[0-9\.]+\s*),(\s*[0-9\.]+\s*)\)", color_str)
23
+ if match00 is not None:
24
+ r = round(float(match00.group(1)))
25
+ g = round(float(match00.group(2)))
26
+ b = round(float(match00.group(3)))
27
+
28
+ return r, g, b
29
+
30
+ match00 = re.match(r"rgba\((\s*[0-9\.]+\s*),(\s*[0-9\.]+\s*),(\s*[0-9\.]+\s*),(\s*[0-9\.]+\s*)\)", color_str)
31
+ if match00 is not None:
32
+ r = round(float(match00.group(1)))
33
+ g = round(float(match00.group(2)))
34
+ b = round(float(match00.group(3)))
35
+
36
+ return r, g, b
37
+
38
+ match00 = re.match(r"hsl\((\s*[0-9\.]+\s*),(\s*[0-9\.]+%\s*),(\s*[0-9\.]+%\s*)\)", color_str)
39
+ if match00 is not None:
40
+ h = float(match00.group(1))
41
+ s = float(match00.group(2).replace("%",""))
42
+ l = float(match00.group(3).replace("%",""))
43
+
44
+
45
+ r, g, b = colorsys.hls_to_rgb(h/360.0, l/100.0, s/100.0)
46
+ r = round(r * 255)
47
+ g = round(g * 255)
48
+ b = round(b * 255)
49
+
50
+ return r, g, b
51
+
52
+ raise ValueError("Invalid color string format")
53
+
54
+ ######
55
+ #color_str = "rgb(190.71875, 42.242530153508774, 42.242530153508774)"
56
+ #color_str = "rgba(190.71875, 42.242530153508774, 42.242530153508774, 1)"
57
+ #r, g, b = colorStringProcessRGB(color_str)
58
+ #print(r, g, b)
app.py ADDED
@@ -0,0 +1,202 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import colorsys
2
+
3
+ import gradio as gr
4
+
5
+ import MiracleCacheTools
6
+
7
+ def numInputCheck(num, min, max):
8
+ if num < min:
9
+ num = min
10
+ if num > max:
11
+ num = max
12
+ return num
13
+
14
+ def RGBInputProcess(r, g, b):
15
+ r = round(numInputCheck(r, 0, 255))
16
+ g = round(numInputCheck(g, 0, 255))
17
+ b = round(numInputCheck(b, 0, 255))
18
+
19
+ # RGB to HEX
20
+ color_hex = f"#{r:02X}{g:02X}{b:02X}"
21
+
22
+ # RGB to HLS
23
+ hls = colorsys.rgb_to_hls(r/255.0, g/255.0, b/255.0)
24
+ hls_h = round(hls[0] * 360)
25
+ hls_l = round(hls[1] * 100)
26
+ hls_s = round(hls[2] * 100)
27
+
28
+ # RGB to HSV
29
+ hsv = colorsys.rgb_to_hsv(r/255.0, g/255.0, b/255.0)
30
+ hsv_h = round(hsv[0] * 360)
31
+ hsv_s = round(hsv[1] * 100)
32
+ hsv_v = round(hsv[2] * 100)
33
+
34
+ return color_hex, color_hex, hls_h, hls_l, hls_s, hsv_h, hsv_s, hsv_v
35
+
36
+ def HLSInputProcess(h, l, s):
37
+ h = round(numInputCheck(h, 0, 360))
38
+ l = round(numInputCheck(l, 0, 100))
39
+ s = round(numInputCheck(s, 0, 100))
40
+
41
+ # HLS to RGB
42
+ rgb = colorsys.hls_to_rgb(h/360.0, l/100.0, s/100.0)
43
+ rgb_r = round(rgb[0] * 255)
44
+ rgb_g = round(rgb[1] * 255)
45
+ rgb_b = round(rgb[2] * 255)
46
+
47
+ # RGB to HEX
48
+ color_hex = f"#{rgb_r:02X}{rgb_g:02X}{rgb_b:02X}"
49
+
50
+ # RGB to HSV
51
+ hsv = colorsys.rgb_to_hsv(rgb_r/255.0, rgb_g/255.0, rgb_b/255.0)
52
+ hsv_h = round(hsv[0] * 360)
53
+ hsv_s = round(hsv[1] * 100)
54
+ hsv_v = round(hsv[2] * 100)
55
+
56
+ return color_hex, color_hex, rgb_r, rgb_g, rgb_b, hsv_h, hsv_s, hsv_v
57
+
58
+ def HSVInputProcess(h, s, v):
59
+ h = round(numInputCheck(h, 0, 360))
60
+ s = round(numInputCheck(s, 0, 100))
61
+ v = round(numInputCheck(v, 0, 100))
62
+
63
+ # HSV to RGB
64
+ rgb = colorsys.hsv_to_rgb(h/360.0, s/100.0, v/100.0)
65
+ rgb_r = round(rgb[0] * 255)
66
+ rgb_g = round(rgb[1] * 255)
67
+ rgb_b = round(rgb[2] * 255)
68
+
69
+ # RGB to HEX
70
+ color_hex = f"#{rgb_r:02X}{rgb_g:02X}{rgb_b:02X}"
71
+
72
+ # RGB to HLS
73
+ hls = colorsys.rgb_to_hls(rgb_r/255.0, rgb_g/255.0, rgb_b/255.0)
74
+ hls_h = round(hls[0] * 360)
75
+ hls_l = round(hls[1] * 100)
76
+ hls_s = round(hls[2] * 100)
77
+
78
+ return color_hex, color_hex, rgb_r, rgb_g, rgb_b, hls_h, hls_l, hls_s
79
+
80
+ def colorInputProcess(color_str):
81
+ #print(f"color_str: {color_str}")
82
+ r, g, b = MiracleCacheTools.colorStringProcessRGB(color_str)
83
+
84
+ # RGB to HEX
85
+ color_hex = f"#{r:02X}{g:02X}{b:02X}"
86
+
87
+ # RGB to HLS
88
+ hls = colorsys.rgb_to_hls(r/255.0, g/255.0, b/255.0)
89
+ hls_h = round(hls[0] * 360)
90
+ hls_l = round(hls[1] * 100)
91
+ hls_s = round(hls[2] * 100)
92
+
93
+ # RGB to HSV
94
+ hsv = colorsys.rgb_to_hsv(r/255.0, g/255.0, b/255.0)
95
+ hsv_h = round(hsv[0] * 360)
96
+ hsv_s = round(hsv[1] * 100)
97
+ hsv_v = round(hsv[2] * 100)
98
+
99
+ return color_hex, r, g, b, hls_h, hls_l, hls_s, hsv_h, hsv_s, hsv_v
100
+
101
+ with gr.Blocks(title="MiracleCacheTools") as demo:
102
+
103
+ gr.Markdown("## 奇迹缓缓辅助工具箱")
104
+
105
+ with gr.Tab("颜色空间换算"):
106
+
107
+ comment_01 = gr.Markdown("注意事项:<br>滑块监听事件为其输入值的改变,快速拖动可能会出现数值更新不及时的bug。(程序缓不过来了)<br>调色时建议先通过点击确定大概位置,再缓慢微移滑块。")
108
+ RGB_hex = gr.Textbox(value="#000000", label="十六进制RGB值", interactive=False)
109
+
110
+ with gr.Row():
111
+ with gr.Column(scale=1):
112
+ gr.Markdown("**RGB**")
113
+ RGB_r = gr.Slider(0, 255, value=0, step=1, label="R(红色)")
114
+ RGB_g = gr.Slider(0, 255, value=0, step=1, label="G(绿色)")
115
+ RGB_b = gr.Slider(0, 255, value=0, step=1, label="B(蓝色)")
116
+
117
+ with gr.Column(scale=1):
118
+ gr.Markdown("**HLS**")
119
+ HLS_h = gr.Slider(0, 360, value=0, step=1, label="H(色相)")
120
+ HLS_l = gr.Slider(0, 100, value=0, step=1, label="L(亮度)")
121
+ HLS_s = gr.Slider(0, 100, value=0, step=1, label="S(饱和度)")
122
+
123
+ with gr.Column(scale=1):
124
+ gr.Markdown("**HSV**")
125
+ HSV_h = gr.Slider(0, 360, value=0, step=1, label="H(色相)")
126
+ HSV_s = gr.Slider(0, 100, value=0, step=1, label="S(饱和度)")
127
+ HSV_v = gr.Slider(0, 100, value=0, step=1, label="V(明度)")
128
+
129
+ with gr.Row():
130
+ with gr.Column(scale=1):
131
+ color = gr.ColorPicker(value="#000000", label="调色板", scale=1)
132
+ gr.Markdown("右侧可上传参考图片,配合调色板的吸管工具使用。<br>试验性功能,请谨慎使用。")
133
+ image_sample = gr.Image(value=None, label="参考图片", interactive=True, scale=3)
134
+
135
+
136
+ RGB_r.input(
137
+ fn=RGBInputProcess,
138
+ inputs=[RGB_r, RGB_g, RGB_b],
139
+ outputs=[color, RGB_hex, HLS_h, HLS_l, HLS_s, HSV_h, HSV_s, HSV_v]
140
+ )
141
+
142
+ RGB_g.input(
143
+ fn=RGBInputProcess,
144
+ inputs=[RGB_r, RGB_g, RGB_b],
145
+ outputs=[color, RGB_hex, HLS_h, HLS_l, HLS_s, HSV_h, HSV_s, HSV_v]
146
+ )
147
+
148
+ RGB_b.input(
149
+ fn=RGBInputProcess,
150
+ inputs=[RGB_r, RGB_g, RGB_b],
151
+ outputs=[color, RGB_hex, HLS_h, HLS_l, HLS_s, HSV_h, HSV_s, HSV_v]
152
+ )
153
+
154
+ HLS_h.input(
155
+ fn=HLSInputProcess,
156
+ inputs=[HLS_h, HLS_l, HLS_s],
157
+ outputs=[color, RGB_hex, RGB_r, RGB_g, RGB_b, HSV_h, HSV_s, HSV_v]
158
+ )
159
+
160
+ HLS_l.input(
161
+ fn=HLSInputProcess,
162
+ inputs=[HLS_h, HLS_l, HLS_s],
163
+ outputs=[color, RGB_hex, RGB_r, RGB_g, RGB_b, HSV_h, HSV_s, HSV_v]
164
+ )
165
+
166
+ HLS_s.input(
167
+ fn=HLSInputProcess,
168
+ inputs=[HLS_h, HLS_l, HLS_s],
169
+ outputs=[color, RGB_hex, RGB_r, RGB_g, RGB_b, HSV_h, HSV_s, HSV_v]
170
+ )
171
+
172
+ HSV_h.input(
173
+ fn=HSVInputProcess,
174
+ inputs=[HSV_h, HSV_s, HSV_v],
175
+ outputs=[color, RGB_hex, RGB_r, RGB_g, RGB_b, HLS_h, HLS_l, HLS_s]
176
+ )
177
+
178
+ HSV_s.input(
179
+ fn=HSVInputProcess,
180
+ inputs=[HSV_h, HSV_s, HSV_v],
181
+ outputs=[color, RGB_hex, RGB_r, RGB_g, RGB_b, HLS_h, HLS_l, HLS_s]
182
+ )
183
+
184
+ HSV_v.input(
185
+ fn=HSVInputProcess,
186
+ inputs=[HSV_h, HSV_s, HSV_v],
187
+ outputs=[color, RGB_hex, RGB_r, RGB_g, RGB_b, HLS_h, HLS_l, HLS_s]
188
+ )
189
+
190
+ color.input(
191
+ fn=colorInputProcess,
192
+ inputs=[color],
193
+ outputs=[RGB_hex, RGB_r, RGB_g, RGB_b, HLS_h, HLS_l, HLS_s, HSV_h, HSV_s, HSV_v]
194
+ )
195
+
196
+
197
+ with gr.Tab("参数批量迁移(测试版)"):
198
+ gr.Markdown("缓不过来了,摆了。")
199
+
200
+
201
+
202
+ demo.launch()
requirements.txt ADDED
@@ -0,0 +1 @@
 
 
1
+ gradio