Spaces:
Sleeping
Sleeping
matrixglitch
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,7 +1,10 @@
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
|
4 |
-
|
|
|
|
|
|
|
5 |
# Convert RGB to range 0-1
|
6 |
r, g, b = r/255.0, g/255.0, b/255.0
|
7 |
|
@@ -28,24 +31,37 @@ def rgb_to_primary(r, g, b, total_ml):
|
|
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
|
32 |
-
yellow = y *
|
33 |
-
blue = c *
|
34 |
-
red = m *
|
35 |
-
black = k *
|
36 |
-
white = w *
|
37 |
|
38 |
return {
|
39 |
-
"Yellow (
|
40 |
-
"Blue (
|
41 |
-
"Red (
|
42 |
-
"Black (
|
43 |
-
"White (
|
44 |
}
|
45 |
|
46 |
-
def calculate_colors(color,
|
47 |
r, g, b = [int(color[i:i+2], 16) for i in (1, 3, 5)]
|
48 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
49 |
|
50 |
# Create color preview
|
51 |
color_preview = np.full((50, 50, 3), [r, g, b], dtype=np.uint8)
|
@@ -57,14 +73,16 @@ iface = gr.Interface(
|
|
57 |
fn=calculate_colors,
|
58 |
inputs=[
|
59 |
gr.ColorPicker(label="Select Color"),
|
60 |
-
gr.Number(label="Total Paint
|
|
|
|
|
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
|
68 |
)
|
69 |
|
70 |
# Launch the interface
|
|
|
1 |
import gradio as gr
|
2 |
import numpy as np
|
3 |
|
4 |
+
# Density of paint in g/ml (this is an approximation, actual density may vary)
|
5 |
+
PAINT_DENSITY = 1.5
|
6 |
+
|
7 |
+
def rgb_to_primary(r, g, b, total_amount, unit):
|
8 |
# Convert RGB to range 0-1
|
9 |
r, g, b = r/255.0, g/255.0, b/255.0
|
10 |
|
|
|
31 |
total = c + m + y + k + w
|
32 |
c, m, y, k, w = c/total, m/total, y/total, k/total, w/total
|
33 |
|
34 |
+
# Convert to selected unit
|
35 |
+
yellow = y * total_amount
|
36 |
+
blue = c * total_amount
|
37 |
+
red = m * total_amount
|
38 |
+
black = k * total_amount
|
39 |
+
white = w * total_amount
|
40 |
|
41 |
return {
|
42 |
+
f"Yellow ({unit})": round(yellow, 2),
|
43 |
+
f"Blue ({unit})": round(blue, 2),
|
44 |
+
f"Red ({unit})": round(red, 2),
|
45 |
+
f"Black ({unit})": round(black, 2),
|
46 |
+
f"White ({unit})": round(white, 2)
|
47 |
}
|
48 |
|
49 |
+
def calculate_colors(color, total_amount, input_unit, output_unit):
|
50 |
r, g, b = [int(color[i:i+2], 16) for i in (1, 3, 5)]
|
51 |
+
|
52 |
+
# Convert input to ml if necessary
|
53 |
+
if input_unit == "mg":
|
54 |
+
total_amount = total_amount / (PAINT_DENSITY * 1000)
|
55 |
+
|
56 |
+
# Calculate color amounts in ml
|
57 |
+
color_amounts_ml = rgb_to_primary(r, g, b, total_amount, "ml")
|
58 |
+
|
59 |
+
# Convert to mg if necessary
|
60 |
+
if output_unit == "mg":
|
61 |
+
color_amounts = {k: v * PAINT_DENSITY * 1000 for k, v in color_amounts_ml.items()}
|
62 |
+
color_amounts = {k.replace("ml", "mg"): round(v, 2) for k, v in color_amounts.items()}
|
63 |
+
else:
|
64 |
+
color_amounts = color_amounts_ml
|
65 |
|
66 |
# Create color preview
|
67 |
color_preview = np.full((50, 50, 3), [r, g, b], dtype=np.uint8)
|
|
|
73 |
fn=calculate_colors,
|
74 |
inputs=[
|
75 |
gr.ColorPicker(label="Select Color"),
|
76 |
+
gr.Number(label="Total Paint Amount", value=100),
|
77 |
+
gr.Radio(["ml", "mg"], label="Input Unit", value="ml"),
|
78 |
+
gr.Radio(["ml", "mg"], label="Output Unit", value="ml")
|
79 |
],
|
80 |
outputs=[
|
81 |
gr.JSON(label="Color Amounts"),
|
82 |
gr.Image(label="Color Preview", type="numpy")
|
83 |
],
|
84 |
title="Paint Color Mixer Calculator",
|
85 |
+
description="Select a color and specify the total amount of paint to calculate the required amounts of primary colors. Choose between ml and mg for input and output units."
|
86 |
)
|
87 |
|
88 |
# Launch the interface
|