Fixed color picker issue (HEX to RGB conversion)
Browse files1234566 unmodified lines131415161718193 unmodified lines23242526272829303117 unmodified lines495051525354555657585960import numpy as npimport matplotlib.pyplot as pltimport gradio as grdef plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid):try:6 unmodified linesfunc_text = func_text.strip()func = lambda x: eval(func_text, {"x": x, "np": np})y_values = func(x_values)plt.plot(x_values, y_values, label=f"f(x) = {func_text}", color=color, linestyle=linestyle)plt.xlabel("x")plt.ylabel("f(x)")3 unmodified linesif grid:plt.grid()plt.savefig("high_res_plot.png", dpi=300) # Save high-res plotplt.close()return "high_res_plot.png", "high_res_plot.png"except Exception as e:return f"Error: {e}", None17 unmodified linesoutput_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()1234567891011126 unmodified lines19202122232425262728293 unmodified lines333435363738394041424317 unmodified lines61626364656667686970import numpy as npimport matplotlib.pyplot as pltimport gradio as grimport matplotlib.colors as mcolors # Import color conversion moduledef 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 RGBreturn rgbdef plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid):try:6 unmodified linesfunc_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 Matplotlibrgb_color = hex_to_rgb(color)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 linesif grid:plt.grid()plot_filename = "high_res_plot.png"plt.savefig(plot_filename, dpi=300) # Save high-res plotplt.close()return plot_filename, plot_filenameexcept Exception as e:return f"Error: {e}", None17 unmodified linesoutput_image = gr.Image(label="Function Plot")download_button = gr.File(label="Download High-Res Plot")submit_button.click(plot_function,inputs=[func_str, x_min, x_max, resolution, color, linestyle, grid],outputs=[output_image, download_button])demo.launch()
app.py+15-5