Fixed custom color issue (converted HEX to RGB for Matplotlib)
Browse files12345678910111213141516175 unmodified lines2324252627282930import 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(",")5 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")plt.ylabel("f(x)")12345678910111213141516171819202122235 unmodified lines2930313233343536import numpy as npimport matplotlib.pyplot as pltimport gradio as grimport 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 valid; fallback to black if emptyif not color or not color.startswith("#") or len(color) != 7:color = "#000000" # Default to black# Convert HEX to RGBcolor_rgb = hex_to_rgb(color)x_values = np.linspace(x_min, x_max, resolution)functions = func_str.split(",")5 unmodified linesfunc = lambda x: eval(func_text, {"x": x, "np": np})y_values = func(x_values)# Use RGB color for Matplotlibplt.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