Fixed color issue by normalizing HEX to RGB for Matplotlib
Browse files
app.py
CHANGED
@@ -3,12 +3,12 @@ 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)) #
|
11 |
-
return rgb # Matplotlib
|
12 |
|
13 |
def plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid):
|
14 |
try:
|
@@ -16,7 +16,7 @@ def plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid):
|
|
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)
|
@@ -29,7 +29,7 @@ def plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid):
|
|
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")
|
|
|
3 |
import gradio as gr
|
4 |
import os
|
5 |
|
6 |
+
# Function to convert HEX color to normalized 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 '#' if present
|
10 |
+
rgb = tuple(int(hex_color[i:i+2], 16) / 255 for i in (0, 2, 4)) # Normalize (0-1)
|
11 |
+
return rgb # Matplotlib needs RGB values between 0-1
|
12 |
|
13 |
def plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid):
|
14 |
try:
|
|
|
16 |
if not color or not color.startswith("#") or len(color) != 7:
|
17 |
color = "#000000" # Default to black
|
18 |
|
19 |
+
# Convert HEX to RGB and normalize it
|
20 |
color_rgb = hex_to_rgb(color)
|
21 |
|
22 |
x_values = np.linspace(x_min, x_max, resolution)
|
|
|
29 |
func = lambda x: eval(func_text, {"x": x, "np": np})
|
30 |
y_values = func(x_values)
|
31 |
|
32 |
+
# Use normalized 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")
|