Fixed color picker issue (HEX to RGB conversion)
Browse files
app.py
CHANGED
@@ -1,6 +1,12 @@
|
|
1 |
import numpy as np
|
2 |
import matplotlib.pyplot as plt
|
3 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
def plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid):
|
6 |
try:
|
@@ -13,7 +19,11 @@ def plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid):
|
|
13 |
func_text = func_text.strip()
|
14 |
func = lambda x: eval(func_text, {"x": x, "np": np})
|
15 |
y_values = func(x_values)
|
16 |
-
|
|
|
|
|
|
|
|
|
17 |
|
18 |
plt.xlabel("x")
|
19 |
plt.ylabel("f(x)")
|
@@ -23,9 +33,11 @@ def plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid):
|
|
23 |
if grid:
|
24 |
plt.grid()
|
25 |
|
26 |
-
|
|
|
27 |
plt.close()
|
28 |
-
|
|
|
29 |
|
30 |
except Exception as e:
|
31 |
return f"Error: {e}", None
|
@@ -49,12 +61,10 @@ with gr.Blocks() as demo:
|
|
49 |
output_image = gr.Image(label="Function Plot")
|
50 |
download_button = gr.File(label="Download High-Res Plot")
|
51 |
|
52 |
-
# Ensure this line is correctly aligned within gr.Blocks()
|
53 |
submit_button.click(
|
54 |
plot_function,
|
55 |
inputs=[func_str, x_min, x_max, resolution, color, linestyle, grid],
|
56 |
outputs=[output_image, download_button]
|
57 |
)
|
58 |
|
59 |
-
# Ensure this is correctly aligned with gr.Blocks()
|
60 |
demo.launch()
|
|
|
1 |
import numpy as np
|
2 |
import matplotlib.pyplot as plt
|
3 |
import gradio as gr
|
4 |
+
import matplotlib.colors as mcolors # Import color conversion module
|
5 |
+
|
6 |
+
def hex_to_rgb(hex_color):
|
7 |
+
"""Convert HEX color (e.g., '#ff5733') to an RGB tuple for Matplotlib."""
|
8 |
+
rgb = mcolors.hex2color(hex_color) # Convert HEX to RGB
|
9 |
+
return rgb
|
10 |
|
11 |
def plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid):
|
12 |
try:
|
|
|
19 |
func_text = func_text.strip()
|
20 |
func = lambda x: eval(func_text, {"x": x, "np": np})
|
21 |
y_values = func(x_values)
|
22 |
+
|
23 |
+
# Convert HEX color to RGB before passing to Matplotlib
|
24 |
+
rgb_color = hex_to_rgb(color)
|
25 |
+
|
26 |
+
plt.plot(x_values, y_values, label=f"f(x) = {func_text}", color=rgb_color, linestyle=linestyle)
|
27 |
|
28 |
plt.xlabel("x")
|
29 |
plt.ylabel("f(x)")
|
|
|
33 |
if grid:
|
34 |
plt.grid()
|
35 |
|
36 |
+
plot_filename = "high_res_plot.png"
|
37 |
+
plt.savefig(plot_filename, dpi=300) # Save high-res plot
|
38 |
plt.close()
|
39 |
+
|
40 |
+
return plot_filename, plot_filename
|
41 |
|
42 |
except Exception as e:
|
43 |
return f"Error: {e}", None
|
|
|
61 |
output_image = gr.Image(label="Function Plot")
|
62 |
download_button = gr.File(label="Download High-Res Plot")
|
63 |
|
|
|
64 |
submit_button.click(
|
65 |
plot_function,
|
66 |
inputs=[func_str, x_min, x_max, resolution, color, linestyle, grid],
|
67 |
outputs=[output_image, download_button]
|
68 |
)
|
69 |
|
|
|
70 |
demo.launch()
|