mrfakename commited on
Commit
d5d9f43
·
verified ·
1 Parent(s): c37ebaa

Improve color parsing

Browse files
Files changed (1) hide show
  1. app.py +44 -11
app.py CHANGED
@@ -10,7 +10,8 @@ import cairo
10
  import numpy as np
11
  import gradio as gr
12
  from pydub import AudioSegment
13
-
 
14
 
15
  def read_audio(audio, seek=None, duration=None):
16
  """
@@ -205,16 +206,48 @@ def parse_color(colorstr):
205
  "Format for color is 3 floats separated by commas 0.xx,0.xx,0.xx, rgb order"
206
  )
207
 
208
-
209
- def hex_to_rgb(hex_color):
210
- if hex_color.startswith("rgb"):
211
- raise ValueError(f"Unexpected color format: {hex_color}")
212
-
213
- hex_color = hex_color.lstrip('#')
214
- r = int(hex_color[0:2], 16) / 255.0
215
- g = int(hex_color[2:4], 16) / 255.0
216
- b = int(hex_color[4:6], 16) / 255.0
217
- return (r, g, b)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
218
 
219
 
220
  def do_viz(
 
10
  import numpy as np
11
  import gradio as gr
12
  from pydub import AudioSegment
13
+ import re
14
+ import colorsys
15
 
16
  def read_audio(audio, seek=None, duration=None):
17
  """
 
206
  "Format for color is 3 floats separated by commas 0.xx,0.xx,0.xx, rgb order"
207
  )
208
 
209
+ def hex_to_rgb(color):
210
+ """
211
+ Convert color codes to RGB format.
212
+
213
+ Supports:
214
+ - HEX codes with # (e.g., "#FFA07A")
215
+ - HEX codes without # (e.g., "FFA07A")
216
+ - RGB format (e.g., "rgb(255, 160, 122)")
217
+ - HSV format (e.g., "hsv(17, 52, 100)")
218
+
219
+ Returns:
220
+ (r, g, b): Tuple of float values (0-1)
221
+ """
222
+ print(f"Received color: {color}") # Debugging line
223
+
224
+ # HEX with or without #
225
+ if color.startswith("#") or (len(color) in [6, 3] and all(c in "0123456789ABCDEFabcdef" for c in color)):
226
+ color = color.lstrip("#") # Remove # if present
227
+
228
+ # Support 3-digit HEX (e.g., "FA5" -> "FFAA55")
229
+ if len(color) == 3:
230
+ color = "".join([c * 2 for c in color])
231
+
232
+ if len(color) == 6:
233
+ return (int(color[0:2], 16) / 255.0,
234
+ int(color[2:4], 16) / 255.0,
235
+ int(color[4:6], 16) / 255.0)
236
+
237
+ # RGB format (rgb(r, g, b))
238
+ match_rgb = re.match(r"rgb\((\d+),\s*(\d+),\s*(\d+)\)", color)
239
+ if match_rgb:
240
+ r, g, b = map(int, match_rgb.groups())
241
+ return (r / 255.0, g / 255.0, b / 255.0)
242
+
243
+ # HSV format (hsv(h, s, v))
244
+ match_hsv = re.match(r"hsv\((\d+),\s*(\d+),\s*(\d+)\)", color)
245
+ if match_hsv:
246
+ h, s, v = map(int, match_hsv.groups())
247
+ r, g, b = colorsys.hsv_to_rgb(h / 360.0, s / 100.0, v / 100.0)
248
+ return (r, g, b)
249
+
250
+ raise ValueError(f"Invalid color format: {color}")
251
 
252
 
253
  def do_viz(