Fixed color picker issue (HEX to RGB conversion)
Browse files1234567891011126 unmodified lines1920212223242516262728293 unmodified lines3334353626373828394041424317 unmodified lines616263526465666768695970import numpy as npimport matplotlib.pyplot as pltimport gradio as grimport matplotlib.colors as mcolors # Import color conversion module
def hex_to_rgb(hex_color): """Convert HEX color (e.g., '#ff5733') to an RGB tuple for Matplotlib.""" rgb = mcolors.hex2color(hex_color) # Convert HEX to RGB return rgb
def plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid): try:6 unmodified lines func_text = func_text.strip() func = lambda x: eval(func_text, {"x": x, "np": np}) y_values = func(x_values)
# Convert HEX color to RGB before passing to Matplotlib rgb_color = hex_to_rgb(color)
plt.plot(x_values, y_values, label=f"f(x) = {func_text}", color=color, linestyle=linestyle) plt.plot(x_values, y_values, label=f"f(x) = {func_text}", color=rgb_color, linestyle=linestyle)
plt.xlabel("x") plt.ylabel("f(x)")3 unmodified lines if grid: plt.grid()
plot_filename = "high_res_plot.png" plt.savefig("high_res_plot.png", dpi=300) # Save high-res plot plt.savefig(plot_filename, dpi=300) # Save high-res plot plt.close() return "high_res_plot.png", "high_res_plot.png"
return plot_filename, plot_filename
except Exception as e: return f"Error: {e}", None17 unmodified lines output_image = gr.Image(label="Function Plot") download_button = gr.File(label="Download High-Res Plot")
# Ensure this line is correctly aligned within gr.Blocks() submit_button.click( plot_function, inputs=[func_str, x_min, x_max, resolution, color, linestyle, grid], outputs=[output_image, download_button] )
# Ensure this is correctly aligned with gr.Blocks()demo.launch()app.py+15-5