Fixed custom color issue (converted HEX to RGB for Matplotlib)
Browse files
app.py
CHANGED
@@ -1,17 +1,23 @@
|
|
1 |
import numpy as np
|
2 |
import matplotlib.pyplot as plt
|
3 |
import gradio as gr
|
4 |
-
import matplotlib.colors as mcolors
|
5 |
import os
|
6 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
def plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid):
|
8 |
try:
|
9 |
-
# Ensure color is
|
10 |
if not color or not color.startswith("#") or len(color) != 7:
|
11 |
color = "#000000" # Default to black
|
12 |
|
13 |
-
# Convert to
|
14 |
-
|
15 |
|
16 |
x_values = np.linspace(x_min, x_max, resolution)
|
17 |
functions = func_str.split(",")
|
@@ -23,8 +29,8 @@ def plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid):
|
|
23 |
func = lambda x: eval(func_text, {"x": x, "np": np})
|
24 |
y_values = func(x_values)
|
25 |
|
26 |
-
# Use
|
27 |
-
plt.plot(x_values, y_values, label=f"f(x) = {func_text}", color=
|
28 |
|
29 |
plt.xlabel("x")
|
30 |
plt.ylabel("f(x)")
|
|
|
1 |
import numpy as np
|
2 |
import matplotlib.pyplot as plt
|
3 |
import gradio as gr
|
|
|
4 |
import os
|
5 |
|
6 |
+
# Function to convert HEX color to RGB tuple
|
7 |
+
def hex_to_rgb(hex_color):
|
8 |
+
"""Convert HEX color (#RRGGBB) to Matplotlib RGB tuple (R, G, B)."""
|
9 |
+
hex_color = hex_color.lstrip("#") # Remove #
|
10 |
+
rgb = tuple(int(hex_color[i:i+2], 16)/255 for i in (0, 2, 4)) # Convert to (R, G, B)
|
11 |
+
return rgb # Matplotlib expects RGB (not HEX)
|
12 |
+
|
13 |
def plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid):
|
14 |
try:
|
15 |
+
# Ensure color is valid; fallback to black if empty
|
16 |
if not color or not color.startswith("#") or len(color) != 7:
|
17 |
color = "#000000" # Default to black
|
18 |
|
19 |
+
# Convert HEX to RGB
|
20 |
+
color_rgb = hex_to_rgb(color)
|
21 |
|
22 |
x_values = np.linspace(x_min, x_max, resolution)
|
23 |
functions = func_str.split(",")
|
|
|
29 |
func = lambda x: eval(func_text, {"x": x, "np": np})
|
30 |
y_values = func(x_values)
|
31 |
|
32 |
+
# Use RGB color for Matplotlib
|
33 |
+
plt.plot(x_values, y_values, label=f"f(x) = {func_text}", color=color_rgb, linestyle=linestyle)
|
34 |
|
35 |
plt.xlabel("x")
|
36 |
plt.ylabel("f(x)")
|