Fixed color picker issue (ensured HEX format & RGBA conversion)
Browse files12345678910111213144 unmodified lines192021222324253 unmodified lines29303132333435import numpy as npimport matplotlib.pyplot as pltimport gradio as grimport osdef plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid):try:# Ensure color is valid (fallback to black if empty)if not color:color = "#000000" # Default to blackx_values = np.linspace(x_min, x_max, resolution)functions = func_str.split(",")4 unmodified linesfunc = lambda x: eval(func_text, {"x": x, "np": np})y_values = func(x_values)# Use HEX color directly without conversionplt.plot(x_values, y_values, label=f"f(x) = {func_text}", color=color, linestyle=linestyle)plt.xlabel("x")3 unmodified linesif grid:plt.grid()# Save the plot correctly and return an absolute pathplot_filename = "high_res_plot.png"abs_path = os.path.abspath(plot_filename) # Convert to absolute pathplt.savefig(abs_path, dpi=300)1234567891011121314151617184 unmodified lines232425262728293 unmodified lines33343536373839import numpy as npimport matplotlib.pyplot as pltimport gradio as grimport matplotlib.colors as mcolorsimport osdef plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid):try:# Ensure color is in proper HEX formatif not color or not color.startswith("#") or len(color) != 7:color = "#000000" # Default to black# Convert to Matplotlib-compatible RGBA formatcolor = mcolors.to_rgba(color)x_values = np.linspace(x_min, x_max, resolution)functions = func_str.split(",")4 unmodified linesfunc = lambda x: eval(func_text, {"x": x, "np": np})y_values = func(x_values)# Use RGBA color format (Matplotlib-compatible)plt.plot(x_values, y_values, label=f"f(x) = {func_text}", color=color, linestyle=linestyle)plt.xlabel("x")3 unmodified linesif grid:plt.grid()# Save the plot and return absolute pathplot_filename = "high_res_plot.png"abs_path = os.path.abspath(plot_filename) # Convert to absolute pathplt.savefig(abs_path, dpi=300)
app.py+8-4