Spaces:
Sleeping
Sleeping
matrixglitch
commited on
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,71 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import numpy as np
|
3 |
+
|
4 |
+
def rgb_to_primary(r, g, b, total_ml):
|
5 |
+
# Convert RGB to range 0-1
|
6 |
+
r, g, b = r/255.0, g/255.0, b/255.0
|
7 |
+
|
8 |
+
# Calculate CMY
|
9 |
+
c = 1 - r
|
10 |
+
m = 1 - g
|
11 |
+
y = 1 - b
|
12 |
+
|
13 |
+
# Calculate K (black)
|
14 |
+
k = min(c, m, y)
|
15 |
+
|
16 |
+
# Calculate true CMY
|
17 |
+
if k < 1:
|
18 |
+
c = (c - k) / (1 - k)
|
19 |
+
m = (m - k) / (1 - k)
|
20 |
+
y = (y - k) / (1 - k)
|
21 |
+
else:
|
22 |
+
c = m = y = 0
|
23 |
+
|
24 |
+
# Calculate white
|
25 |
+
w = 1 - max(c, m, y, k)
|
26 |
+
|
27 |
+
# Normalize to ensure sum is 1
|
28 |
+
total = c + m + y + k + w
|
29 |
+
c, m, y, k, w = c/total, m/total, y/total, k/total, w/total
|
30 |
+
|
31 |
+
# Convert to ml
|
32 |
+
yellow = y * total_ml
|
33 |
+
blue = c * total_ml
|
34 |
+
red = m * total_ml
|
35 |
+
black = k * total_ml
|
36 |
+
white = w * total_ml
|
37 |
+
|
38 |
+
return {
|
39 |
+
"Yellow (ml)": round(yellow, 2),
|
40 |
+
"Blue (ml)": round(blue, 2),
|
41 |
+
"Red (ml)": round(red, 2),
|
42 |
+
"Black (ml)": round(black, 2),
|
43 |
+
"White (ml)": round(white, 2)
|
44 |
+
}
|
45 |
+
|
46 |
+
def calculate_colors(color, total_ml):
|
47 |
+
r, g, b = [int(color[i:i+2], 16) for i in (1, 3, 5)]
|
48 |
+
color_amounts = rgb_to_primary(r, g, b, total_ml)
|
49 |
+
|
50 |
+
# Create color preview
|
51 |
+
color_preview = np.full((50, 50, 3), [r, g, b], dtype=np.uint8)
|
52 |
+
|
53 |
+
return color_amounts, color_preview
|
54 |
+
|
55 |
+
# Create Gradio interface
|
56 |
+
iface = gr.Interface(
|
57 |
+
fn=calculate_colors,
|
58 |
+
inputs=[
|
59 |
+
gr.ColorPicker(label="Select Color"),
|
60 |
+
gr.Number(label="Total Paint Volume (ml)", value=100)
|
61 |
+
],
|
62 |
+
outputs=[
|
63 |
+
gr.JSON(label="Color Amounts"),
|
64 |
+
gr.Image(label="Color Preview", type="numpy")
|
65 |
+
],
|
66 |
+
title="Paint Color Mixer Calculator",
|
67 |
+
description="Select a color and specify the total volume of paint to calculate the required amounts of primary colors."
|
68 |
+
)
|
69 |
+
|
70 |
+
# Launch the interface
|
71 |
+
iface.launch()
|