Fixed custom color issue (converted HEX to RGB for Matplotlib)
Browse files12344567891011121314915161718131419202122235 unmodified lines29303126273233343536import numpy as npimport matplotlib.pyplot as pltimport gradio as grimport matplotlib.colors as mcolorsimport os
# Function to convert HEX color to RGB tupledef hex_to_rgb(hex_color): """Convert HEX color (#RRGGBB) to Matplotlib RGB tuple (R, G, B).""" hex_color = hex_color.lstrip("#") # Remove # rgb = tuple(int(hex_color[i:i+2], 16)/255 for i in (0, 2, 4)) # Convert to (R, G, B) return rgb # Matplotlib expects RGB (not HEX)
def plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid): try: # Ensure color is in proper HEX format # Ensure color is valid; fallback to black if empty if not color or not color.startswith("#") or len(color) != 7: color = "#000000" # Default to black
# Convert to Matplotlib-compatible RGBA format color = mcolors.to_rgba(color) # Convert HEX to RGB color_rgb = hex_to_rgb(color)
x_values = np.linspace(x_min, x_max, resolution) functions = func_str.split(",")5 unmodified lines func = 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) # Use RGB color for Matplotlib plt.plot(x_values, y_values, label=f"f(x) = {func_text}", color=color_rgb, linestyle=linestyle)
plt.xlabel("x") plt.ylabel("f(x)")app.py+12-6