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

Even more robust color parsing

Browse files
Files changed (1) hide show
  1. app.py +29 -9
app.py CHANGED
@@ -208,44 +208,64 @@ def parse_color(colorstr):
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
 
 
208
 
209
  def hex_to_rgb(color):
210
  """
211
+ Convert color codes to RGBA format.
212
 
213
  Supports:
214
+ - HEX codes with # (e.g., "#FFA07A" or "#FFA07A80" for alpha)
215
+ - HEX codes without # (e.g., "FFA07A" or "FFA07A80")
216
  - RGB format (e.g., "rgb(255, 160, 122)")
217
+ - RGBA format (e.g., "rgba(255, 160, 122, 0.5)")
218
  - HSV format (e.g., "hsv(17, 52, 100)")
219
 
220
  Returns:
221
+ (r, g, b, a): Tuple of float values (0-1)
222
  """
223
  print(f"Received color: {color}") # Debugging line
224
 
225
  # HEX with or without #
226
+ if color.startswith("#") or (len(color) in [6, 3, 8, 4] and all(c in "0123456789ABCDEFabcdef" for c in color)):
227
  color = color.lstrip("#") # Remove # if present
228
 
229
  # Support 3-digit HEX (e.g., "FA5" -> "FFAA55")
230
  if len(color) == 3:
231
+ color = "".join([c * 2 for c in color]) # Expand to 6-digit HEX
232
 
233
+ # Support 4-digit HEX with alpha (e.g., "FA58" -> "FFAA5588")
234
+ if len(color) == 4:
235
+ color = "".join([c * 2 for c in color]) # Expand to 8-digit HEX
236
+
237
+ # HEX without alpha
238
  if len(color) == 6:
239
  return (int(color[0:2], 16) / 255.0,
240
  int(color[2:4], 16) / 255.0,
241
+ int(color[4:6], 16) / 255.0,
242
+ 1.0) # Default alpha to 1 (fully opaque)
243
+
244
+ # HEX with alpha (8-digit HEX)
245
+ if len(color) == 8:
246
+ return (int(color[0:2], 16) / 255.0,
247
+ int(color[2:4], 16) / 255.0,
248
+ int(color[4:6], 16) / 255.0,
249
+ int(color[6:8], 16) / 255.0) # Alpha from 0-255
250
 
251
  # RGB format (rgb(r, g, b))
252
  match_rgb = re.match(r"rgb\((\d+),\s*(\d+),\s*(\d+)\)", color)
253
  if match_rgb:
254
  r, g, b = map(int, match_rgb.groups())
255
+ return (r / 255.0, g / 255.0, b / 255.0, 1.0) # Default alpha to 1
256
+
257
+ # RGBA format (rgba(r, g, b, a))
258
+ match_rgba = re.match(r"rgba\((\d+),\s*(\d+),\s*(\d+),\s*([\d.]+)\)", color)
259
+ if match_rgba:
260
+ r, g, b, a = match_rgba.groups()
261
+ return (int(r) / 255.0, int(g) / 255.0, int(b) / 255.0, float(a)) # Alpha in range [0,1]
262
 
263
  # HSV format (hsv(h, s, v))
264
  match_hsv = re.match(r"hsv\((\d+),\s*(\d+),\s*(\d+)\)", color)
265
  if match_hsv:
266
  h, s, v = map(int, match_hsv.groups())
267
  r, g, b = colorsys.hsv_to_rgb(h / 360.0, s / 100.0, v / 100.0)
268
+ return (r, g, b, 1.0) # Default alpha to 1
269
 
270
  raise ValueError(f"Invalid color format: {color}")
271