xoarissa commited on
Commit
f8918f6
·
1 Parent(s): c37d739

Fixed invalid RGBA color issue (default to black)

Browse files
Files changed (1) hide show
  1. app.py +4 -2
app.py CHANGED
@@ -6,6 +6,8 @@ import os # Import os to handle file paths
6
 
7
  def hex_to_rgb(hex_color):
8
  """Convert HEX color (e.g., '#ff5733') to an RGB tuple for Matplotlib."""
 
 
9
  rgb = mcolors.hex2color(hex_color) # Convert HEX to RGB
10
  return rgb
11
 
@@ -21,7 +23,7 @@ def plot_function(func_str, x_min, x_max, resolution, color, linestyle, grid):
21
  func = lambda x: eval(func_text, {"x": x, "np": np})
22
  y_values = func(x_values)
23
 
24
- # Convert HEX color to RGB before passing to Matplotlib
25
  rgb_color = hex_to_rgb(color)
26
 
27
  plt.plot(x_values, y_values, label=f"f(x) = {func_text}", color=rgb_color, linestyle=linestyle)
@@ -55,7 +57,7 @@ with gr.Blocks() as demo:
55
  x_min = gr.Number(label="X Min", value=-10)
56
  x_max = gr.Number(label="X Max", value=10)
57
  resolution = gr.Slider(10, 1000, step=10, label="Resolution", value=100)
58
- color = gr.ColorPicker(label="Line Color")
59
  linestyle = gr.Dropdown(["solid", "dashed", "dotted", "dashdot"], label="Line Style")
60
  grid = gr.Checkbox(label="Show Grid", value=True)
61
  submit_button = gr.Button("Plot Function")
 
6
 
7
  def hex_to_rgb(hex_color):
8
  """Convert HEX color (e.g., '#ff5733') to an RGB tuple for Matplotlib."""
9
+ if hex_color is None: # If no color is selected, default to black
10
+ hex_color = "#000000"
11
  rgb = mcolors.hex2color(hex_color) # Convert HEX to RGB
12
  return rgb
13
 
 
23
  func = lambda x: eval(func_text, {"x": x, "np": np})
24
  y_values = func(x_values)
25
 
26
+ # Ensure a valid color is provided, defaulting to black
27
  rgb_color = hex_to_rgb(color)
28
 
29
  plt.plot(x_values, y_values, label=f"f(x) = {func_text}", color=rgb_color, linestyle=linestyle)
 
57
  x_min = gr.Number(label="X Min", value=-10)
58
  x_max = gr.Number(label="X Max", value=10)
59
  resolution = gr.Slider(10, 1000, step=10, label="Resolution", value=100)
60
+ color = gr.ColorPicker(label="Line Color", value="#000000") # Set default to black
61
  linestyle = gr.Dropdown(["solid", "dashed", "dotted", "dashdot"], label="Line Style")
62
  grid = gr.Checkbox(label="Show Grid", value=True)
63
  submit_button = gr.Button("Plot Function")