xoarissa commited on
Commit
00ea9d2
·
1 Parent(s): 33f9631

Fixed color picker issue (ensured HEX format & RGBA conversion)

Browse files
Files changed (1) hide show
  1. app.py +8 -4
app.py CHANGED
@@ -1,14 +1,18 @@
1
  import numpy as np
2
  import matplotlib.pyplot as plt
3
  import gradio as gr
 
4
  import os
5
 
6
  def plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid):
7
  try:
8
- # Ensure color is valid (fallback to black if empty)
9
- if not color:
10
  color = "#000000" # Default to black
11
 
 
 
 
12
  x_values = np.linspace(x_min, x_max, resolution)
13
  functions = func_str.split(",")
14
 
@@ -19,7 +23,7 @@ def plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid):
19
  func = lambda x: eval(func_text, {"x": x, "np": np})
20
  y_values = func(x_values)
21
 
22
- # Use HEX color directly without conversion
23
  plt.plot(x_values, y_values, label=f"f(x) = {func_text}", color=color, linestyle=linestyle)
24
 
25
  plt.xlabel("x")
@@ -29,7 +33,7 @@ def plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid):
29
  if grid:
30
  plt.grid()
31
 
32
- # Save the plot correctly and return an absolute path
33
  plot_filename = "high_res_plot.png"
34
  abs_path = os.path.abspath(plot_filename) # Convert to absolute path
35
  plt.savefig(abs_path, dpi=300)
 
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 in proper HEX format
10
+ if not color or not color.startswith("#") or len(color) != 7:
11
  color = "#000000" # Default to black
12
 
13
+ # Convert to Matplotlib-compatible RGBA format
14
+ color = mcolors.to_rgba(color)
15
+
16
  x_values = np.linspace(x_min, x_max, resolution)
17
  functions = func_str.split(",")
18
 
 
23
  func = lambda x: eval(func_text, {"x": x, "np": np})
24
  y_values = func(x_values)
25
 
26
+ # Use RGBA color format (Matplotlib-compatible)
27
  plt.plot(x_values, y_values, label=f"f(x) = {func_text}", color=color, linestyle=linestyle)
28
 
29
  plt.xlabel("x")
 
33
  if grid:
34
  plt.grid()
35
 
36
+ # Save the plot and return absolute path
37
  plot_filename = "high_res_plot.png"
38
  abs_path = os.path.abspath(plot_filename) # Convert to absolute path
39
  plt.savefig(abs_path, dpi=300)